zoukankan      html  css  js  c++  java
  • 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类

     @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.前端代码

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

    4.redis配置

    #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
  • 相关阅读:
    Linux进程关系
    ambari 卸载脚本
    CentOS-7.2安装Ambari-2.6.1
    MYSQL57密码策略修改
    CentOS7 离线安装MySQL
    centos 安装mysql Package: akonadi-mysql-1.9.2-4.el7.x86_64 (@anaconda)
    mysql 数据备份
    spring-boot-starter-thymeleaf对没有结束符的HTML5标签解析出错
    ssh: scp命令
    python:os.path
  • 原文地址:https://www.cnblogs.com/-wanglei/p/11376257.html
Copyright © 2011-2022 走看看