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. java Semaphore实现ABC三个线程循环打印

    Semaphore位于java.util.concurrent包下.其中有两个重要的方法acquire()和release().acquire用来获取一个信号量,并且是阻塞型的,如果当前还有可用的信号 ...

  2. python 计算文件md5值

    md5是一种常见不可逆加密算法,使用简单,计算速度快,在很多场景下都会用到,比如:给用户上传的文件命名,数据库中保存的用户密码,下载文件后检验文件是否正确等.下面讲解在python中如何使用md5算法 ...

  3. shell 三剑客之 sed

    sed 在shell 编程里也很常用,功能强大! 同grep一样,sed提供两种方式: 方式一:stdout | sed [option] "pattern command" 从文 ...

  4. 起源seo为何要做seo培训

    http://www.wocaoseo.com/thread-91-1-1.html 焦大,在2010年末左右开始接触seo,2011年3月份正式开始做seo,到如今做seo已经3年了,实话说我没有其 ...

  5. springMVC使用JSR303数据校验

    JSR303注解 hibernate validate是jsr 303的一个参考实现,除支持所有的标准校验注解外,他还支持扩展注解 spring4.0拥有自己独立的数据校验框架,同时支持jsr 303 ...

  6. MyTerm入选北极代码库计划,喜获「Arctic Code Vault Contributor」勋章

  7. 长沙做假证u

    长沙做假证[电/薇:187ヘ1184ヘ0909同号]办各类证件-办毕业证-办离婚证,办学位证书,办硕士毕业证,办理文凭学历,办资格证,办房产证不. 这是一个简单的取最大值程序,可以用于处理 i32 数 ...

  8. Matrix4x4

    Unity3D开发之Matrix4x4矩阵变换 https://www.cnblogs.com/hewei2012/p/4190282.html Matrix4x4 4x4矩阵 http://wiki ...

  9. [BUUOJ记录] [SUCTF 2019]CheckIn

    比较经典的一道文件上传题,考察.user.ini控制解析图片方式 打开题目给出了上传功能,源代码里也没有任何提示,看来需要先测试一下过滤 前后依次提交了php,php5,php7,phtml拓展名的文 ...

  10. 【Azure DevOps系列】Azure DevOps构建并发布Nuget程序包

    在Azure DevOps中,管道可以用来构建解决方案,O(∩_∩)O哈哈~快万能了,本章主要介绍如何创建Nuget包并且将其发布到Nuget服务器的过程. 前面我创建了一个非常简单的类库,这边我不做 ...