Jcaptcha Verification Code

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.