效果如下图:
springBoot 中添加 Redis
https://blog.youkuaiyun.com/qq_36308742/article/details/95178698
文件目录结构如下:
部分文件不做展示。不能完全复现 自行变通。
LonginContreoller.java
package com.haylion.springboot.controller;
import com.haylion.springboot.entity.User;
import com.haylion.springboot.service.LoginService;
import com.haylion.springboot.service.VerificationImgeService;
import com.haylion.springboot.tool.ResultMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@RestController
@RequestMapping("login")
public class LonginContreoller {
@Autowired
private VerificationImgeService verificationImgeService;
@Autowired
private LoginService loginService;
@GetMapping("getImge")//获取验证码图 加载网页时
public void captcha(HttpServletRequest request, HttpServletResponse response) throws IOException {
verificationImgeService.createImage(request, response);
}
}
VerificationImgeService.java
package com.haylion.springboot.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;
import java.util.UUID;
@Service
public class VerificationImgeService {
private int imageWidth = 70;
private int imageHigh = 26;
@Autowired
private RedisService redisService;
public void createImage(HttpServletRequest request, HttpServletResponse response) throws IOException {
BufferedImage image = new BufferedImage(imageWidth, imageHigh, BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
createBackground(g);
String captchaString = createCharacter(g);
String uuid = UUID.randomUUID().toString();
// redis缓存验证码 将验证码缓存与Mengpeng 的文件夹下
redisService.setVal("mengpeng:" + uuid, captchaString, 12000L);
response.addHeader("UUID", uuid);
g.dispose();
OutputStream out = response.getOutputStream();
ImageIO.write(image, "JPEG", out);
out.close();
}
private void createBackground(Graphics g) {
// 填充背景
g.setColor(getRandColor(220, 250));
g.fillRect(0, 0, imageWidth, imageHigh);
// 加入干扰线条
for (int i = 0; i < 8; i++) {
g.setColor(getRandColor(40, 150));
Random random = new Random();
int x = random.nextInt(imageWidth);
int y = random.nextInt(imageHigh);
int x1 = random.nextInt(imageWidth);
int y1 = random.nextInt(imageHigh);
g.drawLine(x, y, x1, y1);
}
}
//颜色
private Color getRandColor(int fc, int bc) {
int f = fc;
int b = bc;
Random random = new Random();
if (f > 255) {
f = 255;
}
if (b > 255) {
b = 255;
}
return new Color(f + random.nextInt(b - f), f + random.nextInt(b - f), f + random.nextInt(b - f));
}
//获取随机字符
private String createCharacter(Graphics g) {
char[] codeSeq = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
'X', 'Y', 'Z', '2', '3', '4', '5', '6', '7', '8', '9'};
String[] fontTypes = {"\u5b8b\u4f53", "\u65b0\u5b8b\u4f53", "\u9ed1\u4f53", "\u6977\u4f53", "\u96b6\u4e66"};
Random random = new Random();
StringBuilder s = new StringBuilder();
for (int i = 0; i < 4; i++) {
String r = String.valueOf(codeSeq[random.nextInt(codeSeq.length)]);//random.nextInt(10));
g.setColor(new Color(50 + random.nextInt(100), 50 + random.nextInt(100), 50 + random.nextInt(100)));
g.setFont(new Font(fontTypes[random.nextInt(fontTypes.length)], Font.BOLD, 26));
g.drawString(r, 15 * i + 5, 19 + random.nextInt(8));
s.append(r);
}
return s.toString();
}
/**
* 验证传入的验证码与缓存中的验证码是否一致
* @param code
* @return
*/
public boolean verificationCode(String code, String uuid) {
String key ="mengpeng:" + uuid;
String originCaptcha = redisService.getVal(key);
if (originCaptcha != null) {
if (code.equals(originCaptcha) == true) {
return true;
} else {
return false;
}
}
return false;
}
}
RedisService.java
package com.haylion.springboot.service;
import com.haylion.springboot.tool.SerializeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import java.util.concurrent.TimeUnit;
@Service
public class RedisService {
@Autowired
private StringRedisTemplate stringRedisTemplate;
/**
* 字符串类型写操作 设置<string,object>
*/
public void setVal(String key, Object val) {
stringRedisTemplate.opsForValue().set(key, SerializeUtil.serialize(val));
}
/**
* 字符串类型写操作 设置<string,object>
*
* @param expire 过期时间
*/
public void setVal(String key, Object val, Long expire) {
stringRedisTemplate.opsForValue().set(key, SerializeUtil.serialize(val), expire, TimeUnit.SECONDS);
}
/**
* 字符串类型读操作 获取缺省为string的反序列化对象
*/
public String getVal(String key) {
return getVal(key, String.class);
}
/**
* 字符串类型读操作 获取反序列化对象
*/
public <T> T getVal(String key, Class<T> clazz) {
return SerializeUtil.deserialize(stringRedisTemplate.opsForValue().get(key), clazz);
}
}
redis 可视化工具会根据你传入的key 名字把它同前缀:的key 生成一个文件夹存放.
其实还是一个个key.