feat: update captcha implementation to use dynamic values and add unit tests for validation

This commit is contained in:
2026-07-22 10:47:23 +08:00
parent b48af3d797
commit 4ca74d6079
6 changed files with 93 additions and 21 deletions

View File

@@ -28,9 +28,6 @@ import com.ruoyi.system.service.ISysConfigService;
@RestController
public class CaptchaController
{
/** 通用图形验证码,图像展示和缓存校验均使用该值。 */
private static final String GENERAL_CAPTCHA_CODE = "820820";
@Resource(name = "captchaProducer")
private Producer captchaProducer;
@@ -60,19 +57,25 @@ public class CaptchaController
String uuid = IdUtils.simpleUUID();
String verifyKey = CacheConstants.CAPTCHA_CODE_KEY + uuid;
// 图形验证码统一展示并校验固定值UUID、有效期和一次性使用机制保持不变。
String capStr = null, code = null;
BufferedImage image = null;
// 生成验证码
String captchaType = RuoYiConfig.getCaptchaType();
BufferedImage image;
if ("math".equals(captchaType))
{
image = captchaProducerMath.createImage(GENERAL_CAPTCHA_CODE);
String capText = captchaProducerMath.createText();
capStr = capText.substring(0, capText.lastIndexOf("@"));
code = capText.substring(capText.lastIndexOf("@") + 1);
image = captchaProducerMath.createImage(capStr);
}
else
else if ("char".equals(captchaType))
{
image = captchaProducer.createImage(GENERAL_CAPTCHA_CODE);
capStr = code = captchaProducer.createText();
image = captchaProducer.createImage(capStr);
}
redisCache.setCacheObject(verifyKey, GENERAL_CAPTCHA_CODE, Constants.CAPTCHA_EXPIRATION, TimeUnit.MINUTES);
redisCache.setCacheObject(verifyKey, code, Constants.CAPTCHA_EXPIRATION, TimeUnit.MINUTES);
// 转换流信息写出
FastByteArrayOutputStream os = new FastByteArrayOutputStream();
try