Basically funda of Captcha is to distinguish between human or robot(basically bot written to automate to fill some forms of your website which might be used to crash your application).
In this, program will produce some image containing dictionary words or some random alphanumeric words which you have to fill up in corresponding textfield in the form.
For this I am using JCaptcha and Spring. You can get more information JCaptcha from http://forge.octo.com/jcaptcha/confluence/display/general/Home .
So let start with the integration of JCaptcha and Spring.
- First thing is to have one conf file, mine is applicationContext.xml file which get loaded using web.xml file.<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value></context-param>
- In applicationContext.xml file,put these lines for basic configuration :
<bean class="com.octo.captcha.service.image.DefaultManageableImageCaptchaService" id="imageCaptchaService"/>
- Third important thing is to set up the imageEngine bean which is instance of CaptchaEngine.
<bean id="imageEngine"
class="com.octo.captcha.engine.GenericCaptchaEngine">
<constructor-arg index="0">
<list>
<ref bean="CaptchaFactory" />
</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.
class="com.octo.captcha.image.gimpy.GimpyFactory">
<constructor-arg>
<ref bean="wordgen" />
</constructor-arg>
<constructor-arg>
<ref bean="wordtoimage" />
</constructor-arg>
</bean>
class="com.octo.captcha.component.word.wordgenerator.DictionaryWordGenerator">
<constructor-arg>
<ref bean="filedict" />
</constructor-arg>
</bean>
A Dictionary provides words, this one reads words from the one provided by default, with almost 6000 english words.
<bean id="filedict"
class="com.octo.captcha.component.word.FileDictionary">
<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:
- A font generator
- A background generator
- A Text paster
<bean id="wordtoimage"
class="com.octo.captcha.component.image.wordtoimage.ComposedWordToImage">
<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>
</bean>
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" />
</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>
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>
- Minimal length of the text
- Maximal length of the text
- A color generator component to create the text color, see Annexes.<bean id="simpleWhitePaster" class="com.octo.captcha.component.image.textpaster.SimpleTextPaster">
<constructor-arg type="java.lang.Integer" index="0">
<value>3</value>
</constructor-arg>
<constructor-arg type="java.lang.Integer" index="1">
<value>5</value>
</constructor-arg>
<constructor-arg type="java.awt.Color" index="2">
<ref bean="colorGreen" />
</constructor-arg>
</bean>
<bean id="colorGreen" class="java.awt.Color">
<constructor-arg index="0">
<value>0</value>
</constructor-arg>
<constructor-arg index="1">
<value>255</value>
</constructor-arg>
<constructor-arg index="2">
<value>0</value>
</constructor-arg>
</bean>
- In your registration form ,you should have text with name "j_captcha_response" and the second thing is to have image source which will get generated using CaptchaImageGenerator class.
<td><label>Control text</label></td><td><input type="text" name="j_captcha_response" /></td>
</tr>
<tr>
<td colspan="2"><img src="captcha.htm" /></td>
- There is CaptchaImageGenerator which generate a jpeg image file for capchta image using captcha service.
byte[] captchaChallengeAsJpeg = null;
BufferedImage challenge = captchaService.getImageChallengeForID(captchaId,request.getLocale());
JPEGImageEncoder jpegEncoder =
JPEGCodec.createJPEGEncoder(jpegOutputStream);
return
null;
- There will be validator method to validate this image :
void validateCaptcha(HttpServletRequest request, BindException errors){
try {