• SpringBoot实现发送验证码功能


    提示:本次内容主要学习如何做一个发送验证码和识别验证码的功能

    文章目录

    目录

    文章目录

    前言

    一、图片验证码是什么?

    二、使用步骤

    1.创建验证码生成

    2.生成验证码

    总结


    前言

    提示:本次内容主要学习如何做一个发送验证码和识别验证码的功能

    例如:随着现在互联网的不断发展,Web发展可谓是越来越好,但是在这种情况之下,就会出现很多不守规矩的人,可能会利用爬虫来不断地去破解你的用户,此时验证码的作用就发挥出来了,可以有效地阻止一些不法分子来破解你的网站


    一、图片验证码是什么?

    图片验证码(Captcha)是一种通过生成图片来验证用户身份的技术,用于区分人类用户和自动化程序。其主要目的是防止机器人的恶意访问和攻击,保护网站的安全。

    二、使用步骤

    1.创建验证码生成

    代码如下(示例):这个代码主要来源于程序员老罗的教程

    1. package com.easybbs.entity.dto;
    2. import javax.imageio.ImageIO;
    3. import java.awt.*;
    4. import java.awt.image.BufferedImage;
    5. import java.io.IOException;
    6. import java.io.OutputStream;
    7. import java.util.Random;
    8. public class CreateImageCode {
    9. // 图片的宽度。
    10. private int width = 160;
    11. // 图片的高度。
    12. private int height = 40;
    13. // 验证码字符个数
    14. private int codeCount = 4;
    15. // 验证码干扰线数
    16. private int lineCount = 20;
    17. // 验证码
    18. private String code = null;
    19. // 验证码图片Buffer
    20. private BufferedImage buffImg = null;
    21. Random random = new Random();
    22. public CreateImageCode() {
    23. creatImage();
    24. }
    25. public CreateImageCode(int width, int height) {
    26. this.width = width;
    27. this.height = height;
    28. creatImage();
    29. }
    30. public CreateImageCode(int width, int height, int codeCount) {
    31. this.width = width;
    32. this.height = height;
    33. this.codeCount = codeCount;
    34. creatImage();
    35. }
    36. public CreateImageCode(int width, int height, int codeCount, int lineCount) {
    37. this.width = width;
    38. this.height = height;
    39. this.codeCount = codeCount;
    40. this.lineCount = lineCount;
    41. creatImage();
    42. }
    43. // 生成图片
    44. private void creatImage() {
    45. int fontWidth = width / codeCount;// 字体的宽度
    46. int fontHeight = height - 5;// 字体的高度
    47. int codeY = height - 8;
    48. // 图像buffer
    49. buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    50. Graphics g = buffImg.getGraphics();
    51. //Graphics2D g = buffImg.createGraphics();
    52. // 设置背景色
    53. g.setColor(getRandColor(200, 250));
    54. g.fillRect(0, 0, width, height);
    55. // 设置字体
    56. //Font font1 = getFont(fontHeight);
    57. Font font = new Font("Fixedsys", Font.BOLD, fontHeight);
    58. g.setFont(font);
    59. // 设置干扰线
    60. for (int i = 0; i < lineCount; i++) {
    61. int xs = random.nextInt(width);
    62. int ys = random.nextInt(height);
    63. int xe = xs + random.nextInt(width);
    64. int ye = ys + random.nextInt(height);
    65. g.setColor(getRandColor(1, 255));
    66. g.drawLine(xs, ys, xe, ye);
    67. }
    68. // 添加噪点
    69. float yawpRate = 0.01f;// 噪声率
    70. int area = (int) (yawpRate * width * height);
    71. for (int i = 0; i < area; i++) {
    72. int x = random.nextInt(width);
    73. int y = random.nextInt(height);
    74. buffImg.setRGB(x, y, random.nextInt(255));
    75. }
    76. String str1 = randomStr(codeCount);// 得到随机字符
    77. this.code = str1;
    78. for (int i = 0; i < codeCount; i++) {
    79. String strRand = str1.substring(i, i + 1);
    80. g.setColor(getRandColor(1, 255));
    81. // g.drawString(a,x,y);
    82. // a为要画出来的东西,x和y表示要画的东西最左侧字符的基线位于此图形上下文坐标系的 (x, y) 位置处
    83. g.drawString(strRand, i * fontWidth + 3, codeY);
    84. }
    85. }
    86. // 得到随机字符
    87. private String randomStr(int n) {
    88. String str1 = "ABCDEFGHJKMNOPQRSTUVWXYZabcdefghjkmnopqrstuvwxyz234567890";
    89. String str2 = "";
    90. int len = str1.length() - 1;
    91. double r;
    92. for (int i = 0; i < n; i++) {
    93. r = (Math.random()) * len;
    94. str2 = str2 + str1.charAt((int) r);
    95. }
    96. return str2;
    97. }
    98. // 得到随机颜色
    99. private Color getRandColor(int fc, int bc) {// 给定范围获得随机颜色
    100. if (fc > 255) fc = 255;
    101. if (bc > 255) bc = 255;
    102. int r = fc + random.nextInt(bc - fc);
    103. int g = fc + random.nextInt(bc - fc);
    104. int b = fc + random.nextInt(bc - fc);
    105. return new Color(r, g, b);
    106. }
    107. /**
    108. * 产生随机字体
    109. */
    110. private Font getFont(int size) {
    111. Random random = new Random();
    112. Font font[] = new Font[5];
    113. font[0] = new Font("Ravie", Font.PLAIN, size);
    114. font[1] = new Font("Antique Olive Compact", Font.PLAIN, size);
    115. font[2] = new Font("Fixedsys", Font.PLAIN, size);
    116. font[3] = new Font("Wide Latin", Font.PLAIN, size);
    117. font[4] = new Font("Gill Sans Ultra Bold", Font.PLAIN, size);
    118. return font[random.nextInt(5)];
    119. }
    120. // 扭曲方法
    121. private void shear(Graphics g, int w1, int h1, Color color) {
    122. shearX(g, w1, h1, color);
    123. shearY(g, w1, h1, color);
    124. }
    125. private void shearX(Graphics g, int w1, int h1, Color color) {
    126. int period = random.nextInt(2);
    127. boolean borderGap = true;
    128. int frames = 1;
    129. int phase = random.nextInt(2);
    130. for (int i = 0; i < h1; i++) {
    131. double d = (double) (period >> 1) * Math.sin((double) i / (double) period + (6.2831853071795862D * (double) phase) / (double) frames);
    132. g.copyArea(0, i, w1, 1, (int) d, 0);
    133. if (borderGap) {
    134. g.setColor(color);
    135. g.drawLine((int) d, i, 0, i);
    136. g.drawLine((int) d + w1, i, w1, i);
    137. }
    138. }
    139. }
    140. private void shearY(Graphics g, int w1, int h1, Color color) {
    141. int period = random.nextInt(40) + 10; // 50;
    142. boolean borderGap = true;
    143. int frames = 20;
    144. int phase = 7;
    145. for (int i = 0; i < w1; i++) {
    146. double d = (double) (period >> 1) * Math.sin((double) i / (double) period + (6.2831853071795862D * (double) phase) / (double) frames);
    147. g.copyArea(i, 0, 1, h1, 0, (int) d);
    148. if (borderGap) {
    149. g.setColor(color);
    150. g.drawLine(i, (int) d, i, 0);
    151. g.drawLine(i, (int) d + h1, i, h1);
    152. }
    153. }
    154. }
    155. public void write(OutputStream sos) throws IOException {
    156. ImageIO.write(buffImg, "png", sos);
    157. sos.close();
    158. }
    159. public BufferedImage getBuffImg() {
    160. return buffImg;
    161. }
    162. public String getCode() {
    163. return code.toLowerCase();
    164. }
    165. }

    2.生成验证码

    代码如下(示例):

    1. @RequestMapping("/checkCode")
    2. public void checkCode(HttpServletResponse response, HttpSession session) throws IOException {
    3. // 创建一个验证码对象,参数分别指定验证码的宽度、高度、字符数量和干扰线数量。
    4. CreateImageCode vCode = new CreateImageCode(130, 38, 5, 10);
    5. // 设置响应头,确保浏览器不缓存验证码图像。
    6. response.setHeader("Pragma","no-cache");
    7. response.setHeader("Cache-Control","no-cache");
    8. response.setDateHeader("Expires", 0);
    9. // 设置响应类型为JPEG图像。
    10. response.setContentType("image/jpeg");
    11. // 生成验证码字符串。
    12. String code = vCode.getCode();
    13. // 根据类型将验证码存储在不同的session属性中。
    14. session.setAttribute("checkCodeKey",code);
    15. // 将生成的验证码图像写入响应输出流。
    16. vCode.write(response.getOutputStream());
    17. }

    具体识别的时候可以从session里面取到,具体情况具体对待吧


    总结

    这里主要是记录了一下如何生成图片验证码

  • 相关阅读:
    java毕业设计房屋租赁系统演示录像2021mybatis+源码+调试部署+系统+数据库+lw
    【SpringMVC】注解开发
    Web API:ResizeObserver——监听元素大小的变化
    基于SWOT的智能手机企业财务战略研究1.62
    资深工程师整理《数字后端春招面经总结》(附下载)
    【Redis笔记】Redis消息队列方案
    旧苹果短信导入新苹果手机上,iphone短信迁移
    Spark -- Spark3.2.2集成Hudi 0.11.1并同步Hive 3.1.3
    一套SCDM脚本建模与二次开发攻略
    鸿蒙OS开发:典型页面场景【一次开发,多端部署】(信息应用)案例
  • 原文地址:https://blog.csdn.net/weixin_60523038/article/details/139983817