Эх сурвалжийг харах

improvement 验证码改为四位数的数字验证

chenchen 4 сар өмнө
parent
commit
29f55945f3

+ 10 - 6
application-webadmin/src/main/java/com/tourism/webadmin/app/website/controller/LoginToWebsiteController.java

@@ -59,6 +59,7 @@ import jakarta.servlet.http.HttpServletResponse;
 import jakarta.validation.Valid;
 import lombok.extern.slf4j.Slf4j;
 import me.chanjar.weixin.common.error.WxErrorException;
+import org.apache.commons.lang3.RandomUtils;
 import org.apache.commons.lang3.StringUtils;
 import org.redisson.api.RBucket;
 import org.redisson.api.RedissonClient;
@@ -211,17 +212,20 @@ public class LoginToWebsiteController {
         String capStr = null, code = null;
         BufferedImage image = null;
 
-        // 生成验证码
-        String capText = captchaProducerMath.createText();
-        capStr = capText.substring(0, capText.lastIndexOf("@"));
-        code = capText.substring(capText.lastIndexOf("@") + 1);
-        image = captchaProducerMath.createImage(capStr);
+        // 生成数字验证码(乘除)
+//        String capText = captchaProducerMath.createText();
+//        capStr = capText.substring(0, capText.lastIndexOf("@"));
+//        code = capText.substring(capText.lastIndexOf("@") + 1);
+//        image = captchaProducerMath.createImage(capStr);
+
+        int randomNumber = RandomUtils.nextInt(1000, 9999);
+        image = captchaProducerMath.createImage(String.valueOf(randomNumber));
 
         // 获取 RBucket 对象
         RBucket<String> bucket = redissonClient.getBucket(verifyKey);
 
         // 设置缓存对象并设置过期时间
-        bucket.set(code, Constants.CAPTCHA_EXPIRATION, TimeUnit.MINUTES);
+        bucket.set(String.valueOf(randomNumber), Constants.CAPTCHA_EXPIRATION, TimeUnit.MINUTES);
 
 //        redisCache.setCacheObject(verifyKey, code, Constants.CAPTCHA_EXPIRATION, TimeUnit.MINUTES);
         // 转换流信息写出