Using Kaptcha to generate a verification code under the Spring MVC framework

Source: Internet
Author: User
Tags border color

1. Download the jar package and import it.

  Kaptcha-2.3.2.jar

2,Spring configuration file Applicationcontext.xml.

<bean id="Captchaproducer" class="Com.google.code.kaptcha.impl.DefaultKaptcha"> <property name="Config"> <beanclass="Com.google.code.kaptcha.util.Config"> <constructor-arg> <props> <prop key="Kaptcha.border">yes</prop> <prop key="Kaptcha.border.color"> the,179, -</prop> <prop key="Kaptcha.textproducer.font.color">blue</prop> <prop key="Kaptcha.image.width"> the</prop> <prop key="Kaptcha.image.height"> $</prop> <prop key="kaptcha.textproducer.font.size"> $</prop> <prop key="Kaptcha.session.key">code</prop> <prop key="Kaptcha.textproducer.char.length">4</prop> <prop key="Kaptcha.textproducer.font.names"> arial, Italic, Microsoft Black </prop> </props> </constructor-arg> </bean > </property> </bean>

The configuration entries are as follows:

Kaptcha.border Whether there is a border default to True we can set the Yes,no kaptcha.border.color border color by default to Color.Black kaptcha.border.thickness Border thickness default to 1 Kaptcha.producer.impl Authenticode generator defaults to Defaultkaptcha kaptcha.textproducer.impl Authenticode text generator defaults to Defaulttextcrea Tor Kaptcha.textproducer.Charthe. String code text character content range defaults to abcde2345678gfynmnpwx kaptcha.textproducer.Char. Length code text character length default to 5 kaptcha.textproducer.font.names captcha text font style default to new font ("Arial", 1, FontSize),NewFont ("Courier", 1, FontSize) kaptcha.textproducer.font.size Verification Code text character size default to Kaptcha.textproducer.font.color captcha text character color default to Color.b LACK Kaptcha.textproducer.Char. Space captcha text character spacing defaults to 2 Kaptcha.noise.impl Authenticode Noise Generation object default to Defaultnoise kaptcha.noise.color Authenticode noise color default to COLOR.BL ACK Kaptcha.obscurificator.impl Verification Code style engine defaults to waterripple Kaptcha.word.impl captcha text character rendering defaults to Defaultwordrenderer Kaptch A.background.impl Verification Code Background generator defaults to defaultbackground kaptcha.background.clear.from captcha background color progressive default to Color.light_gray KAPT   Cha.background.clear.to Verification Code Background color Progressive default to Color.White kaptcha.image.width captcha picture width default to $ kaptcha.image.height captcha picture height The default is

3, JSP page.

<script type= ' text/javascript ' src= ' ${base}/js/jquery-1.10.2.min.js ' ></script> <script type= "text/ JavaScript ">                      $(function(){                 $(' #kaptchaImage '). Click (function() {//Generate verification Code$( This). Hide (). attr (' src ', ' ${base}/captcha-image.htm ' + Math.floor (math.random () *100) . FadeIn (); Event.cancelbubble=true;          });                 }); functionChangeCode () {$ (' #kaptchaImage '). Hide (). attr (' src ', ' ${base}/captcha-image.htm? ' + Math.floor (math.random () *100) . FadeIn (); Event.cancelbubble=true; }    </script>
    <Divclass= "Chknumber">            <label>Verification Code:<inputname= "Kaptcha"type= "text"ID= "Kaptcha"maxlength= "4"class= "Chknumber_input" />                         </label>            <BR/>            <imgsrc= "${base}/captcha-image.htm"ID= "Kaptchaimage"style= "Margin-bottom: -3px"/>            <ahref="#"onclick= "ChangeCode ()">I can't see it clearly.</a>      </Div>

4, create the Verification code generation control class Captchaimagecreatecontroller.java.

ImportJava.awt.image.BufferedImage; ImportJavax.imageio.ImageIO; ImportJavax.servlet.ServletOutputStream; Importjavax.servlet.http.HttpServletRequest; ImportJavax.servlet.http.HttpServletResponse; Importorg.springframework.beans.factory.annotation.Autowired; ImportOrg.springframework.stereotype.Controller; Importorg.springframework.web.bind.annotation.RequestMapping; ImportOrg.springframework.web.servlet.ModelAndView; Importcom.google.code.kaptcha.Constants; ImportCom.google.code.kaptcha.Producer; @Controller @RequestMapping ("/")   Public classCaptchaimagecreatecontroller {PrivateProducer Captchaproducer =NULL; @Autowired Public voidsetcaptchaproducer (Producer captchaproducer) { This. Captchaproducer =Captchaproducer; } @RequestMapping ("Captcha-image")       PublicModelandview HandleRequest (httpservletrequest request, httpservletresponse response)throwsException {response.setdateheader ("Expires", 0); //Set Standard http/1.1 no-cache headers. Response.setheader ("Cache-control", "No-store, No-cache, Must-revalidate"); //Set IE Extended http/1.1 no-cache headers (use AddHeader). Response.AddHeader ("Cache-control", "Post-check=0, pre-check=0"); //Set Standard http/1.0 No-cache header. Response.setheader ("Pragma", "No-cache"); //return a JPEGResponse.setcontenttype ("Image/jpeg"); //Create the text for the imageString Captext =Captchaproducer.createtext (); //Store the text in the sessionrequest.getsession (). SetAttribute (Constants.kaptcha_session_key, Captext); //Create the image with the textBufferedImage bi =captchaproducer.createimage (Captext); Servletoutputstream out=Response.getoutputstream (); //write the data outImageio.write (BI, "JPG", out); Try{Out.flush (); } finally{out.close (); }          return NULL; }    } 

5, the control class gets the current verification code.

Effect

Using Kaptcha to generate a verification code under the Spring MVC framework

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.