Spring Boot實戰之Redis緩存登陸驗證碼

1.工具類前端

 1 import lombok.experimental.UtilityClass;
 2 
 3 import java.awt.*;
 4 import java.awt.image.BufferedImage;
 5 import java.util.Random;
 6 
 7 @UtilityClass
 8 public class CaptchaUtil {
 9 
10 
11     private int width = 200;
12     private int height = 50;
13 
14 
15     public BufferedImage createImage(){
16         //生成對應寬高的初始圖片
17         return  new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
18     }
19 
20     public String drawRandomText(BufferedImage verifyImg) {
21         Graphics2D graphics = (Graphics2D) verifyImg.getGraphics();
22         //設置畫筆顏色-驗證碼背景色
23         graphics.setColor(Color.WHITE);
24         //填充背景
25         graphics.fillRect(0, 0, width, height);
26         graphics.setFont(new Font("微軟雅黑", Font.PLAIN, 30));
27         //數字和字母的組合
28         String baseNumLetter = "123456789abcdefghijklmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ";
29         StringBuilder sBuffer = new StringBuilder();
30         //旋轉原點的 x 座標
31         int x = 10;
32         String ch = "";
33         Random random = new Random();
34         for (int i = 0; i < 4; i++) {
35             graphics.setColor(getRandomColor());
36             //設置字體旋轉角度
37             //角度小於30度
38             int degree = random.nextInt() % 30;
39             int dot = random.nextInt(baseNumLetter.length());
40             ch = baseNumLetter.charAt(dot) + "";
41             sBuffer.append(ch);
42             //正向旋轉
43             graphics.rotate(degree * Math.PI / 180, x, 45);
44             graphics.drawString(ch, x, 45);
45             //反向旋轉
46             graphics.rotate(-degree * Math.PI / 180, x, 45);
47             x += 48;
48         }
49 
50         //畫干擾線
51         for (int i = 0; i < 6; i++) {
52             // 設置隨機顏色
53             graphics.setColor(getRandomColor());
54             // 隨機畫線
55             graphics.drawLine(random.nextInt(width), random.nextInt(height), random.nextInt(width), random.nextInt(height));
56         }
57         //添加噪點
58         for (int i = 0; i < 30; i++) {
59             int x1 = random.nextInt(width);
60             int y1 = random.nextInt(height);
61             graphics.setColor(getRandomColor());
62             graphics.fillRect(x1, y1, 2, 1);
63         }
64         return sBuffer.toString();
65     }
66 
67     /**
68      * 隨機取色
69      */
70     private Color getRandomColor() {
71         Random ran = new Random();
72         return new Color(ran.nextInt(256), ran.nextInt(256), ran.nextInt(256));
73 
74     }
75 }

2.controller類java

 @GetMapping("/captcha.jpg")
    public void captcha(HttpServletResponse response, HttpServletRequest request) throws IOException {
        response.setHeader("Cache-Control", "no-store, no-cache");
        response.setContentType("image/jpeg");
        // 生成圖片驗證碼
        BufferedImage image = CaptchaUtil.createImage();
        // 生成文字驗證碼
        String randomText = CaptchaUtil.drawRandomText(image);
        // 保存到驗證碼到 redis 有效期兩分鐘
        String t = request.getParameter("t");
        redisTemplate.opsForValue().set(key + t, randomText.toLowerCase(), 2, TimeUnit.MINUTES);
        ServletOutputStream out = response.getOutputStream();
        ImageIO.write(image, "jpeg", out);
    }

3.前端代碼redis

refreshCaptcha: function() {
      this.loginForm.code = ''
      this.loginForm.t = new Date().getTime()
      this.src = 'http://localhost:8081/captcha.jpg?t=' + this.loginForm.t
    }

4.redis配置spring

#Redis配置
## Redis數據庫索引(默認爲0)
spring.redis.database=1
# Redis服務器地址
spring.redis.host=47.98.184.17
# Redis服務器鏈接端口
spring.redis.port=6379
# Redis服務器鏈接密碼
spring.redis.password=root
# 鏈接池最大鏈接數(使用負值表示沒有限制)
spring.redis.jedis.pool.max-active=8
# 鏈接池最大阻塞等待時間(使用負值表示沒有限制)
spring.redis.jedis.pool.max-wait=30000ms
# 鏈接池中的最大空閒鏈接
spring.redis.jedis.pool.max-idle=8
# 鏈接池中的最小空閒鏈接
spring.redis.jedis.pool.min-idle=1
# 鏈接超時時間(毫秒)
spring.redis.timeout=6000ms
相關文章
相關標籤/搜索