This commit is contained in:
雷欧(林平凡)
2025-06-16 16:36:25 +08:00
parent 98204bb9f9
commit 2e4128ab40
22 changed files with 757 additions and 38 deletions

View File

@@ -0,0 +1,32 @@
package cn.van.business.service;
import com.google.code.kaptcha.Producer;
import org.springframework.stereotype.Service;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.util.Base64;
@Service
public class CaptchaService {
private final Producer kaptchaProducer;
public CaptchaService(Producer kaptchaProducer) {
this.kaptchaProducer = kaptchaProducer;
}
public String generateCaptchaImage() throws Exception {
String text = kaptchaProducer.createText();
BufferedImage image = kaptchaProducer.createImage(text);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ImageIO.write(image, "PNG", outputStream);
return Base64.getEncoder().encodeToString(outputStream.toByteArray());
}
public boolean validateCaptcha(String userInput, String generatedCaptcha) {
return userInput.equalsIgnoreCase(generatedCaptcha);
}
}