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. centos7下的redis集群模式

    1.先安装好单机版的redis 2.Reids安装包里有个集群工具,要复制到/usr/local/bin里去 cd /home/redis/redis-4.0./src ls - cp redis-t ...

  2. hdfs-default.xml

    <?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href="confi ...

  3. 下载 golang.org/x 包出错不用代理的解决办法

    原文链接:https://www.jianshu.com/p/6fe61053c8aa?utm_campaign=maleskine&utm_content=note&utm_medi ...

  4. Redis主从复制(读写分离)

    主从复制(读写分离):读在从库读,写在主库写. 主从复制的好处:避免redis单点故障构建读写分离架构,满足读多写少的需求. 主从架构: 操作(启动实例,在一台机器上启动不同的实例,进行伪主从复制): ...

  5. 循序渐进VUE+Element 前端应用开发(19)--- 后端查询接口和Vue前端的整合

    循序渐进VUE+Element 前端应用开发的系列文章中,前面介绍了系统各个功能的处理实现,本篇随笔从一个主线上介绍前后端开发的整合,让我们从ABP框架后端的查询接口的处理,前端API接口调用的封装, ...

  6. css基本样式设置

    div中文字居中 如何让一个div中的文字水平和垂直居中?设置如下: 给定该div的长宽(或者二者只给出其一也可) .box{ height: 100px; width: 30%; text-alig ...

  7. 利用Python爬虫刷新某网站访问量

    前言:前一段时间看到有博友写了爬虫去刷新博客访问量一篇文章,当时还觉得蛮有意思的,就保存了一下,但是当我昨天准备复现的时候居然发现文章404了.所以本篇文章仅供学习交流,严禁用于商业用途 很多人学习p ...

  8. jdk1.8 新增工具类

    目录 optional 时间API Instant localDateTime LocalDate LocalTime Duration TemporalAdjuster DateTimeFormat ...

  9. CSS中的包含块

    1.初始包含块,浏览器viewport大小 2.非根元素,position:relative/static,包含块为最近的块级框,表格单元或行内祖先框的内容区 3.非根元素,position:abso ...

  10. Istio 的配置分析

    Istio 的配置分析 目录 Istio 的配置分析 Analyzer 的消息格式 ConflictingMeshGatewayVirtualServiceHosts 问题解决 举例 Conflict ...