当前位置:   article > 正文

SpringBoot实现图形验证码验证_springboot 图形验证码

springboot 图形验证码

文章目录

概要

  使用图像化技术,在SpringBoot中实现图像验证码验证。

实现过程

1. 添加工具类

        该工具定义了图形验证码的参数、内容以及生成验证码的方法。

  1. import java.awt.Color;
  2. import java.awt.Font;
  3. import java.awt.Graphics2D;
  4. import java.awt.image.BufferedImage;
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;
  7. import java.io.OutputStream;
  8. import java.util.Random;
  9. import javax.imageio.ImageIO;
  10. /**
  11. * 验证码生成器
  12. * @author dsna
  13. *
  14. */
  15. public class ValidateCode {
  16. // 图片的宽度。
  17. private int width = 160;
  18. // 图片的高度。
  19. private int height = 40;
  20. // 验证码字符个数
  21. private int codeCount = 5;
  22. // 验证码干扰线数
  23. private int lineCount = 150;
  24. // 验证码
  25. private String code = null;
  26. // 验证码图片Buffer
  27. private BufferedImage buffImg=null;
  28. private char[] codeSequence = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
  29. 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
  30. 'X', 'Y', 'Z', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
  31. public ValidateCode() {
  32. this.createCode();
  33. }
  34. /**
  35. *
  36. * @param width 图片宽
  37. * @param height 图片高
  38. */
  39. public ValidateCode(int width,int height) {
  40. this.width=width;
  41. this.height=height;
  42. this.createCode();
  43. }
  44. /**
  45. *
  46. * @param width 图片宽
  47. * @param height 图片高
  48. * @param codeCount 字符个数
  49. * @param lineCount 干扰线条数
  50. */
  51. public ValidateCode(int width,int height,int codeCount,int lineCount) {
  52. this.width=width;
  53. this.height=height;
  54. this.codeCount=codeCount;
  55. this.lineCount=lineCount;
  56. this.createCode();
  57. }
  58. public void createCode() {
  59. int x = 0,fontHeight=0,codeY=0;
  60. int red = 0, green = 0, blue = 0;
  61. x = width / (codeCount +2);//每个字符的宽度
  62. fontHeight = height - 2;//字体的高度
  63. codeY = height - 4;
  64. // 图像buffer
  65. buffImg = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);
  66. Graphics2D g = buffImg.createGraphics();
  67. // 生成随机数
  68. Random random = new Random();
  69. // 将图像填充为白色
  70. g.setColor(Color.WHITE);
  71. g.fillRect(0, 0, width, height);
  72. // 创建字体
  73. ImgFontByte imgFont=new ImgFontByte();
  74. Font font =imgFont.getFont(fontHeight);
  75. g.setFont(font);
  76. for (int i = 0; i < lineCount; i++) {
  77. int xs = random.nextInt(width);
  78. int ys = random.nextInt(height);
  79. int xe = xs+random.nextInt(width/8);
  80. int ye = ys+random.nextInt(height/8);
  81. red = random.nextInt(255);
  82. green = random.nextInt(255);
  83. blue = random.nextInt(255);
  84. g.setColor(new Color(red, green, blue));
  85. g.drawLine(xs, ys, xe, ye);
  86. }
  87. // randomCode记录随机产生的验证码
  88. StringBuffer randomCode = new StringBuffer();
  89. // 随机产生codeCount个字符的验证码。
  90. for (int i = 0; i < codeCount; i++) {
  91. String strRand = String.valueOf(codeSequence[random.nextInt(codeSequence.length)]);
  92. // 产生随机的颜色值,让输出的每个字符的颜色值都将不同。
  93. red = random.nextInt(255);
  94. green = random.nextInt(255);
  95. blue = random.nextInt(255);
  96. g.setColor(new Color(red, green, blue));
  97. g.drawString(strRand, (i + 1) * x, codeY);
  98. // 将产生的四个随机数组合在一起。
  99. randomCode.append(strRand);
  100. }
  101. // 将四位数字的验证码保存到Session中。
  102. code=randomCode.toString();
  103. }
  104. public void write(String path) throws IOException {
  105. OutputStream sos = new FileOutputStream(path);
  106. this.write(sos);
  107. }
  108. public void write(OutputStream sos) throws IOException {
  109. ImageIO.write(buffImg, "png", sos);
  110. sos.close();
  111. }
  112. public BufferedImage getBuffImg() {
  113. return buffImg;
  114. }
  115. public String getCode() {
  116. return code;
  117. }
  118. }
2.在Controller层调用
  1. @GetMapping("/checkCode")
  2. public void checkCode(HttpSession session, HttpServletResponse resp, Integer type) throws IOException {
  3. //新建一个对象
  4. ValidateCode validateCode = new ValidateCode(200,30,4,20);
  5. //拿到验证码
  6. String codes = validateCode.getCode();
  7. ///通过response,以流的方式把图像返回给前端
  8. validateCode.write(resp.getOutputStream());
  9. }
3.如何验证?

        我们在给前端响应图片验证码的同时,也把code放到了session,所以下一步请求处理的时候可以使用getAttribute("key") 的方式获得code,进行比对即可。

小结

SpringBoot中实现图形验证码验证

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Guff_9hys/article/detail/901951
推荐阅读
相关标签
  

闽ICP备14008679号