mirror of
https://gitee.com/kekingcn/file-online-preview.git
synced 2025-10-07 23:24:30 +08:00
重构验证码生成方法 (#481)
This commit is contained in:
@@ -1,18 +1,14 @@
|
||||
package cn.keking.utils;
|
||||
|
||||
import com.aspose.cad.Tuple;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
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
|
||||
@@ -25,11 +21,12 @@ public class CaptchaUtil {
|
||||
'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 验证码和验证码图片
|
||||
* 指定验证码、生成验证码图片。
|
||||
* @param captchaCode 验证码
|
||||
* @return 验证码图片
|
||||
*/
|
||||
public static Map<String, Object> generateCaptcha(String captchaCode) {
|
||||
public static BufferedImage generateCaptchaPic(final String captchaCode) {
|
||||
Assert.notNull(captchaCode, "captchaCode must not be null");
|
||||
// 定义图像buffer
|
||||
BufferedImage buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
|
||||
Graphics gd = buffImg.getGraphics();
|
||||
@@ -54,11 +51,6 @@ public class CaptchaUtil {
|
||||
}
|
||||
// randomCode用于保存随机产生的验证码,以便用户登录后进行验证。
|
||||
int red, green, blue;
|
||||
|
||||
if (ObjectUtils.isEmpty(captchaCode)) {
|
||||
captchaCode = generateCaptchaCode();
|
||||
}
|
||||
|
||||
// 产生随机的颜色分量来构造颜色值,这样输出的每位数字的颜色值都将不同。
|
||||
red = random.nextInt(255);
|
||||
green = random.nextInt(255);
|
||||
@@ -66,19 +58,14 @@ public class CaptchaUtil {
|
||||
// 用随机产生的颜色将验证码绘制到图像中。
|
||||
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 buffImg;
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成随机字符串。
|
||||
* @return 字符串
|
||||
*/
|
||||
private static String generateCaptchaCode() {
|
||||
public static String generateCaptchaCode() {
|
||||
Random random = new Random();
|
||||
StringBuilder randomCode = new StringBuilder();
|
||||
for (int i = 0; i < codeLength; i++) {
|
||||
|
@@ -293,6 +293,19 @@ public class WebUtils {
|
||||
return Long.parseLong(value);
|
||||
}
|
||||
|
||||
/**
|
||||
* session 中设置属性
|
||||
* @param request 请求
|
||||
* @param key 属性名
|
||||
*/
|
||||
public static void setSessionAttr(HttpServletRequest request, String key, Object value) {
|
||||
HttpSession session = request.getSession();
|
||||
if (session == null) {
|
||||
return;
|
||||
}
|
||||
session.setAttribute(key, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* 移除 session 中的属性
|
||||
* @param request 请求
|
||||
|
@@ -2,9 +2,9 @@ package cn.keking.web.controller;
|
||||
|
||||
import cn.keking.config.ConfigConstants;
|
||||
import cn.keking.model.ReturnResponse;
|
||||
import cn.keking.utils.CaptchaUtil;
|
||||
import cn.keking.utils.DateUtils;
|
||||
import cn.keking.utils.KkFileUtils;
|
||||
import cn.keking.utils.CaptchaUtil;
|
||||
import cn.keking.utils.RarUtils;
|
||||
import cn.keking.utils.WebUtils;
|
||||
import org.slf4j.Logger;
|
||||
@@ -22,16 +22,21 @@ import javax.imageio.ImageIO;
|
||||
import javax.servlet.ServletOutputStream;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.awt.image.RenderedImage;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
import static cn.keking.utils.CaptchaUtil.*;
|
||||
import static cn.keking.utils.CaptchaUtil.captcha_code;
|
||||
import static cn.keking.utils.CaptchaUtil.captcha_generate_time;
|
||||
|
||||
/**
|
||||
* @author yudian-it
|
||||
@@ -104,22 +109,19 @@ public class FileController {
|
||||
long captchaGenerateTime = WebUtils.getLongSessionAttr(request, captcha_generate_time);
|
||||
long timeDifference = DateUtils.calculateCurrentTimeDifference(captchaGenerateTime);
|
||||
|
||||
Map<String, Object> codeMap;
|
||||
|
||||
// 验证码为空,且生成验证码超过50秒,重新生成验证码
|
||||
if (timeDifference > 50 && ObjectUtils.isEmpty(captchaCode)) {
|
||||
codeMap = CaptchaUtil.generateCaptcha(null);
|
||||
captchaCode = CaptchaUtil.generateCaptchaCode();
|
||||
// 更新验证码
|
||||
request.getSession().setAttribute(captcha_code, codeMap.get(captcha_code).toString());
|
||||
request.getSession().setAttribute(captcha_generate_time, DateUtils.getCurrentSecond());
|
||||
WebUtils.setSessionAttr(request, captcha_code, captchaCode);
|
||||
WebUtils.setSessionAttr(request, captcha_generate_time, DateUtils.getCurrentSecond());
|
||||
} else {
|
||||
captchaCode = ObjectUtils.isEmpty(captchaCode) ? "wait" : captchaCode;
|
||||
codeMap = CaptchaUtil.generateCaptcha(captchaCode);
|
||||
}
|
||||
|
||||
ServletOutputStream sos = response.getOutputStream();
|
||||
ImageIO.write((RenderedImage) codeMap.get(captcha_code_pic), "jpeg", sos);
|
||||
sos.close();
|
||||
ServletOutputStream outputStream = response.getOutputStream();
|
||||
ImageIO.write(CaptchaUtil.generateCaptchaPic(captchaCode), "jpeg", outputStream);
|
||||
outputStream.close();
|
||||
}
|
||||
|
||||
@GetMapping("/listFiles")
|
||||
|
Reference in New Issue
Block a user