kaptcha Producer生成验证码案例

第一步:在pom.xml 中导入所需依赖 如下:



    com.github.penggle
    kaptcha
    2.3.2

第二步:在spring.xml或者spring-mvc.xml 中 添加如下bean对象



    
        
            
                
                    no
                    25,60,170
                    4
                    42
                    1
                    14
                    15
                    Arial
                    com.google.code.kaptcha.impl.NoNoise
                    white
                    com.google.code.kaptcha.impl.ShadowGimpy
                
            
        
    

第三步:后台代码 参考如下

package com.demoinfo.system.controller;

import com.google.code.kaptcha.Producer;
import com.demoinfo.support.constants.Constants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

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 java.awt.image.BufferedImage;
import java.io.IOException;

/**
 * 描述 验证码控制类
 *
 * @author ***
 * @date 2019/1/14 10:32
 */
@Controller
@RequestMapping("/kaptcha")
public class DefaultKaptchaController {
    private Logger logger = LoggerFactory.getLogger(DefaultKaptchaController.class);

    @Resource
    private Producer kaptchaProducer;

    /**
     * 描述 获取验证码
     *
     * @param
     * @return
     * @author ***
     * @date 2019/1/14 15:40
     */
    @RequestMapping("/getCheckCode")
    public void getCheckCode(HttpServletRequest request, HttpServletResponse response) {
        ServletOutputStream out = null;
        try {
            //设置页面不缓存
            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");

            // 生成验证码编码
            String capText = kaptchaProducer.createText();
            // 将验证码编码生成图片
            BufferedImage kaptchaImage = kaptchaProducer.createImage(capText);

            //输出验证码PNG格式图片
            response.setHeader("content-type", "image/png");
            out = response.getOutputStream();
            ImageIO.write(kaptchaImage, "png", out);
            out.flush();

            //获取session,并将验证码编码存放到session中
            HttpSession session = request.getSession();
            session.setAttribute(Constants.DEFAULT_KAPTCHA_SESSION_KEY, capText);
        } catch (IOException e) {
            logger.info("*****服务端获取验证码异常*******" + e.getMessage());
            e.printStackTrace();
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {
                logger.info("*****流关闭异常*******" + e.getMessage());
                e.printStackTrace();
            }
        }
    }

}

上述代码中的 com.icinfo.support.constants.Constants 类是自定义的常量类 参考如下:
public final class Constants {

    /**
     * 登录验证码session key
     */
    public final static String DEFAULT_KAPTCHA_SESSION_KEY = "default_kaptcha_session_key";
    //...
}

第四步:前端代码

jsp代码片段:


对应的js代码片段:

$('.js-checkcode').on('click', function () {
    $(this).attr('src', '/kaptcha/getCheckCode?t=' + Math.random());
})

 

你可能感兴趣的:(Small,Technology,Point,SpringMvc)