...
JCaptcha tries to strictly respect the Inversion of Control pattern, to ease 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.
An
application using jCaptcha should only manipulate a CaptchaService instance.
...
Code Block |
---|
|
<bean class="com.octo.captcha.service.image.DefaultManageableImageCaptchaService" id="imageCaptchaService"/>
|
10-seconds configuration
Showing how to change the default engine, for getting diferent captchas:
Code Block |
---|
|
<bean id="captchaEngine" class="com.octo.captcha.engine.image.gimpy.SimpleListImageCaptchaEngine"/>
<bean id="captchaService" class="com.octo.captcha.service.image.DefaultManageableImageCaptchaService"
singleton="true">
<property name="captchaEngine" ref="captchaEngine"/>
</bean>
|
Now if you want to customize your engine configuration, you'll need to define your own implementation
...
Code Block |
---|
|
<bean id="imageEngine" class="com.octo.captcha.engine.GenericCaptchaEngine" id="imageEngine">
<constructor-arg index="0">
<list>
<ref bean="imageCaptchaFactoryCaptchaFactory"/>
</list>
</constructor-arg>
</bean>
|
Then, a CaptchaFactory needs:
...
Code Block |
---|
|
<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 creates 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 a human being. In the example the WordGenerator needs a Dictionnary to get real words from.
Code Block |
---|
|
<bean id="wordgen" class= "com.octo.captcha.component.word.wordgenerator.ComposeDictionaryWordGeneratorDictionaryWordGenerator" >
<constructor-arg><ref bean="filedict"/></constructor-arg>
</bean>
|
A Dictionary provides words, this one reads words from diskthe one provided by default, with almost 6000 english words.
Code Block |
---|
|
<bean id="filedict" class="com.octo.captcha.component.wordgeneratorword.FileDictionnaryFileDictionary" >
<constructor-arg index="0"><value>toddlist</value></constructor-arg>
</bean>
|
After to other important part to create a factory, is the WordToImage component, which is mainly created with three others components:
...
Code Block |
---|
|
<bean id="wordtoimage" class="com.octo.captcha.component.image.wordtoimage.DeformedComposedWordToImageComposedWordToImage" >
<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 generates random fonts from a list, and the first two arguments are the minimum size and the maximum size of the font.
Code Block |
---|
|
<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 :
Code Block |
---|
|
<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.
Code Block |
---|
|
<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, pastes the text on the background. This can be done in a simple way, (see example below), or another implementation can paste each character randomly (but still readably), or can double the text to make computers more confused. TextPaster can be even decorated to put perturbations around the text, a component, TextDecorator, is designed 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)
Code Block |
---|
|
<bean id="simpleWhitePaster" class="com.octo.captcha.component.image.textpaster.SimpleTextPaster" >
<constructor-arg type="java.lang.Integer" index="0"><value>4</value><>
<value>3</value>
</constructor-arg>
<constructor-arg type="java.lang.Integer" index="1"><value>6</value><>
<value>5</value>
</constructor-arg>
<constructor-arg type="comjava.octo.captcha.component.image.color.ColorGeneratorawt.Color" index="2"><ref>
<ref bean="colorGenRandomDarkcolorGreen"/><>
</constructor-arg>
</bean>
|
And a color definition:
Code Block |
---|
|
<bean id="colorGreen" class="java.awt.Color" >
<constructor-arg index="0"><value>0</value></constructor-arg>
<constructor-arg index="31"><value>true<><value>255</value></constructor-arg>
<constructor-arg index="2"><value>0</value></constructor-arg>
</bean>
|
Now we are ready to setup the CaptchaService singleton...
...
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 takes an CaptchaEngine, as you can see, the BufferedEngineContairner, is in fact CaptchaEngine.
The arg index1 is the captcha session expering time, in seconds. Next arg especifies the maximun storage size.
Code Block |
---|
|
<bean id="imageCaptchaServicecaptchaService" class="com.octo.captcha.service.multitype.GenericManageableCaptchaService">
<constructor-arg index="0">><ref <ref bean="imageContainerimageEngine"/>
<></constructor-arg>
<constructor-arg index="1"><value>180</value></constructor-arg>
<constructor-arg index="2"><value>180000</value></constructor-arg>
</bean>
|
Code Block |
---|
xml | xml | borderStyle | solid
Other links, resources:
Please refers to External resources