Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 9 Next »

Introduction 

JCaptcha try to strictly respect Inversion of Control pattern, in order to easier creation of components for concrete applications. On the other side the Spring Framework allows to use the power of IOC combined with XML declarations.
So that, every single component of jCaptcha can be declared in XML, and instanced through Spring. The application using jCaptcha should only manipulate a CaptchaService instance.

5 secondes configuration

The most simple configuration possible is to declare the following in your Spring context

<bean class="com.octo.captcha.service.image.DefaultManageableImageCaptchaService" id="imageCaptchaService"/>

Now if you want to customize your engine configuration, you'll need to define your own implementation

Setting a captcha custom service and engine configuration in Spring

In this section we will describe how to set components to create a full CaptchaEngine.

We will start with what we want, the CaptchaEngine, for each component we provide a working and common example, but every component has several implementations, see the Java doc for more details. The construction of each component in XML must respect a constructor of this component.

There are several Engines pre configured, but as we want to control configuration, we have to use the GenericCaptchaEngine, which is build with a list of captcha factories (factories are the ?real? producer of captchas)

<bean class="com.octo.captcha.engine.GenericCaptchaEngine" id="imageEngine">
		<constructor-arg index="0">
			<list>
				<ref bean="imageCaptchaFactory"/>
			</list>
		</constructor-arg>
	</bean>

Then, a CaptchaFactory needs:

  • A word generator, to create the text to read.
  • A wordToImage, to generate the captcha from the text.
	<bean id="CaptchaFactory" class="com.octo.captcha.image.gimpy.GimpyFactory" >
		<constructor-arg><ref bean="wordgen"/></constructor-arg>
		<constructor-arg><ref bean="wordtoimage"/></constructor-arg>
	</bean>

A WordGenerator create a text to be read, it can be random, be a common implementation take words from a list, and can make composition to create a text easier to read for human being. In the example the WordGenerator need a Dictionnary to get real words from.

<bean id="wordgen" class= "com.octo.captcha.component.wordgenerator.ComposeDictionaryWordGenerator" >
		<constructor-arg><ref bean="filedict"/></constructor-arg>
	</bean>

A Dictionary provides words, this one read words from disk.

<bean id="filedict" class="com.octo.captcha.component.wordgenerator.FileDictionnary" >
		<constructor-arg index="0"><value>toddlist</value></constructor-arg>
	</bean>

After to other important part to creat a factory, is the WordToImage component, which is mainly created with three others components:

  • A font generator
  • A background generator
  • A Text paster

This example is a bit more complex one; it takes the usual main three components, but also three deformations, to increase the power of captchas. All three are set to none, a component which create no deformation, see below, and Examples to have more examples of deformations.

<bean id="wordtoimage" class="com.octo.captcha.component.image.wordtoimage.DeformedComposedWordToImage" >
		<constructor-arg index="0"><ref bean="fontGenRandom"/></constructor-arg>
		<constructor-arg index="1"><ref bean="backGenUni"/></constructor-arg>
		<constructor-arg index="2"><ref bean="simpleWhitePaster"/></constructor-arg>
		<constructor-arg index="3"><ref bean="none"/></constructor-arg>
		<constructor-arg index="4"><ref bean="none"/></constructor-arg>
		<constructor-arg index="5"><ref bean="none"/></constructor-arg>
	</bean>

A FontGenerator provide Fonts to a WordToImage, differents fonts increase the difficulties for cracking software using a learning process. This one generate random fonts from a list, and the first two arguments are the minimum size and the maximum size of the font.

	<bean id="fontGenRandom" class="com.octo.captcha.component.image.fontgenerator.RandomFontGenerator" >
		<constructor-arg index="0"><value>40</value></constructor-arg>
		<constructor-arg index="1"><value>50</value></constructor-arg>
		<constructor-arg index="2">
			<list>
				<ref bean="fontArial"/>
				<ref bean="fontTahoma"/>
				<ref bean="fontVerdana"/>
				<ref bean="fontComic"/>
				<ref bean="fontLucida"/>
			</list>
		</constructor-arg>
	</bean>

A font is declared like this :


		<bean id="fontArial" class="java.awt.Font" >
			<constructor-arg index="0"><value>Arial</value></constructor-arg>
			<constructor-arg index="1"><value>0</value></constructor-arg>
			<constructor-arg index="2"><value>10</value></constructor-arg>
		</bean>

The BackgrountGenerator component can be very simple like in the example, single color, or more complex with real picture, or fancy computed shapes. The first two arguments are always, the size (length and height) of the resulting image.

	<bean id="backGenUni" class="com.octo.captcha.component.image.backgroundgenerator.UniColorBackgroundGenerator" >
			<constructor-arg index="0"><value>300</value></constructor-arg>
			<constructor-arg index="1"><value>100</value></constructor-arg>
	</bean>

The TextPaster, according to his name, paste the text on the background. This can be done in a simple way, (see example below), or anothere implementation can paste randomly (but still readable) each character, or can double the text to make computer more confused. TextPaster can be even decorated to put perturbation around the text, a component, TextDecorator, is design for this purpose, see Annexes for some examples. Commons arguments for TextPaster are:
1. Minimal length of the text
2. Maximal length of the text
3. A color generator component to create the text color, see Annexes.
4. A Boolean to precise if each character should have a different color (true) or the same color for all the text (false)

	<bean id="simpleWhitePaster" class="com.octo.captcha.component.image.textpaster.SimpleTextPaster" >
		<constructor-arg type="java.lang.Integer" index="0"><value>4</value></constructor-arg>
		<constructor-arg type="java.lang.Integer" index="1"><value>6</value></constructor-arg>
		<constructor-arg type="com.octo.captcha.component.image.color.ColorGenerator" index="2"><ref bean="colorGenRandomDark"/></constructor-arg>
		<constructor-arg index="3"><value>true</value></constructor-arg>
	</bean>

Now we are ready to setup the CaptchaService singleton...

Setting the CaptchaService in Spring

This component has a special section because it is the top level component, which is actually manipulated within your application. As for the engine there is many already configured CaptchaService, but the one we need is a custom one, which take an CaptchaEngine, as you can see, the BufferedEngineContairner, is in fact CaptchaEngine.

	<bean id="imageCaptchaService"
		class="com.octo.captcha.service.multitype.GenericManageableCaptchaService">
		<constructor-arg index="0">
			<ref bean="imageContainer"/>
		</constructor-arg>
       	 	<constructor-arg index="1"><value>180</value></constructor-arg>
        		<constructor-arg index="2"><value>180000</value></constructor-arg>
	</bean>
 
  • No labels