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实战>这本书,希望温故知新.顺便实现自己的 ...
随机推荐
- Ubuntu LNMP环境的搭建
一.安装nginx Step1:安装: sudo apt-get install nginx Step2:查看ngnix 运行状态 : service nginx status 查看80端口是否开启: ...
- Linux进程管理与性能监控
1. 进程管理工具 这一节我们介绍进程管理工具: 使用进程管理工具,我们可以查询程序当前的运行状态,或终止一个进程: 任何进程都与文件关联:我们会用到lsof工具(list opened files) ...
- 【MySQL】面试官问我:MySQL如何实现无数据插入,有数据更新?我是这样回答的!
写在前面 马上就是金九银十的跳槽黄金期了,很多读者都开始出去面试了.这不,又一名读者出去面试被面试官问了一个MySQL的问题:向MySQL中插入数据,如何实现MySQL中没有当前id标识的数据时插入数 ...
- 单表千万行数据库 LIKE 搜索优化手记
我们经常在数据库中使用 LIKE 操作符来完成对数据的模糊搜索,LIKE 操作符用于在 WHERE 子句中搜索列中的指定模式. 如果需要查找客户表中所有姓氏是“张”的数据,可以使用下面的 SQL 语句 ...
- Java开发环境搭建的准备工作
Java开发环境搭建的准备工作 网络配置(修改hosts) 什么时候需要 比如我们在安装homeBrew的时候会遇到 curl: (7) Failed to connect to raw.github ...
- 阿里Java中级开发测试相关试题及答案
------------恢复内容开始------------ 1.JSP文件上传的HTML标签代码定义是? A. <input type="text" name=" ...
- c++ binding code generator based on clang
google it http://www.swig.org/Doc3.0/CSharp.html http://samanbarghi.com/blog/2016/12/06/generate-c-i ...
- Deep Env
https://mirror.tuna.tsinghua.edu.cn/help/ubuntu/ Ready Ubuntu 18.04 这里直接wsl Ubuntu 的软件源配置文件是 /etc/ap ...
- 使用Unity的50个建议
关于这些建议 这些建议并不适用于所有的项目 这些建议是基于我与3-20人的小团队项目经验总结出来的 结构.可重复使用性.明晰度都是有价的——团队规模和项目规模决定了是否值得付这个价. 一些建议也许公然 ...
- 大概是win里最方便快捷的截图+拾色软件——Snipaste
Snipaste 是一个简单而又强大的截图工具,还可以让你将截图贴回到屏幕上!按下 F1 截图快捷键来开始截图,再按 F3贴图快捷键,截图就在桌面置顶显示了.对比文字,对比表格,对比图片,就这么简单! ...