Spring|9、springboot 集成 kaptcha验证码
kaptcha: https://github.com/penggle/kaptcha
基于 SimpleCaptcha 开发的验证码插件,google开源插件
1、引入kaptcha maven依赖
kaptcha 依赖
kaptcha
2.3 . 2
|
2、构建 kaptcha 配置类,并添加@Configuration 注解
- springboot启动时会自动读取配置参数,并将DefaultKaptcha 实例 captchaProducer 注入到spring容器中
- 验证码样式及格式在配置参数中设置
/** * @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;
} } |
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();
} } |
从session中获取验证码 :String code = (String)session.getAttribute(Constants.KAPTCHA_SESSION_KEY);
从登录请求中获取用户输入的验证码:request.getParameter("authCode")
二者比较即可
5、总结
- kaptcha 验证码是可以个性定制的:验证码长度(4位)、样式、大小等
- kaptcha 验证码原件保存在session会话中,并设置了失效时间,进一步加强了安全校验
- kaptcha 极其轻量级,集成方便
推荐阅读
- 一个人的碎碎念
- 野营记-第五章|野营记-第五章 讨伐梦魇兽
- Shell-Bash变量与运算符
- 清明,是追思、是传承、是感恩。
- 牛人进化+|牛人进化+ 按自己的意愿过一生
- Activiti(一)SpringBoot2集成Activiti6
- 七老修复好敏感、角质层薄、红血丝
- 华为旁!大社区、地铁新盘,佳兆业城市广场五期!
- 标签、语法规范、内联框架、超链接、CSS的编写位置、CSS语法、开发工具、块和内联、常用选择器、后代元素选择器、伪类、伪元素。
- 螃蟹和这些食物同吃,轻则腹泻、重则中毒!要小心哦~