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

原文地址:https://www.cnblogs.com/-wanglei/p/11376257.html

时间: 2024-07-29 09:30:48

Spring Boot实战之Redis缓存登录验证码的相关文章

Spring Boot下的Redis缓存实战

最近在做的一个系统涉及到基础数据的频繁调用,大量的网络开销和数据读写给系统带来了极大的性能压力,我们决定引入缓存机制来缓解系统压力. 什么是缓存 提起缓存机制,大概10个程序员总有5种不同的解释吧(姑且认为只有一半的程序员是通过复制粘贴来学习知识的),我也不能免俗的来说说我的理解. 在回答这个问题之前,我们首先要搞清楚为什么要用缓存? 历史唯物主义揭示了社会发展的基本动力是社会基础矛盾. 运用到软件领域同样适用,一种新技术的出现必然是伴随着特定的矛盾产生的,而缓存的出现正是因为介质提供的实际处理

spring boot项目之redis缓存

以程序为例,tomcat里是我们的java应用,第一步会先从redis获取,如果没有,就会从db上面获取,如果取出了,他还会把取出的东西重新写回redis 使用缓存的步骤: 一.在SellApplication上添加注解@EnableCaching 如果你想引入缓存的话,可以在pom上直接写入以下代码 二.在BuyerProductController.list()方法上添加注解@Cacheable(cacheNames = "product", key = "123&quo

《Spring Boot实战》笔记(目录)

目录 目 录第一部分 点睛Spring 4.x第1 章 Spring 基础 ............................................................................................................. 21.1 Spring 概述 .......................................................................................

Spring Boot 实战

原文引用https://www.dazhuanlan.com/2019/08/26/5d630071b1ce1/ 打开google 打开springboot资料 Search or jump to- Pull requests Issues Marketplace Explore @shandudu 59 1,084 316 hansonwang99/Spring-Boot-In-Action Code Issues 0 Pull requests 0 Projects 0 Wiki Secur

spring boot实战——微信点餐系统01:项目设计

技术要点: 前端: Vue 后端:Spring Boot + BootStrap + FreeMarker + JQuery 详细技术: Spring Boot : 数据库方面:Spring Boot + JPA 缓存方面:Spring Boot + Redis(会讨论 分布式Session + 分布式锁) 消息推送方面:WebSocker 微信方面: 微信扫码登录 模板消息推送 微信支付与退款 项目设计: 角色划分 功能模块划分:商品:订单:类目 部署架构 数据库设计 数据库设计: 遇到问题

Spring Boot实战之Filter实现使用JWT进行接口认证

Spring Boot实战之Filter实现使用JWT进行接口认证 jwt(json web token) 用户发送按照约定,向服务端发送 Header.Payload 和 Signature,并包含认证信息(密码),验证通过后服务端返回一个token,之后用户使用该token作为登录凭证,适合于移动端和api jwt使用流程 本文示例接上面几篇文章中的代码进行编写,请阅读本文的同时可以参考前面几篇文章 1.添加依赖库jjwt,本文中构造jwt及解析jwt都使用了jjwt库 <dependenc

Spring Boot实战之逐行释义HelloWorld

一.前言  研究Spring boot也有一小段时间了,最近会将研究东西整理一下给大家分享,大概会有10~20篇左右的博客,整个系列会以一个简单的博客系统作为基础,因为光讲理论很多东西不是特别容易理解,并且如果每次通过一个简单的小程序也无法系统的把握好一些知识点,所以就以一个简单的系统作为基础来讲,看看通过spring boot如何实现一个完整系统.本系列除了Spring boot基本的知识点之外,还会涉及到Spring boot与数据库.缓存(redis).消息队列等的结合以及多实例部署等方面

Spring Boot实战之定制URL匹配规则

本文首发于个人网站:Spring Boot实战之定制URL匹配规则 构建web应用程序时,并不是所有的URL请求都遵循默认的规则.有时,我们希望RESTful URL匹配的时候包含定界符".",这种情况在Spring中可以称之为"定界符定义的格式":有时,我们希望识别斜杠的存在.Spring提供了接口供开发人员按照需求定制. 在之前的几篇文章中,可以通过WebConfiguration类来定制程序中的过滤器.格式化工具等等,同样得,也可以在这个类中用类似的办法配置&

Spring Boot实战之定制type Formatters

本文首发于个人网站:Spring Boot实战之定制type Formatters 前面我们有篇文章介绍了PropertyEditors,是用来将文本类型转换成指定的Java类型,不过,考虑到PropertyEditor的无状态和非线程安全特性,Spring 3增加了一个Formatter接口来替代它.Formatters提供和PropertyEditor类似的功能,但是提供线程安全特性,也可以实现字符串和对象类型的互相转换. 假设在我们的程序中,需要根据一本书的ISBN字符串得到对应的book