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
Spring Boot实战之Redis缓存登录验证码的更多相关文章
- 《Spring Boot实战》笔记(目录)
目录 目 录第一部分 点睛Spring 4.x第1 章 Spring 基础 .............................................................. ...
- Spring Boot + MyBatis + Druid + Redis + Thymeleaf 整合小结
Spring Boot + MyBatis + Druid + Redis + Thymeleaf 整合小结 这两天闲着没事想利用**Spring Boot**加上阿里的开源数据连接池**Druid* ...
- spring boot 实战教程
二八法则 - get more with less Java.spring经过多年的发展,各种技术纷繁芜杂,初学者往往不知道该从何下手.其实开发技术的世界也符合二八法则,80%的场景中只有20%的技术 ...
- spring boot实战(第十三篇)自动配置原理分析
前言 spring Boot中引入了自动配置,让开发者利用起来更加的简便.快捷,本篇讲利用RabbitMQ的自动配置为例讲分析下Spring Boot中的自动配置原理. 在上一篇末尾讲述了Spring ...
- spring boot实战(第十二篇)整合RabbitMQ
前言 最近几篇文章将围绕消息中间件RabbitMQ展开,对于RabbitMQ基本概念这里不阐述,主要讲解RabbitMQ的基本用法.Java客户端API介绍.spring Boot与RabbitMQ整 ...
- Spring Boot实战之逐行释义HelloWorld
一.前言 研究Spring boot也有一小段时间了,最近会将研究东西整理一下给大家分享,大概会有10~20篇左右的博客,整个系列会以一个简单的博客系统作为基础,因为光讲理论很多东西不是特别容易理解 ...
- thymeltesys-基于Spring Boot Oauth2的扫码登录框架
thymeltesys thymelte是一个基于Spring Boot Oauth2的扫码登录框架,使用PostgreSQL存储数据,之后会慢慢支持其他关系型数据库.即使你不使用整个框架,只使用其中 ...
- 在Spring Boot中使用数据缓存
春节就要到了,在回家之前要赶快把今年欠下的技术债还清.so,今天继续.Spring Boot前面已经预热了n篇博客了,今天我们来继续看如何在Spring Boot中解决数据缓存问题.本篇博客是以初识在 ...
- 《spring boot 实战》读书笔记
前言:虽然已经用spring boot开发过一套系统,但是之前都是拿来主义,没有系统的,全面的了解过这套框架.现在通过学习<spring boot实战>这本书,希望温故知新.顺便实现自己的 ...
随机推荐
- java自动拆装箱
介绍 Java 5增加了自动装箱与自动拆箱机制,方便基本类型与包装类型的相互转换操作.(关于基本类型与包装类型之前有记录过https://www.cnblogs.com/xiuzhublog/p/12 ...
- Linux三剑客老三---grep
1.Linux三剑客老三 过滤需要的内容,例子:grep -v oldboy hello.txt grep一般常用参数: -a:在二进制文件中,以文本文件的方式搜索数据. -c:计算找到"搜 ...
- QPS过万,redis大量连接超时怎么解决?
7月2号10点后,刚好某个负责的服务发生大量的redis连接超时的异常(redis.clients.jedis.exceptions.JedisConnectionException),由于本身的数据 ...
- 如何使用python移除/删除非空文件夹?
移除/删除非空文件夹/目录的最有效方法是什么? 1.标准库参考:shutil.rmtree. 根据设计,rmtree在包含只读文件的文件夹树上失败.如果要删除文件夹,不管它是否包含只读文件,请使用 i ...
- 复制输入框内容(兼容ios)
const copyInput = document.querySelector('.copy-container'); copyInput.select(); //安卓可识别进行选中 copyInp ...
- GitHub 热点速览 Vol.35:Let's Go,Rust 大放异彩
摘要:语言之争,一直存在于各类社群,不论是单个编程语言的交流群,亦或是 NoSQL.云开发等技术群,总能看到"要不要换 Go"."Rust 比 C++ 更强"的 ...
- 5000字 | 24张图带你彻底理解Java中的21种锁
本篇主要内容如下: 本篇文章已收纳到我的Java在线文档. Github 我的SpringCloud实战项目持续更新中 帮你总结好的锁: 序号 锁名称 应用 1 乐观锁 CAS 2 悲观锁 synch ...
- 熟练剖分(tree) 树形DP
熟练剖分(tree) 树形DP 题目描述 题目传送门 分析 我们设\(f[i][j]\)为以\(i\)为根节点的子树中最坏时间复杂度小于等于\(j\)的概率 设\(g[i][j]\)为当前扫到的以\( ...
- 据说是最好的记忆工具——Anki
http://www.ankichina.net/ .u1s1,确实挺好用,自建题库,全程自助. 可以插入文字.图片.音频,会安排合理的复习频率,可以随时同步,电脑手机版本全.
- 三层架构的一点理解以及Dapper一对多查询
1.首先说一下自己对三层架构的一点理解 论坛里经常说会出现喜欢面相对象的写法,所以使用EF的,我个人觉得他俩没啥关系,先别反对,先听听我怎么说吧. 三层架构,基本都快说烂了,但今天还是说三层架构:UI ...