一、验证码工具类使用到的对象

验证码对象

  1. import lombok.Data;
  2. /**
  3. * @Author :JADemo
  4. * @Date :2020/11/2
  5. */
  6. @Data
  7. public class ImageCode {
  8. private String code;
  9. private Long expiration;
  10. }

自定义异常

  1. /**
  2. * 自定义异常(知道原因的异常)
  3. *
  4. * @Auther : wodaoanran
  5. * @Date : 2022/4/14
  6. */
  7. public class CustomException extends RuntimeException{
  8. public CustomException(String msg){
  9. super(msg);
  10. }
  11. }

验证码工具类

  1. import com.hzdy.credit.common.exception.CustomException;
  2. import com.hzdy.credit.entity.system.ImageCode;
  3. import com.hzdy.credit.util.maintain.Assert;
  4. import com.hzdy.credit.util.shiro.ShiroUtils;
  5. import org.apache.commons.lang3.StringUtils;
  6. import org.apache.shiro.session.Session;
  7. import javax.imageio.ImageIO;
  8. import javax.servlet.http.HttpServletRequest;
  9. import javax.servlet.http.HttpSession;
  10. import java.awt.*;
  11. import java.awt.geom.AffineTransform;
  12. import java.awt.image.BufferedImage;
  13. import java.io.IOException;
  14. import java.io.OutputStream;
  15. import java.security.SecureRandom;
  16. import java.util.Arrays;
  17. import java.util.HashMap;
  18. import java.util.Objects;
  19. import java.util.Random;
  20. /**
  21. * 验证码工具类
  22. *
  23. * @author ruoyi
  24. */
  25. public class VerifyCodeUtils
  26. {
  27. // 使用到Algerian字体,系统里没有的话需要安装字体,字体只显示大写,去掉了1,0,i,o几个容易混淆的字符
  28. public static final String VERIFY_CODES = "123456789ABCDEFGHJKLMNPQRSTUVWXYZ";
  29. private static final long EFFECTIVITY_TIME = 1000 * 60;
  30. private static final String IMAGE_CODE_KEY = ImageCode.class.getName();
  31. private static final HashMap<String,ImageCode> MAP = new HashMap();
  32. private static Random random = new SecureRandom();
  33. /**
  34. * 生成随机字符串并存入map
  35. * @param verifySize
  36. * @return
  37. */
  38. public static String generateAndSetMap(int verifySize){
  39. String code = generateVerifyCode(verifySize);
  40. ImageCode imageCode = new ImageCode();
  41. imageCode.setCode(code);
  42. imageCode.setExpiration(System.currentTimeMillis() + EFFECTIVITY_TIME);
  43. // ShiroUtils.getSession().setAttribute(IMAGE_CODE_KEY, imageCode);
  44. MAP.put(IMAGE_CODE_KEY,imageCode);
  45. return code;
  46. }
  47. public static void verifyMap(String code){
  48. //转换为大写
  49. code = code.toUpperCase();
  50. // ImageCode imageCode = (ImageCode) ShiroUtils.getSession().getAttribute(IMAGE_CODE_KEY);
  51. ImageCode imageCode = MAP.get(IMAGE_CODE_KEY);
  52. Assert.isEmpty(imageCode, "验证码为空请重新获取");
  53. // ShiroUtils.getSession().removeAttribute(IMAGE_CODE_KEY);
  54. MAP.remove(IMAGE_CODE_KEY);
  55. if(imageCode.getExpiration() < System.currentTimeMillis()){
  56. throw new CustomException("验证码过期,请重新获取");
  57. }
  58. if(!imageCode.getCode().equals(code)){
  59. throw new CustomException("错误的验证码");
  60. }
  61. }
  62. /**
  63. * 生成字符串验证码
  64. *
  65. * @param verifySize 验证码长度
  66. * @return
  67. */
  68. public static String generateVerifyCode(int verifySize)
  69. {
  70. return generateVerifyCode(verifySize, VERIFY_CODES);
  71. }
  72. /**
  73. * 使用指定源生成验证码
  74. *
  75. * @param verifySize 验证码长度
  76. * @param sources 验证码字符源
  77. * @return
  78. */
  79. public static String generateVerifyCode(int verifySize, String sources)
  80. {
  81. if (sources == null || sources.length() == 0)
  82. {
  83. sources = VERIFY_CODES;
  84. }
  85. int codesLen = sources.length();
  86. Random rand = new Random(System.currentTimeMillis());
  87. StringBuilder verifyCode = new StringBuilder(verifySize);
  88. for (int i = 0; i < verifySize; i++)
  89. {
  90. verifyCode.append(sources.charAt(rand.nextInt(codesLen - 1)));
  91. }
  92. return verifyCode.toString();
  93. }
  94. /**
  95. * 输出指定验证码图片流
  96. *
  97. * @param w
  98. * @param h
  99. * @param os
  100. * @param code
  101. * @throws IOException
  102. */
  103. public static void outputImage(int w, int h, OutputStream os, String code) throws IOException
  104. {
  105. int verifySize = code.length();
  106. BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
  107. Random rand = new Random();
  108. Graphics2D g2 = image.createGraphics();
  109. g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  110. Color[] colors = new Color[5];
  111. Color[] colorSpaces = new Color[] { Color.WHITE, Color.CYAN, Color.GRAY, Color.LIGHT_GRAY, Color.MAGENTA,
  112. Color.ORANGE, Color.PINK, Color.YELLOW };
  113. float[] fractions = new float[colors.length];
  114. for (int i = 0; i < colors.length; i++)
  115. {
  116. colors[i] = colorSpaces[rand.nextInt(colorSpaces.length)];
  117. fractions[i] = rand.nextFloat();
  118. }
  119. Arrays.sort(fractions);
  120. g2.setColor(Color.GRAY);// 设置边框色
  121. g2.fillRect(0, 0, w, h);
  122. Color c = getRandColor(200, 250);
  123. g2.setColor(c);// 设置背景色
  124. g2.fillRect(0, 2, w, h - 4);
  125. // 绘制干扰线
  126. Random random = new Random();
  127. g2.setColor(getRandColor(160, 200));// 设置线条的颜色
  128. for (int i = 0; i < 20; i++)
  129. {
  130. int x = random.nextInt(w - 1);
  131. int y = random.nextInt(h - 1);
  132. int xl = random.nextInt(6) + 1;
  133. int yl = random.nextInt(12) + 1;
  134. g2.drawLine(x, y, x + xl + 40, y + yl + 20);
  135. }
  136. // 添加噪点
  137. float yawpRate = 0.05f;// 噪声率
  138. int area = (int) (yawpRate * w * h);
  139. for (int i = 0; i < area; i++)
  140. {
  141. int x = random.nextInt(w);
  142. int y = random.nextInt(h);
  143. int rgb = getRandomIntColor();
  144. image.setRGB(x, y, rgb);
  145. }
  146. shear(g2, w, h, c);// 使图片扭曲
  147. g2.setColor(getRandColor(100, 160));
  148. int fontSize = h - 4;
  149. Font font = new Font("Algerian", Font.ITALIC, fontSize);
  150. g2.setFont(font);
  151. char[] chars = code.toCharArray();
  152. for (int i = 0; i < verifySize; i++)
  153. {
  154. AffineTransform affine = new AffineTransform();
  155. affine.setToRotation(Math.PI / 4 * rand.nextDouble() * (rand.nextBoolean() ? 1 : -1),
  156. (w / verifySize) * i + fontSize / 2, h / 2);
  157. g2.setTransform(affine);
  158. g2.drawChars(chars, i, 1, ((w - 10) / verifySize) * i + 5, h / 2 + fontSize / 2 - 10);
  159. }
  160. g2.dispose();
  161. ImageIO.write(image, "jpg", os);
  162. }
  163. private static Color getRandColor(int fc, int bc)
  164. {
  165. if (fc > 255)
  166. fc = 255;
  167. if (bc > 255)
  168. bc = 255;
  169. int r = fc + random.nextInt(bc - fc);
  170. int g = fc + random.nextInt(bc - fc);
  171. int b = fc + random.nextInt(bc - fc);
  172. return new Color(r, g, b);
  173. }
  174. private static int getRandomIntColor()
  175. {
  176. int[] rgb = getRandomRgb();
  177. int color = 0;
  178. for (int c : rgb)
  179. {
  180. color = color << 8;
  181. color = color | c;
  182. }
  183. return color;
  184. }
  185. private static int[] getRandomRgb()
  186. {
  187. int[] rgb = new int[3];
  188. for (int i = 0; i < 3; i++)
  189. {
  190. rgb[i] = random.nextInt(255);
  191. }
  192. return rgb;
  193. }
  194. private static void shear(Graphics g, int w1, int h1, Color color)
  195. {
  196. shearX(g, w1, h1, color);
  197. shearY(g, w1, h1, color);
  198. }
  199. private static void shearX(Graphics g, int w1, int h1, Color color)
  200. {
  201. int period = random.nextInt(2);
  202. boolean borderGap = true;
  203. int frames = 1;
  204. int phase = random.nextInt(2);
  205. for (int i = 0; i < h1; i++)
  206. {
  207. double d = (double) (period >> 1)
  208. * Math.sin((double) i / (double) period + (6.2831853071795862D * (double) phase) / (double) frames);
  209. g.copyArea(0, i, w1, 1, (int) d, 0);
  210. if (borderGap)
  211. {
  212. g.setColor(color);
  213. g.drawLine((int) d, i, 0, i);
  214. g.drawLine((int) d + w1, i, w1, i);
  215. }
  216. }
  217. }
  218. private static void shearY(Graphics g, int w1, int h1, Color color)
  219. {
  220. int period = random.nextInt(40) + 10; // 50;
  221. boolean borderGap = true;
  222. int frames = 20;
  223. int phase = 7;
  224. for (int i = 0; i < w1; i++)
  225. {
  226. double d = (double) (period >> 1)
  227. * Math.sin((double) i / (double) period + (6.2831853071795862D * (double) phase) / (double) frames);
  228. g.copyArea(i, 0, 1, h1, 0, (int) d);
  229. if (borderGap)
  230. {
  231. g.setColor(color);
  232. g.drawLine(i, (int) d, i, 0);
  233. g.drawLine(i, (int) d + h1, i, h1);
  234. }
  235. }
  236. }
  237. }

二、验证码使用流程

  1. 使用generateAndSetMap()方法生成验证码
  2. 使用verifyMap()方法进行验证