About Me

My photo
Talk to me... you will know

Tuesday, September 27, 2011

JCaptcha




Lots of guys asked me help in integrating a simple jCaptcha module to their struts application.Was a bit busy with my work so am making this entry late. hope this is helpful
JCaptcha stands for Java Completely Automated Public Test to tell Computers and Humans Apart. Four years since the inception, the 1.0 release is the first declared as stable, available on the central Maven repository or on Sourceforge. The 1.0 release keep the JDK3 backward compatibility, next one 2.0-alpha1 will be built with JDK6 and provide compatibility with JDK5. The JCaptcha project is designed to ease extension, integration and configuration.
jCaptcha has the objective of checking the authenticity of the user trying to perform some action on the page. It allows providing a check on the page by verifying the user’s value with the image value generated to access the functionality of user. It can be used to determine the validity of registration in a page.
Prerequisites:
The user should be using java version 1.6 or higher.
The jar files required are:
·         jcaptcha-all.jar
·         commons-collection-3.2




JCaptcha tries to strictly respect the Inversion of Control pattern, to ease creation of components for concrete applications. An application using JCaptcha should only manipulate a CaptchaService instance.
Step 1:
 For maven users the simple procedure is to include the specific jar dependency to the pom.xml file instance as follows:
<dependency>
     <groupId>com.octo.captcha</groupId>
     <artifactId>jcaptcha</artifactId>
     <version>1.0</version>
</dependency>

For those who don’t use maven the simple procedure is to add the jcaptcha-all.jar and commons-collection-3.2 or greater to the application class path, ie in the WEB-INF/lib folder.


Step 2:
Provide the reference of the new servlet in the web.xml file to instantiate and allow access to the servlet as:

<servlet>
        <servlet-name>jcaptcha</servlet-name>
        <servlet-class>com.octo.captcha.servlet.image.SimpleImageCaptchaServlet</servlet-class>
</servlet>
<servlet-mapping>
        <servlet-name>jcaptcha</servlet-name>
        <url-pattern>/jcaptcha.jpg</url-pattern>
</servlet-mapping>

Step 3:
Add an image tag to the form you want to protect, by referring to the servlet call which asks the SimpleImageCaptchaServlet to generate a fresh new captcha.
<img src="jcaptcha.jpg" /> <input type="text" name="jcaptcha" value="" />

Step 4:
In the action file that validates the previous form’s action validate the input of the user with respect to the captcha value.
String userCaptchaResponse =request.getParameter ("jcaptcha");
boolean captchaPassed = SimpleImageCaptchaServlet.validateResponse(request,userCaptchaResponse);
if(captchaPassed)
{
// proceed to submit action
}
Else
{
// return error to user
}

No comments:

Post a Comment

There was an error in this gadget