使用Spring-MVC框架生成验证码

  1. 导入依赖包——kaptcha-2.3.2-jdk14.jar

  2. 在Spring-mvc配置文件中加入以下代码


    <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">yesprop>
                        <prop key="kaptcha.border.color">105,179,90prop>
                        <prop key="kaptcha.textproducer.font.color">blueprop>
                        <prop key="kaptcha.image.width">100prop>
                        <prop key="kaptcha.image.height">60prop>
                        <prop key="kaptcha.textproducer.font.size">45prop>
                        <prop key="kaptcha.session.key">codeprop>
                        <prop key="kaptcha.textproducer.char.length">4prop>
                        <prop key="kaptcha.textproducer.font.names">宋体,楷体,微软雅黑prop>
                    props>
                constructor-arg>
            bean>
        property>
    bean>

3.html代码

    
class="checkcode"> type="text" id="codetext" placeholder="验证码" maxlength="4" class="login_txtbx">"vcode" type="image" alt="验证码" src="captcha-image.do" οnclick="changeCode()">

4.js脚本

/**
 * 获取验证码
 */
function changeCode() {
    $("#vcode").attr('src',
            'captcha-image.do?' + Math.floor(Math.random() * 100));
}

5.controller层

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: 登陆控制类
 * @author 梁志成
 * @date 2016年3月17日 下午11:08:51
 *
 */
@Controller
public class LoginController {

    /**
     * 自动装载验证码产生器
     */
    @Autowired
    private Producer captchaProducer = null;

    /**
     * 
     * @Title: getKaptchaImage
     * @Description: 生成图片验证码
     * @param @param request
     * @param @param response
     * @param @throws Exception
     * @return void
     * @throws
     */
    @RequestMapping(value = "captcha-image")
    public void getKaptchaImage(HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        HttpSession session = request.getSession();

        // 禁止图像缓存
        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();

        // 将内存中的图片通过流动形式输出到客户端
        ImageIO.write(bi, "jpg", out);
        try {
            out.flush();
        } finally {
            out.close();
        }
    }
}

你可能感兴趣的:(spring-mvc)