基于Java实现随机字母数字验证码

你的名字 2022-12-11 04:25 262阅读 0赞

生成随街验证码

VerifyCode 工具类

  1. package com.meeno.common.cerifycode;
  2. import javax.imageio.ImageIO;
  3. import java.awt.*;
  4. import java.awt.image.BufferedImage;
  5. import java.io.FileNotFoundException;
  6. import java.io.FileOutputStream;
  7. import java.io.IOException;
  8. import java.io.OutputStream;
  9. import java.util.Random;
  10. /**
  11. * @description: 随机验证码
  12. * @author: Wzq
  13. * @create: 2020-09-08 16:55
  14. */
  15. public class VerifyCode {
  16. private int w=70;
  17. private int h=35;
  18. private Random r= new Random();
  19. private String[] fontNames={"宋体","华文楷体","黑体","微软雅黑","楷体_GB2312"};
  20. private String codes="012345678901234567890123456789abcdefghijkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ";
  21. private Color bgColor = new Color(255,255,255);
  22. private String text;
  23. private Color randomColor(){
  24. int red = r.nextInt(150);
  25. int green = r.nextInt(150);
  26. int blue = r.nextInt(150);
  27. return new Color(red,green,blue);
  28. }
  29. private Font randomFont(){
  30. int index = this.r.nextInt(fontNames.length);
  31. String fontName = fontNames[index];
  32. int style = this.r.nextInt(4);
  33. int size = this.r.nextInt(5) + 24;
  34. return new Font(fontName, style, size);
  35. }
  36. private void drawLine (BufferedImage image){
  37. int num = 3;
  38. Graphics2D g2=(Graphics2D)image.getGraphics();
  39. for(int i=0;i<num;i++){
  40. int x1=r.nextInt(w);
  41. int y1=r.nextInt(h);
  42. int x2=r.nextInt(w);
  43. int y2=r.nextInt(h);
  44. g2.setStroke(new BasicStroke(1.5F));
  45. g2.setColor(Color.BLUE);
  46. g2.drawLine(x1,y1,x2,y2);
  47. }
  48. }
  49. private char randomChar(){
  50. int index=r.nextInt(this.codes.length());
  51. return this.codes.charAt(index);
  52. }
  53. private BufferedImage createImage(){
  54. BufferedImage image=new BufferedImage(this.w,this.h,BufferedImage.TYPE_INT_RGB);//BufferedImage.TYPE_INT_RGB
  55. Graphics2D g2 = (Graphics2D)image.getGraphics();
  56. g2.setColor(this.bgColor);
  57. g2.fillRect(0,0,this.w,this.h);
  58. return image;
  59. }
  60. public BufferedImage getImage(){
  61. BufferedImage image=createImage();
  62. Graphics2D g2=(Graphics2D)image.getGraphics();
  63. StringBuilder sb = new StringBuilder();
  64. for(int i =0;i<4;i++){
  65. String s= randomChar()+"";
  66. sb.append(s);
  67. float x= i*1.0F*this.w/4.0F;
  68. g2.setFont(randomFont());
  69. g2.setColor(randomColor());
  70. g2.drawString(s,x,this.h-5);
  71. }
  72. this.text=sb.toString();
  73. drawLine(image);//添加干扰线
  74. return image;
  75. }
  76. //返回验证码上的文本
  77. public String getText(){
  78. return this.text;
  79. }
  80. //保存图片到指定的输出流
  81. public static void output(BufferedImage image, OutputStream out)
  82. throws IOException {
  83. ImageIO.write(image,"JPEG",out);
  84. }
  85. public static void main(String[] args) throws IOException {
  86. VerifyCode vc=new VerifyCode();
  87. BufferedImage bi = vc.getImage();
  88. VerifyCode.output(bi,new FileOutputStream("E:\\work\\temp\\xxx.jpg"));
  89. System.out.println("图片中的验证是:"+vc.getText());
  90. }
  91. }

使用

  1. /**
  2. * 生成随机验证码
  3. */
  4. @RequestMapping("randomVerifyCode.do")
  5. public ResponseBean randomVerifyCode() throws IOException {
  6. VerifyCode vc = new VerifyCode();
  7. BufferedImage bi = vc.getImage();
  8. //VerifyCode.output(bi,response.getOutputStream());
  9. ByteArrayOutputStream out = new ByteArrayOutputStream();
  10. ImageIO.write(bi, "png", out);
  11. byte[] bytes = out.toByteArray();
  12. String str = Base64.encode(bytes);
  13. str = "data:image/png;base64," + str;
  14. String uuid = IdUtil.simpleUUID();
  15. Map<String,String> resultMap = Maps.newHashMap();
  16. resultMap.put("base64", str);
  17. resultMap.put("uuid", uuid);
  18. //save redis
  19. String key = "RandomVerify:" + uuid;
  20. RedisUtil.set(key, vc.getText(), 180);
  21. return ResultUtil.success(resultMap);
  22. }

验证方法

  1. /**
  2. * 账号登录
  3. * @param session
  4. * @param phone
  5. * @param pwd
  6. * @param entryType
  7. * @return
  8. */
  9. @RequestMapping("accountLogin.do")
  10. public ResponseBean accountLogin(final HttpSession session, String phone, String pwd, String entryType,
  11. String uuid, String randomCerifyCode){
  12. //校验验证码
  13. MeenoAssert.hasLength(randomCerifyCode,"randomCerifyCode can not empty!");
  14. Object randomCerifyCodeObj = RedisUtil.get("RandomVerify:" + uuid);
  15. MeenoAssert.notNull(randomCerifyCodeObj, CErrEnum.RANDOM_VERIFY_CODE_FAILURE);
  16. MeenoAssert.isTrue(randomCerifyCode.toLowerCase().equals(randomCerifyCodeObj.toString().toLowerCase()), CErrEnum.RANDOM_VERIFY_CODE_ERR);
  17. LoginResult loginResult = this.employeeService.accountLogin(session, phone, pwd, entryType);
  18. EmpView employeeView = this.employeeService.getEmployee(loginResult.getUserInfo().getId());
  19. Map<String,Object> resultMap = Maps.newHashMap();
  20. resultMap.put("loginResult", loginResult);
  21. resultMap.put("employee", employeeView);
  22. return ResultUtil.success(resultMap);
  23. }

发表评论

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

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

相关阅读