Currently, verification codes are used in many systems and websites to enhance system security.
The following describesJcaptchaTo generate verification code and verify the http://jcaptcha.sourceforge.net/
The following program code is referenced and modeled on springside3.
1. Add the verification code filter on Web. xml.
Create a filter for the verification code Image
<Filter-mapping>
<Filter-Name> jcaptchafilter </filter-Name>
<URL-pattern>/jcaptcha.jpg </url-pattern>
</Filter-mapping>
Verified Filter
<Filter-mapping>
<Filter-Name> jcaptchafilter </filter-Name>
<URL-pattern>/check </url-pattern>
</Filter-mapping>
Write the filter that uses jcaptcha to generate verification codes and implement verification.
<Filter>
<Filter-Name> jcaptchafilter </filter-Name>
<Filter-class> Security. jcaptcha. jcaptchafilter </filter-class>
<Init-param> <! -- Return page upon failure -->
<Param-Name> failureurl </param-Name>
<Param-value>/head. VM </param-value>
</Init-param>
</Filter>
2. Obtain the verification code
Before introducing the Image Code filter, we will introduce the APIs and codes for jcaptcha to generate verification codes.
Verification Code Generation and verification requires jcaptchaCom. Octo. CAPTCHA. Service. image. defamanagmanageableimagecaptchaserviceClass services
Generate a verification code and output it to the client as an image
How to obtain the verification code:Captchaservice. getchallengeforid (ID)
Generate a Random verification code based on your ID
Code snippet
Protected void genernatecaptchaimage (final httpservletrequest request, final httpservletresponse response)
Throws ioexception {
// Set response. The output image client is not cached.
Response. setdateheader ("expires", 1l );
Response. addheader ("Pragma", "No-Cache ");
Response. setheader ("cache-control", "No-cache, no-store, Max-age = 0 ");
Response. setcontenttype ("image/JPEG ");
Servletoutputstream out = response. getoutputstream ();
// Obtain the verification code
Try {
String captchaid = request. getsession (true). GETID ();
// Use the session ID to generate the verification code
Bufferedimage challenge = (bufferedimage) captchaservice. getchallengeforid (captchaid, request. getlocale (); // obtain the verification code
ImageIO. Write (challenge, "jpg", out );
Out. Flush ();
} Catch (captchaserviceexception e ){
System. Out. println (E );
} Finally {
Out. Close ();
}
}
Display the Verification Code
3. Verification Code
Before introducing the Image Code filter, we will introduce the APIs and codes for jcaptcha to generate verification codes.
Verification Code Generation and verification requires jcaptchaCom. Octo. CAPTCHA. Service. image. defamanagmanageableimagecaptchaserviceClass services
Generate a verification code and output it to the client as an image
How to obtain the verification code:Captchaservice. validateresponseforid (captchaid, the entered Verification Code );
Return ValueTrueIndicates verification of customs clearance,FalseVerification Failed
Code snippet
Protected Boolean validatecaptchachallenge (final httpservletrequest request ){
Try {
// Obtain the ID that generates the verification code and use the session ID to generate the verification code
String captchaid = request. getsession (). GETID ();
// Obtain the entered Verification Code
String challengeresponse = request. getparameter (captchaparamtername );
Return captchaservice. validateresponseforid (captchaid, challengeresponse );
} Catch (captchaserviceexception e ){
System. Out. println (E );
Return false;
}
}
Jcaptcha Verification Code