kaptcha生成验证码

Meven的pom.xml中加入

<!--验证码-->
<dependency>
     <groupId>com.github.axet</groupId>
     <artifactId>kaptcha</artifactId>
     <version>0.0.9</version>
</dependency>

Spring中声明

<bean id="captchaProducer" class="com.google.code.kaptcha.impl.DefaultKaptcha">
    <property name="config">
        <bean class="com.google.code.kaptcha.util.Config">
            <constructor-arg>
                <props>
                    <prop key="kaptcha.border">yes</prop>
                    <prop key="kaptcha.border.color">105,179,90</prop>
                    <prop key="kaptcha.textproducer.font.color">blue</prop>
                    <prop key="kaptcha.image.width">125</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>
                    <prop key="kaptcha.textproducer.font.names">宋体,楷体,微软雅黑</prop>
                </props>
            </constructor-arg>
        </bean>
    </property>
</bean>

编写Controller

@Controller
@RequestMapping("/account")
public class AccountController extends AdminBaseController {

    Producer captchaProducer = null;

    @Autowired
    public void setCaptchaProducer(Producer captchaProducer) {
        this.captchaProducer = captchaProducer;
    }

    @RequestMapping("/kaptchaGet")
    public ModelAndView doGet(HttpServletRequest request, HttpServletResponse response) throws Exception {
        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 jpeg
        response.setContentType("image/jpeg");
        // create the text for the image
        String capText = captchaProducer.createText();
        // store the text in the session
        request.getSession().setAttribute(Constants.KAPTCHA_SESSION_KEY, capText);
        // create the image with the text
        BufferedImage bi = captchaProducer.createImage(capText);
        ServletOutputStream out = response.getOutputStream();
        // write the data out
        ImageIO.write(bi, "jpg", out);
        try {
            out.flush();
        } finally {
            out.close();
        }
        System.out.println("Captchca:"+request.getSession().getAttribute(Constants.KAPTCHA_SESSION_KEY));
        return null;
    }

    @ResponseBody
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public AjaxResult login(LoginModel model,HttpServletRequest request) {
        String captcha = (String) request.getSession().getAttribute(Constants.KAPTCHA_SESSION_KEY);

        /*if (!model.getCaptcha().equals(captcha))
            return ajaxResult(false, "验证码不正确");*/

        ......

        return ajaxResult(true,"");
    }

}

login界面

<div class="form-group">
        <label class="control-label visible-ie8 visible-ie9">验证码</label>
        <input class="form-control placeholder-no-fix validate[required]" type="text" autocomplete="off"
               placeholder="验证码" name="captcha"/>
        <div class="input-icon">
            <img id="captcha1" alt="如果看不清楚,请单击图片刷新!" src="/account/kaptchaGet">
            &nbsp;&nbsp;&nbsp;&nbsp;<a href="javascript:;" onclick="refreshCaptcha()">点击刷新</a>
        </div>
</div>

Js

function refreshCaptcha(){
    var ran = Math.floor(Math.random() * 100)
    $('#captcha1').attr('src','/account/kaptchaGet?' + ran);
}

完成效果

http://imglf5.nosdn.127.net/img/TTMwdVJSKzM0MmhRbDYzakVrTlNiZThlV3l2UzJEcGx1ZjJzV2FTUnI5ekczeC9hRG9xMldnPT0.png?imageView&thumbnail=500x0&quality=96&stripmeta=0

猜你喜欢

转载自blog.csdn.net/qq_25868207/article/details/78875673