Spring|9、springboot 集成 kaptcha验证码

kaptcha: https://github.com/penggle/kaptcha
基于 SimpleCaptcha 开发的验证码插件,google开源插件 1、引入kaptcha maven依赖
kaptcha 依赖



com.github.penggle
kaptcha
2.3.2


2、构建 kaptcha 配置类,并添加@Configuration 注解
  • springboot启动时会自动读取配置参数,并将DefaultKaptcha 实例 captchaProducer 注入到spring容器中
  • 验证码样式及格式在配置参数中设置
kaptcha 配置类
/**
* @author changneng
* @version 1.0
* @title: KaptchaConfig
* @description: Kaptcha 验证码配置类
* @date 2019/4/1216:47
*/
@Configuration
public class KaptchaConfig {
@Bean
public DefaultKaptcha getDefaultKaptcha(){
DefaultKaptcha captchaProducer = new DefaultKaptcha();
Properties properties = new Properties();
/**
* @description: 设置 Kaptcha 生成验证码格式
* @param []
* @return com.google.code.kaptcha.impl.DefaultKaptcha
* @author changneng
【Spring|9、springboot 集成 kaptcha验证码】* @date 2019/4/12 16:53
* @version 1.0
*/
properties.setProperty("kaptcha.textproducer.font.color", "blue");
properties.setProperty("kaptcha.textproducer.char.length", "4");
properties.setProperty("kaptcha.textproducer.font.size", "30");
properties.setProperty("kaptcha.image.width", "120");
properties.setProperty("kaptcha.image.height", "40");
properties.setProperty("kaptcha.textproducer.char.space", "6");
properties.setProperty("kaptcha.textproducer.font.names", "Arial, Courier");
Config config = new Config(properties);
captchaProducer.setConfig(config);
return captchaProducer;
}
}
3、构建kaptcha 验证码的controller 方法,以流的形式返回给客户端(登录页面)

kaptcha 提供验证码服务
/**
* @description: TODO
* @param
* @return
* @author changneng
* @date 2019/4/12 16:27
* @version 1.0
*/
@RequestMapping(value=https://www.it610.com/article/"/getKaptcha", method = RequestMethod.GET)
public void getKaptchaImage(HttpServletRequest request, HttpServletResponse response) throws Exception {
HttpSession session = request.getSession();
String code = (String)session.getAttribute(Constants.KAPTCHA_SESSION_KEY);
System.out.println("******************上次的验证码是: " + code + "******************");
// 设置浏览器不要对数据进行缓存
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
session.setAttribute(Constants.KAPTCHA_SESSION_KEY, capText);
//settimg time out
session.setMaxInactiveInterval(120);
System.out.println("session中验证码的超时时间是:"+session.getMaxInactiveInterval()+" seconds");
System.out.println("******************本次的验证码是: " + 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();
}
}
4、kaptcha 验证码 校验
从session中获取验证码 :String code = (String)session.getAttribute(Constants.KAPTCHA_SESSION_KEY);
从登录请求中获取用户输入的验证码:request.getParameter("authCode")
二者比较即可
5、总结
  • kaptcha 验证码是可以个性定制的:验证码长度(4位)、样式、大小等
  • kaptcha 验证码原件保存在session会话中,并设置了失效时间,进一步加强了安全校验
  • kaptcha 极其轻量级,集成方便

    推荐阅读