Using the SPRING-MVC framework to generate a verification code _ verification Code

Source: Internet
Author: User
Tags generator

Import Dependency Pack--kaptcha-2.3.2-jdk14.jar

Add the following code to the SPRING-MVC configuration file

<!--verification Code generator configuration--> <bean id= "Captchaproducer" class= "Com.google.code.kaptcha.impl.DefaultKaptcha" > <property name= "config" > <bean class= "com.google.code.kaptcha.util.Config" > <c onstructor-arg> <props> <prop key= "Kaptcha.border" >yes</prop > <prop key= "kaptcha.border.color" >105,179,90</prop> &LT;PR Op key= "Kaptcha.textproducer.font.color" >blue</prop> <prop key= "Kaptcha.image.width" &G
                        t;100</prop> <prop key= "Kaptcha.image.height" >60</prop> <prop key= "Kaptcha.textproducer.font.size" >45</prop> <prop key= "Kaptcha.session.key"
                        ">code</prop> <prop key=" Kaptcha.textproducer.char.length ">4</prop> <propkey= "Kaptcha.textproducer.font.names" > Arial, italics, Microsoft Ya-black </prop> </props> </c onstructor-arg> </bean> </property> </bean>

3.html Code

    <div class= "Checkcode" >
                <input type= "text" id= "Codetext" placeholder= "Authentication Code" maxlength= "4"
                    class= " LOGIN_TXTBX "><input id=" Vcode type= "image" alt= "Authenticode"
                    src= "captcha-image.do" onclick= "ChangeCode" () "> </input>
            </div>

4.js Script

/**
 * Get Verification code
 /
function ChangeCode () {
    $ ("#vcode"). attr (' src ',
            ' captcha-image.do? ' + Math.floor (Math.random () *));
}

5.controller Layer

Package com.xinrui.hospital.action;
Import Java.awt.image.BufferedImage;

Import java.io.IOException;
Import Javax.annotation.Resource;
Import Javax.imageio.ImageIO;
Import Javax.servlet.ServletOutputStream;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;

Import javax.servlet.http.HttpSession;
Import org.springframework.beans.factory.annotation.Autowired;
Import Org.springframework.stereotype.Controller;
Import org.springframework.web.bind.annotation.RequestMapping;

Import Org.springframework.web.bind.annotation.RequestMethod;
Import Com.google.code.kaptcha.Producer;
Import Com.xinrui.hospital.service.impl.AdminServiceImpl;
Import com.xinrui.hospital.util.Constants;

Import Com.xinrui.hospital.util.PrintWriterUtil; /** * * @ClassName: Logincontroller * @Description: Login Control class * @author Liang Zhicheng * @date March 17, 2016 PM 11:08:51 * * * * * * * * @Contro Ller public class Logincontroller {/** * Automatic load Verification code generator/@Autowired private produceR captchaproducer = null; /** * * @Title: Getkaptchaimage * @Description: Generate picture Verification code * @param @param request * @param @param Response * @param @throws Exception * @return void * @throws/@RequestMapping (value = "Captcha-i Mage ") public void Getkaptchaimage (HttpServletRequest request, httpservletresponse response) throws Except

        Ion {HttpSession session = Request.getsession ();
        Prohibit image caching Response.setdateheader ("Expires", 0);
        Response.setheader ("Cache-control", "No-store, No-cache, must-revalidate");
        Response.AddHeader ("Cache-control", "post-check=0, pre-check=0");
        Response.setheader ("Pragma", "No-cache");

        Response.setcontenttype ("Image/jpeg");
        String Captext = Captchaproducer.createtext ();

        Session.setattribute (Constants.kaptcha_session_key, Captext);
        BufferedImage bi = captchaproducer.createimage (captext); ServletouTputstream out = Response.getoutputstream ();
        Output the picture in memory to the client imageio.write (bi, "JPG", out) through the flow form;
        try {out.flush ();
        finally {out.close ();
 }
    }
}

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.