feat: implement general captcha code for consistent rendering and caching

This commit is contained in:
2026-07-22 10:23:22 +08:00
parent 45d8494a40
commit b48af3d797
7 changed files with 293 additions and 12 deletions

View File

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