1
This commit is contained in:
112
auth/src/main/java/com/example/springboot4/util/CaptchaUtil.java
Normal file
112
auth/src/main/java/com/example/springboot4/util/CaptchaUtil.java
Normal file
@@ -0,0 +1,112 @@
|
||||
package com.example.springboot4.util;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import java.awt.*;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.Base64;
|
||||
import java.util.Random;
|
||||
|
||||
public class CaptchaUtil {
|
||||
|
||||
private static final char[] CHARS = {'2', '3', '4', '5', '6', '7', '8', '9',
|
||||
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M',
|
||||
'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
|
||||
|
||||
private static final int SIZE = 4;
|
||||
private static final int LINES = 5;
|
||||
private static final int WIDTH = 120;
|
||||
private static final int HEIGHT = 40;
|
||||
|
||||
/**
|
||||
* 生成验证码图片和文本
|
||||
*/
|
||||
public static Captcha generateCaptcha() {
|
||||
BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
|
||||
Graphics g = image.getGraphics();
|
||||
|
||||
// 设置背景色
|
||||
g.setColor(Color.WHITE);
|
||||
g.fillRect(0, 0, WIDTH, HEIGHT);
|
||||
|
||||
// 生成随机验证码
|
||||
Random random = new Random();
|
||||
StringBuilder captchaText = new StringBuilder();
|
||||
for (int i = 0; i < SIZE; i++) {
|
||||
char c = CHARS[random.nextInt(CHARS.length)];
|
||||
captchaText.append(c);
|
||||
}
|
||||
|
||||
// 绘制验证码文本
|
||||
// 使用系统默认字体替代指定字体
|
||||
// g.setFont(new Font("Arial", Font.BOLD, 24));
|
||||
Font font = new Font(null, Font.BOLD, 24);
|
||||
g.setFont(font);
|
||||
|
||||
for (int i = 0; i < captchaText.length(); i++) {
|
||||
g.setColor(getRandomColor());
|
||||
g.drawString(String.valueOf(captchaText.charAt(i)), 20 + i * 20, 30);
|
||||
}
|
||||
|
||||
// 绘制干扰线
|
||||
for (int i = 0; i < LINES; i++) {
|
||||
g.setColor(getRandomColor());
|
||||
int x1 = random.nextInt(WIDTH);
|
||||
int y1 = random.nextInt(HEIGHT);
|
||||
int x2 = random.nextInt(WIDTH);
|
||||
int y2 = random.nextInt(HEIGHT);
|
||||
g.drawLine(x1, y1, x2, y2);
|
||||
}
|
||||
|
||||
g.dispose();
|
||||
|
||||
// 将图片转换为Base64编码
|
||||
String base64Image = imageToBase64(image);
|
||||
|
||||
return new Captcha(captchaText.toString(), base64Image);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取随机颜色
|
||||
*/
|
||||
private static Color getRandomColor() {
|
||||
Random random = new Random();
|
||||
return new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256));
|
||||
}
|
||||
|
||||
/**
|
||||
* 将BufferedImage转换为Base64编码
|
||||
*/
|
||||
private static String imageToBase64(BufferedImage image) {
|
||||
try {
|
||||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
||||
ImageIO.write(image, "png", outputStream);
|
||||
byte[] imageBytes = outputStream.toByteArray();
|
||||
return Base64.getEncoder().encodeToString(imageBytes);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException("Failed to convert image to Base64", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证码数据类
|
||||
*/
|
||||
public static class Captcha {
|
||||
private final String text;
|
||||
private final String imageBase64;
|
||||
|
||||
public Captcha(String text, String imageBase64) {
|
||||
this.text = text;
|
||||
this.imageBase64 = imageBase64;
|
||||
}
|
||||
|
||||
public String getText() {
|
||||
return text;
|
||||
}
|
||||
|
||||
public String getImageBase64() {
|
||||
return imageBase64;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package com.example.springboot4.util;
|
||||
|
||||
|
||||
import org.springframework.util.ResourceUtils;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.security.PrivateKey;
|
||||
import java.security.PublicKey;
|
||||
|
||||
/**
|
||||
* RSA密钥加载器
|
||||
* 用于从本地文件加载RSA密钥对
|
||||
*/
|
||||
public class RsaKeyLoader {
|
||||
|
||||
/**
|
||||
* 从指定路径加载私钥
|
||||
*
|
||||
* @param privateKeyPath 私钥文件路径
|
||||
* @return PrivateKey 私钥对象
|
||||
* @throws IOException IO异常
|
||||
* @throws ClassNotFoundException 类未找到异常
|
||||
*/
|
||||
public static PrivateKey loadPrivateKey(String privateKeyPath) throws IOException, ClassNotFoundException {
|
||||
try (ObjectInputStream ois = new ObjectInputStream(Files.newInputStream(Path.of(privateKeyPath)))) {
|
||||
return (PrivateKey) ois.readObject();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 从指定路径加载公钥
|
||||
*
|
||||
* @param publicKeyPath 公钥文件路径
|
||||
* @return PublicKey 公钥对象
|
||||
* @throws IOException IO异常
|
||||
* @throws ClassNotFoundException 类未找到异常
|
||||
*/
|
||||
public static PublicKey loadPublicKey(String publicKeyPath) throws IOException, ClassNotFoundException {
|
||||
try (ObjectInputStream ois = new ObjectInputStream(Files.newInputStream(Path.of(publicKeyPath)))) {
|
||||
return (PublicKey) ois.readObject();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user