rename com.imyeyu.server to com.imyeyu.api

This commit is contained in:
Timi
2025-07-22 15:26:14 +08:00
parent e816b885b2
commit 323e038e86
340 changed files with 1174 additions and 1175 deletions
@@ -0,0 +1,187 @@
package com.imyeyu.api.util;
import com.imyeyu.java.TimiJava;
import com.imyeyu.java.bean.timi.TimiCode;
import com.imyeyu.java.bean.timi.TimiException;
import com.imyeyu.api.bean.CaptchaFrom;
import com.imyeyu.spring.TimiSpring;
import com.imyeyu.utils.Calc;
import com.imyeyu.utils.Time;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.geom.QuadCurve2D;
import java.awt.image.BufferedImage;
/**
* 验证码绘制
*
* @author 夜雨
* @version 2021-05-20 16:48
*/
@Slf4j
@Component
public class CaptchaManager {
/** 会话频率限制时间,毫秒 */
private static final int LOCK_TIME = 1000;
/** 会话频率限制键,插值会话 ID */
private static final String LOCK_KEY = "CAPTCHA:LOCK:%s";
/** 会话值缓存键,插值验证码来源 */
private static final String CACHE_KEY = "CAPTCHA:%s";
/** 绘制字符,移除 O,o,I,l */
private static final char[] CHARS = {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'm', 'n', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
};
@Value("${spring.profiles.active}")
private String env;
public BufferedImage generate(CaptchaFrom from, int width, int height) throws InterruptedException {
return generate(from, 4, width, height);
}
/**
* 生成并缓存验证码
*
* @param from 来自模块
* @param width 宽度
* @param height 高度
* @return 图片流
*/
public BufferedImage generate(CaptchaFrom from, int length, int width, int height) throws InterruptedException {
// 加锁
String lockKey = LOCK_KEY.formatted(TimiSpring.getSession().getId());
Long lockAt = TimiSpring.getSessionAttr(lockKey, Long.class);
TimiSpring.setSessionAttr(lockKey, Time.now());
if (lockAt != null) {
long diff = Time.now() - lockAt;
if (diff < LOCK_TIME) {
// 限制频率
synchronized (this) {
wait(LOCK_TIME - diff);
}
}
}
int fontWidth = width / 4;
int fontSize = (int) (height * .8);
int yOffset = (int) (height * .2);
StringBuilder value = new StringBuilder();
// 图片流
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g = (Graphics2D) image.getGraphics();
// 渐变背景
GradientPaint gradient = new GradientPaint(
0, 0,
new Color(Calc.random(200, 255), Calc.random(200, 255), Calc.random(200, 255)),
width,
height,
new Color(Calc.random(200, 255), Calc.random(200, 255), Calc.random(200, 255))
);
g.setPaint(gradient);
g.fillRect(0, 0, width, height);
// 半透明干扰层
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.4F));
for (int i = 0; i < 15; i++) {
g.setColor(new Color(Calc.random(0, 150), Calc.random(0, 150), Calc.random(0, 150)));
g.drawString(String.valueOf(CHARS[Calc.random(0, CHARS.length - 1)]), Calc.random(0, width), Calc.random(0, height));
}
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
// 绘制验证码字符
g.setFont(new Font("Fixedsys", Font.BOLD, Calc.random(fontSize - 2, fontSize + 2)));
for (int i = 0; i < length; i++) {
char c = CHARS[Calc.random(0, CHARS.length - 1)];
value.append(c);
// 颜色
g.setColor(new Color(Calc.random(50, 150), Calc.random(50, 150), Calc.random(50, 150)));
// 位置
int x = fontWidth * i + Calc.random(0, 10);
int y = height - yOffset + Calc.random(-4, 4);
// 随机角度
AffineTransform original = g.getTransform();
AffineTransform rotate = AffineTransform.getRotateInstance(Math.toRadians(Calc.random(0, 15)), x + fontSize / 2D, y - fontSize / 2D);
g.setTransform(rotate);
g.drawString(String.valueOf(c), x, y);
g.setTransform(original);
}
// 干扰线
for (int i = 0; i < height / 2; i++) {
int x1 = Calc.random(0, width);
int y1 = Calc.random(0, height);
int x2 = x1 + Calc.random(-20, 20);
int y2 = y1 + Calc.random(-20, 20);
g.setColor(new Color(Calc.random(100, 150), Calc.random(100, 150), Calc.random(100, 150)));
if (Calc.randomBoolean()) {
g.drawLine(x1, y1, x2, y2);
} else {
QuadCurve2D curve = new QuadCurve2D.Float();
double ctrlX = (x1 + x2) / 2D + Calc.random(-15, 15);
double ctrlY = (y1 + y2) / 2D + Calc.random(-15, 15);
curve.setCurve(x1, y1, ctrlX, ctrlY, x2, y2);
g.draw(curve);
}
}
// 写入缓存
TimiSpring.setSessionAttr(CACHE_KEY.formatted(from), value.toString());
return image;
}
/** @return 错误回调图像 */
public BufferedImage error(TimiCode code) {
final int width = 74, height = 24;
BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g = (Graphics2D) img.getGraphics();
g.setColor(Color.RED);
// 文本
g.setFont(new Font("Fixedsys", Font.BOLD, 13));
g.drawString("ERR." + code.getValue(), 2, 18);
g.setColor(Color.DARK_GRAY);
return img;
}
/**
* <p>验证。通过验证不抛出任何异常也不返回任何内容,否则抛出相应异常
* <p>会验证非空和期限
* <p>测试环境总是通过验证
*
* @param captcha 提交的验证码
* @param from 来自模块
* @throws TimiException 验证异常
*/
public void test(String captcha, String from) {
if (TimiJava.isEmpty(captcha)) {
throw new TimiException(TimiCode.ARG_MISS).msgKey("captcha.miss");
}
if (env.startsWith("dev")) {
return;
}
// Session 验证
String sessionCaptcha = TimiSpring.getSessionAttrAsString(CACHE_KEY.formatted(from));
if (TimiJava.isEmpty(sessionCaptcha)) {
throw new TimiException(TimiCode.ARG_EXPIRED).msgKey("captcha.expire");
}
if (!captcha.trim().equalsIgnoreCase(sessionCaptcha)) {
throw new TimiException(TimiCode.RESULT_BAD).msgKey("captcha.error");
}
// 清除缓存
TimiSpring.removeSessionAttr(CACHE_KEY.formatted(from));
}
}