思路:后端生成验证码,将对应的key和value存放到redis,生成图形化验证码返回给前端,前端输入验证码将参数传给后端做逻辑校验。
一、图片验证码生成工具类
public class CreateImageCode {
// 图片的宽度。
private int width = 120;
// 图片的高度。
private int height = 40;
// 验证码字符个数
private int codeCount = 4;
// 验证码干扰线数
private int lineCount = 30;
// 验证码
private String code = null;
// 验证码图片Buffer
private BufferedImage buffImg = null;
Random random = new Random();
public CreateImageCode() {
creatImage();
}
public CreateImageCode(int width, int height) {
this.width = width;
this.height = height;
creatImage();
}
public CreateImageCode(int width, int height, int codeCount) {
this.width = width;
this.height = height;
this.codeCount = codeCount;
creatImage();
}
public CreateImageCode(int width, int height, int codeCount, int lineCount) {
this.width = width;
this.height = height;
this.codeCount = codeCount;
this.lineCount = lineCount;
creatImage();
}
// 生成图片
private void creatImage() {
int fontWidth = width / codeCount;// 字体的宽度
int fontHeight = height - 5;// 字体的高度
int codeY = height - 8;
// 图像buffer
buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics g = buffImg.getGraphics();
//Graphics2D g = buffImg.createGraphics();
// 设置背景色
g.setColor(getRandColor(200, 250));

本文介绍了如何在后端使用Java创建图像验证码,包括生成随机字符、添加干扰线和噪点,然后将验证码及其键值对存入Redis。同时,文章还展示了如何在前端获取验证码并将其传回后端进行逻辑校验的过程。
最低0.47元/天 解锁文章
1005

被折叠的 条评论
为什么被折叠?



