重构验证码删除文件的实现逻辑 (#479)

* 重构验证码删除文件的实现逻辑

* 移除未使用的依赖

* 微调描述信息
This commit is contained in:
kl
2023-08-11 16:39:07 +08:00
committed by GitHub
parent aa49cc6ac0
commit 0a8be8ac95
7 changed files with 230 additions and 213 deletions

View File

@@ -0,0 +1,89 @@
package cn.keking.utils;
import com.aspose.cad.Tuple;
import org.springframework.util.ObjectUtils;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
public class CaptchaUtil {
public static final String captcha_code = "captchaCode";
public static final String captcha_code_pic = "captchaCodePic";
public static final String captcha_generate_time = "captchaTime";
private static final int width = 100;// 定义图片的width
private static final int height = 30;// 定义图片的height
private static final int codeLength = 4;// 定义图片上显示验证码的个数
private static final int xx = 18;
private static final int fontHeight = 28;
private static final int codeY = 27;
private static final char[] codeSequence = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'M', 'N', 'P', 'Q', 'R', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'j', 'k', 'm', 'n', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', '2', '3', '4', '5', '6', '7', '8', '9'};
/**
* 指定验证码、或生成验证码。
* @param captchaCode 指定验证码, 如果为 null则生成验证码。否则使用指定的验证码。
* @return 验证码和验证码图片
*/
public static Map<String, Object> generateCaptcha(String captchaCode) {
// 定义图像buffer
BufferedImage buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics gd = buffImg.getGraphics();
Random random = new Random();
// 将图像填充为白色
gd.setColor(Color.WHITE);
gd.fillRect(0, 0, width, height);
Font font = new Font("Times New Roman", Font.BOLD, fontHeight);
gd.setFont(font);
// 画边框。
gd.setColor(Color.BLACK);
gd.drawRect(0, 0, width - 1, height - 1);
// 随机产生40条干扰线使图象中的认证码不易被其它程序探测到。
gd.setColor(Color.BLACK);
for (int i = 0; i < 30; i++) {
int x = random.nextInt(width);
int y = random.nextInt(height);
int xl = random.nextInt(12);
int yl = random.nextInt(12);
gd.drawLine(x, y, x + xl, y + yl);
}
// randomCode用于保存随机产生的验证码以便用户登录后进行验证。
int red, green, blue;
if (ObjectUtils.isEmpty(captchaCode)) {
captchaCode = generateCaptchaCode();
}
// 产生随机的颜色分量来构造颜色值,这样输出的每位数字的颜色值都将不同。
red = random.nextInt(255);
green = random.nextInt(255);
blue = random.nextInt(255);
// 用随机产生的颜色将验证码绘制到图像中。
gd.setColor(new Color(red, green, blue));
gd.drawString(captchaCode, 18, codeY);
Map<String, Object> map = new HashMap<>();
map.put(captcha_code, captchaCode);
//存放生成的验证码BufferedImage对象
map.put(captcha_code_pic, buffImg);
return map;
}
/**
* 生成随机字符串。
* @return 字符串
*/
private static String generateCaptchaCode() {
Random random = new Random();
StringBuilder randomCode = new StringBuilder();
for (int i = 0; i < codeLength; i++) {
randomCode.append(codeSequence[random.nextInt(52)]);
}
return randomCode.toString();
}
}

View File

@@ -0,0 +1,26 @@
package cn.keking.utils;
import java.time.Instant;
/**
* @author kl (http://kailing.pub)
* @since 2023/8/11
*/
public class DateUtils {
/**
* 获取当前时间的秒级时间戳
* @return
*/
public static long getCurrentSecond() {
return Instant.now().getEpochSecond();
}
/**
* 计算当前时间与指定时间的秒级时间戳差值
* @param datetime 指定时间
* @return 差值
*/
public static long calculateCurrentTimeDifference(long datetime) {
return getCurrentSecond() - datetime;
}
}

View File

@@ -1,90 +0,0 @@
package cn.keking.utils;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
public class RandomValidateCodeUtil {
private static final int width = 100;// 定义图片的width
private static final int height = 30;// 定义图片的height
private static final int codeCount = 4;// 定义图片上显示验证码的个数
private static final int xx = 18;
private static final int fontHeight = 28;
private static final int codeY = 27;
private static final char[] codeSequence = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'M', 'N', 'P', 'Q', 'R','T', 'U', 'V', 'W', 'X', 'Y', 'Z',
'a','b','c','d','e','f','g','h','j','k','m','n','p','q','r','s','t','u','v','w','x','y', '2', '3', '4','5', '6', '7', '8', '9' };
/**
* 生成一个map集合
* code为生成的验证码
* codePic为生成的验证码BufferedImage对象
*/
public static Map<String,Object> generateCodeAndPic(String ip, String sessionCode, int lx) {
// 定义图像buffer
BufferedImage buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
// Graphics2D gd = buffImg.createGraphics();
// Graphics2D gd = (Graphics2D) buffImg.getGraphics();
Graphics gd = buffImg.getGraphics();
// 创建一个随机数生成器类
Random random = new Random();
// 将图像填充为白色
gd.setColor(Color.WHITE);
gd.fillRect(0, 0, width, height);
// 创建字体,字体的大小应该根据图片的高度来定。
Font font = new Font("Times New Roman", Font.BOLD, fontHeight);
// 设置字体。
gd.setFont(font);
// 画边框。
gd.setColor(Color.BLACK);
gd.drawRect(0, 0, width - 1, height - 1);
// 随机产生40条干扰线使图象中的认证码不易被其它程序探测到。
gd.setColor(Color.BLACK);
for (int i = 0; i < 30; i++) {
int x = random.nextInt(width);
int y = random.nextInt(height);
int xl = random.nextInt(12);
int yl = random.nextInt(12);
gd.drawLine(x, y, x + xl, y + yl);
}
StringBuffer randomCode = new StringBuffer();
Map<String,Object> map = new HashMap<>();
// randomCode用于保存随机产生的验证码以便用户登录后进行验证。
int red, green, blue;
if (lx ==1){
// 产生随机的颜色分量来构造颜色值,这样输出的每位数字的颜色值都将不同。
red = random.nextInt(255);
green = random.nextInt(255);
blue = random.nextInt(255);
// 用随机产生的颜色将验证码绘制到图像中。
gd.setColor(new Color(red, green, blue));
gd.drawString(sessionCode, 18, codeY);
randomCode.append(sessionCode);
}else {
// 随机产生codeCount数字的验证码。
for (int i = 0; i < codeCount; i++) {
// 得到随机产生的验证码数字。
String code = String.valueOf(codeSequence[random.nextInt(52)]);
// 产生随机的颜色分量来构造颜色值,这样输出的每位数字的颜色值都将不同。
red = random.nextInt(255);
green = random.nextInt(255);
blue = random.nextInt(255);
// 用随机产生的颜色将验证码绘制到图像中。
gd.setColor(new Color(red, green, blue));
gd.drawString(code, (i + 1) * xx, codeY);
// 将产生的四个随机数组合在一起。
randomCode.append(code);
}
}
//存放验证码
map.put("code", randomCode);
//存放生成的验证码BufferedImage对象
map.put("codePic", buffImg);
return map;
}
}

View File

@@ -9,6 +9,8 @@ import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.util.HtmlUtils;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
@@ -27,7 +29,7 @@ import java.util.regex.Pattern;
public class WebUtils {
private static final Logger LOGGER = LoggerFactory.getLogger(WebUtils.class);
private static final String BASE64_MSG = "base64";
private static final String BASE64_MSG = "base64";
/**
* 获取标准的URL
*
@@ -205,13 +207,7 @@ public class WebUtils {
Matcher matcher = pattern.matcher(url);
return matcher.find();
}
public static boolean kuayu(String host, String wjl) { //查询域名是否相同
if (wjl.contains(host)) {
return true;
}else {
return false;
}
}
/**
* 将 Base64 字符串解码再解码URL参数, 默认使用 UTF-8
* @param source 原始 Base64 字符串
@@ -265,4 +261,48 @@ public class WebUtils {
}
return null;
}
/**
* 获取 session 中的 String 属性
* @param request 请求
* @return 属性值
*/
public static String getSessionAttr(HttpServletRequest request, String key) {
HttpSession session = request.getSession();
if (session == null) {
return null;
}
Object value = session.getAttribute(key);
if (value == null) {
return null;
}
return value.toString();
}
/**
* 获取 session 中的 long 属性
* @param request 请求
* @param key 属性名
* @return 属性值
*/
public static long getLongSessionAttr(HttpServletRequest request, String key) {
String value = getSessionAttr(request, key);
if (value == null) {
return 0;
}
return Long.parseLong(value);
}
/**
* 移除 session 中的属性
* @param request 请求
* @param key 属性名
*/
public static void removeSessionAttr(HttpServletRequest request, String key) {
HttpSession session = request.getSession();
if (session == null) {
return;
}
session.removeAttribute(key);
}
}