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缓存登录验证码的更多相关文章

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

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

  2. Spring Boot + MyBatis + Druid + Redis + Thymeleaf 整合小结

    Spring Boot + MyBatis + Druid + Redis + Thymeleaf 整合小结 这两天闲着没事想利用**Spring Boot**加上阿里的开源数据连接池**Druid* ...

  3. spring boot 实战教程

    二八法则 - get more with less Java.spring经过多年的发展,各种技术纷繁芜杂,初学者往往不知道该从何下手.其实开发技术的世界也符合二八法则,80%的场景中只有20%的技术 ...

  4. spring boot实战(第十三篇)自动配置原理分析

    前言 spring Boot中引入了自动配置,让开发者利用起来更加的简便.快捷,本篇讲利用RabbitMQ的自动配置为例讲分析下Spring Boot中的自动配置原理. 在上一篇末尾讲述了Spring ...

  5. spring boot实战(第十二篇)整合RabbitMQ

    前言 最近几篇文章将围绕消息中间件RabbitMQ展开,对于RabbitMQ基本概念这里不阐述,主要讲解RabbitMQ的基本用法.Java客户端API介绍.spring Boot与RabbitMQ整 ...

  6. Spring Boot实战之逐行释义HelloWorld

    一.前言  研究Spring boot也有一小段时间了,最近会将研究东西整理一下给大家分享,大概会有10~20篇左右的博客,整个系列会以一个简单的博客系统作为基础,因为光讲理论很多东西不是特别容易理解 ...

  7. thymeltesys-基于Spring Boot Oauth2的扫码登录框架

    thymeltesys thymelte是一个基于Spring Boot Oauth2的扫码登录框架,使用PostgreSQL存储数据,之后会慢慢支持其他关系型数据库.即使你不使用整个框架,只使用其中 ...

  8. 在Spring Boot中使用数据缓存

    春节就要到了,在回家之前要赶快把今年欠下的技术债还清.so,今天继续.Spring Boot前面已经预热了n篇博客了,今天我们来继续看如何在Spring Boot中解决数据缓存问题.本篇博客是以初识在 ...

  9. 《spring boot 实战》读书笔记

    前言:虽然已经用spring boot开发过一套系统,但是之前都是拿来主义,没有系统的,全面的了解过这套框架.现在通过学习<spring boot实战>这本书,希望温故知新.顺便实现自己的 ...

随机推荐

  1. golang并发

    1.goroutine goroutine是Go并行设计的核心.goroutine说到底其实就是线程,但是它比线程更小,十几个goroutine可能体现在底层就是五六个线程,Go语言内部帮你实现了这些 ...

  2. golang学习笔记:Interface类型断言详情

    原文链接:https://www.2cto.com/kf/201712/703563.html 1. 用于判断变量类型 demo如下: switch t := var.(type){ case str ...

  3. 从《三体》到“中美科技战”,3分钟理解“网络”D丝为什么要迎娶“算力”白富美

    摘要:在多维的世界里,高维的文明对于低维文明具有碾压的优势,而网络也正在从二维走向三维!网络硬件的竞争主要是“芯片+算法”. 从三体到中美科技战,理解网络与算力深度融合助力高维度竞争 1:对抗封锁,需 ...

  4. muduo源码解析4-exception类

    exception class exception:public std::exception { }; 作用: 实现了一个异常类,继承于std::exception,主要用于实现打印线程调用栈信息. ...

  5. Azure Logic App 入门(一)

    一,引言 前两天看一个azure相关的题,接触到一个叫 “Azure Logic App” 的服务,刚好,今天抽空学习以下,顺便结合它做一篇入门的分析文章. 首先,我们得对它有个大概的认识,了解以下A ...

  6. caoz:数据分析这点事

    http://www.wocaoseo.com/thread-53-1-1.html 先声明一下,按照传统的定义,我还真不是数据分析高手,各种关联算法,只会最简单的一种(话说不少场合还算管用):各种挖 ...

  7. 关于对MyBatis.net框架的学习笔记( MyBatis.net是一款灵活性极大,sql由开发者自行在xml中编写, 轻量的ORM映射框架). 同时避免了sql硬编码到代码中不易维护的问题...

    对于为什么要用ORM,为什么又要选择MyBatis.net,这个问题希望读者自行查找资料.这里直接贴出相关的调试笔记. 步骤1)下载与引用. http://code.google.com/p/myba ...

  8. React设计模式相关

    关于我在React设计模式上做的一些思考: 一,项目里实战的经历 最开始我根据组件不同的职能定义,拆分了展示组件和容器组件两大类,后来随着业务逻辑越来越复杂,容器组件代码越来越冗长,我又加入了HOC高 ...

  9. lua 优化

    彻底解析Android缓存机制——LruCache https://www.jianshu.com/p/b49a111147ee lua:部分常用操作的效率对比及代码优化建议(附测试代码) https ...

  10. python3学习笔记回忆录01

    1.print(),print在python3中是一个函数,默认输出内容会自动换行,如果不换行需要加参数end=‘’ print ('hello,',end=' ’) print ('world!') ...