Commit c58d2c8d authored by danfuman's avatar danfuman

Merge branch 'dev20230707' of http://192.168.60.201/root/dsk-operate-sys into dev20230707

parents 9dcddaf0 c7b5f678
package com.dsk.web.controller.common; package com.dsk.web.controller.common;
import java.awt.*;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.io.IOException; import java.io.IOException;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
...@@ -72,7 +73,8 @@ public class CaptchaController ...@@ -72,7 +73,8 @@ public class CaptchaController
else if ("char".equals(captchaType)) else if ("char".equals(captchaType))
{ {
capStr = code = captchaProducer.createText(); capStr = code = captchaProducer.createText();
image = captchaProducer.createImage(capStr); // image = captchaProducer.createImage(capStr);
image = captchaImage(capStr);
} }
redisCache.setCacheObject(verifyKey, code, Constants.CAPTCHA_EXPIRATION, TimeUnit.MINUTES); redisCache.setCacheObject(verifyKey, code, Constants.CAPTCHA_EXPIRATION, TimeUnit.MINUTES);
...@@ -91,4 +93,47 @@ public class CaptchaController ...@@ -91,4 +93,47 @@ public class CaptchaController
ajax.put("img", Base64.encode(os.toByteArray())); ajax.put("img", Base64.encode(os.toByteArray()));
return ajax; return ajax;
} }
private static final int WIDTH = 120; // 图片宽度
private static final int HEIGHT = 40; // 图片高度
private static final int CODE_LENGTH = 4; // 验证码长度
private BufferedImage captchaImage(String capStr) {
// 创建图片缓冲区
BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
// 获取画笔
Graphics graphics = image.getGraphics();
// 绘制背景
graphics.setColor(Color.WHITE);
graphics.fillRect(0, 0, WIDTH, HEIGHT);
// 绘制验证码
graphics.setFont(new Font("Arial", Font.BOLD, 28));
for (int i = 0; i < capStr.length(); i++) {
graphics.setColor(getRandomColor());
graphics.drawString(String.valueOf(capStr.charAt(i)), (i + 1) * (WIDTH / (CODE_LENGTH + 1)), 30);
}
// 添加干扰线
for (int i = 0; i < 3; i++) {
graphics.setColor(getRandomColor());
graphics.drawLine((int) (Math.random() * WIDTH), (int) (Math.random() * HEIGHT),
(int) (Math.random() * WIDTH), (int) (Math.random() * HEIGHT));
}
// 释放资源
graphics.dispose();
return image;
}
// 获取随机颜色
private static Color getRandomColor() {
int r = (int) (Math.random() * 256);
int g = (int) (Math.random() * 256);
int b = (int) (Math.random() * 256);
return new Color(r, g, b);
}
} }
...@@ -14,7 +14,7 @@ ruoyi: ...@@ -14,7 +14,7 @@ ruoyi:
# 获取ip地址开关 # 获取ip地址开关
addressEnabled: false addressEnabled: false
# 验证码类型 math 数组计算 char 字符验证 # 验证码类型 math 数组计算 char 字符验证
captchaType: math captchaType: char
# 开发环境配置 # 开发环境配置
server: server:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment