Kaptcha图片验证码整合

曾经终败给现在 2023-07-05 13:07 78阅读 0赞

流程:生成图片——>把验证码数据放到session中——>登录接口进行比较。

这里对session解释下:当服务端在getSession(true)时创建了Session的同时,服务器会为该Session生成唯一的Session id,而这个Session id在随后的请求中会被用来重新获得已经创建的Session;在Session被创建之后,就可以调用Session相关的方法往Session中增加内容了,而这些内容只会保存在服务器中,发到客户端的只有Session id;当客户端再次发送请求的时候,会将这个Session id带上,服务器接受到请求之后就会依据Session id找到相应的Session,从而再次使用之。所以说这是安全的。

1,导入依赖

  1. <!--验证码-->
  2. <dependency>
  3. <groupId>com.github.penggle</groupId>
  4. <artifactId>kaptcha</artifactId>
  5. </dependency>

2,可以把bean注入Spring中,要使用的时候再Autowired使用。

  1. /**
  2. * 验证码生成相关
  3. */
  4. @Bean
  5. public DefaultKaptcha kaptcha() {
  6. Properties properties = new Properties();
  7. properties.put("kaptcha.border", "no");
  8. properties.put("kaptcha.border.color", "105,179,90");
  9. properties.put("kaptcha.textproducer.font.color", "blue");
  10. properties.put("kaptcha.image.width", "125");
  11. properties.put("kaptcha.image.height", "45");
  12. properties.put("kaptcha.textproducer.font.size", "45");
  13. properties.put("kaptcha.session.key", "code");
  14. properties.put("kaptcha.textproducer.char.length", "4");
  15. properties.put("kaptcha.textproducer.font.names", "宋体,楷体,微软雅黑");
  16. Config config = new Config(properties);
  17. DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
  18. defaultKaptcha.setConfig(config);
  19. return defaultKaptcha;
  20. }

3,提供生成图片的接口,并且把验证码存入session中,之后在登录接口比较。

  1. @Autowired
  2. private Producer producer;
  3. /**
  4. * 生成验证码
  5. */
  6. @RequestMapping("/verificationCode")
  7. public void index(HttpServletRequest request, HttpServletResponse response) {
  8. HttpSession session = request.getSession();
  9. response.setDateHeader("Expires", 0);
  10. // Set standard HTTP/1.1 no-cache headers.
  11. response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
  12. // Set IE extended HTTP/1.1 no-cache headers (use addHeader).
  13. response.addHeader("Cache-Control", "post-check=0, pre-check=0");
  14. // Set standard HTTP/1.0 no-cache header.
  15. response.setHeader("Pragma", "no-cache");
  16. // return a jpeg
  17. response.setContentType("image/jpeg");
  18. // create the text for the image
  19. String capText = producer.createText();
  20. // store the text in the session
  21. session.setAttribute(Constants.KAPTCHA_SESSION_KEY, capText);
  22. // create the image with the text
  23. BufferedImage bi = producer.createImage(capText);
  24. ServletOutputStream out = null;
  25. try {
  26. out = response.getOutputStream();
  27. } catch (IOException e) {
  28. e.printStackTrace();
  29. }
  30. // write the data out
  31. try {
  32. ImageIO.write(bi, "jpg", out);
  33. } catch (IOException e) {
  34. e.printStackTrace();
  35. }
  36. try {
  37. try {
  38. out.flush();
  39. } catch (IOException e) {
  40. e.printStackTrace();
  41. }
  42. } finally {
  43. try {
  44. out.close();
  45. } catch (IOException e) {
  46. e.printStackTrace();
  47. }
  48. }
  49. }

4,比较即可。

本文章有参考其他框架,如有雷同,纯属意外。

发表评论

表情:
评论列表 (有 0 条评论,78人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Kaptcha生成图片验证

    Kaptcha简介 kaptcha 是一个很有用的验证码生成工具。由于它是可配置的,有了它,你能够生成各种样式的验证码。 Kaptcha 是一个可高度配置的实用验证码