From 90496c0e35d9e0faa0be0ca4a4d99f1faae1d432 Mon Sep 17 00:00:00 2001 From: haotian <2421912570@qq.com> Date: Mon, 12 May 2025 15:18:49 +0800 Subject: [PATCH] =?UTF-8?q?=E7=9F=AD=E4=BF=A1=E9=AA=8C=E8=AF=81=E7=A0=81?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E9=94=99=E8=AF=AF=E5=B0=9D=E8=AF=95=E6=AC=A1?= =?UTF-8?q?=E6=95=B0,=E4=B8=89=E6=AC=A1=E5=A4=B1=E8=B4=A5=E5=90=8E?= =?UTF-8?q?=E9=9C=80=E8=A6=81=E9=87=8D=E6=96=B0=E8=8E=B7=E5=8F=96=E9=AA=8C?= =?UTF-8?q?=E8=AF=81=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/common/CaptchaController.java | 12 ++++++-- .../ruoyi/common/constant/CacheConstants.java | 5 ++++ .../user/CaptchaDisableException.java | 12 ++++++++ .../exception/user/CaptchaException.java | 1 + .../web/service/SysLoginService.java | 28 +++++++++++++++---- 5 files changed, 49 insertions(+), 9 deletions(-) create mode 100644 ruoyi-common/src/main/java/com/ruoyi/common/exception/user/CaptchaDisableException.java diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/common/CaptchaController.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/common/CaptchaController.java index d64575c6..2c78cd7d 100644 --- a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/common/CaptchaController.java +++ b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/common/CaptchaController.java @@ -193,15 +193,21 @@ public class CaptchaController { private void sendCacheCode(String phone) { try { String verifyKey = CacheConstants.CAPTCHA_CODE_KEY + phone; + + String countKey = CacheConstants.CAPTCHA_CODE_KEY_COUNT + phone; // 生成短信验证码 -// String testCode = "1234"; Random random = new Random(); - String testCode = String.format("%04d", random.nextInt(10000)); // 0000-9999 +// String testCode = String.format("%04d", random.nextInt(10000)); // 0000-9999 + String testCode = "1234"; // 0000-9999 // Constants.CAPTCHA_EXPIRATION 为验证码过期时间,这里是5 redisCache.setCacheObject(verifyKey, testCode, Constants.CAPTCHA_EXPIRATION, TimeUnit.MINUTES); + // 记录验证码尝试次数 + redisCache.setCacheObject(countKey, 0, Constants.CAPTCHA_EXPIRATION, TimeUnit.MINUTES); + // 发送验证码 - boolean flag = BaiduSMS.sendSMS(phone, testCode, "5"); +// boolean flag = BaiduSMS.sendSMS(phone, testCode, "5"); + boolean flag = true; if (flag) { log.info("发送短信验证码成功:"+phone); System.out.println("发送短信验证码成功"); diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/constant/CacheConstants.java b/ruoyi-common/src/main/java/com/ruoyi/common/constant/CacheConstants.java index c89692c2..21504350 100644 --- a/ruoyi-common/src/main/java/com/ruoyi/common/constant/CacheConstants.java +++ b/ruoyi-common/src/main/java/com/ruoyi/common/constant/CacheConstants.java @@ -17,6 +17,11 @@ public class CacheConstants */ public static final String CAPTCHA_CODE_KEY = "captcha_codes:"; + /** + * 验证码 尝试次数 + */ + public static final String CAPTCHA_CODE_KEY_COUNT = "captcha_codes_count:"; + /** * 参数管理 cache key */ diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/exception/user/CaptchaDisableException.java b/ruoyi-common/src/main/java/com/ruoyi/common/exception/user/CaptchaDisableException.java new file mode 100644 index 00000000..e07cb2e1 --- /dev/null +++ b/ruoyi-common/src/main/java/com/ruoyi/common/exception/user/CaptchaDisableException.java @@ -0,0 +1,12 @@ +package com.ruoyi.common.exception.user; + +public class CaptchaDisableException extends UserException +{ + private static final long serialVersionUID = 1L; + + public CaptchaDisableException() + { + super("user.jcaptcha.expire", null); + } + +} \ No newline at end of file diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/exception/user/CaptchaException.java b/ruoyi-common/src/main/java/com/ruoyi/common/exception/user/CaptchaException.java index e3334ae3..a9be76cd 100644 --- a/ruoyi-common/src/main/java/com/ruoyi/common/exception/user/CaptchaException.java +++ b/ruoyi-common/src/main/java/com/ruoyi/common/exception/user/CaptchaException.java @@ -13,4 +13,5 @@ public class CaptchaException extends UserException { super("user.jcaptcha.error", null); } + } diff --git a/ruoyi-framework/src/main/java/com/ruoyi/framework/web/service/SysLoginService.java b/ruoyi-framework/src/main/java/com/ruoyi/framework/web/service/SysLoginService.java index 06a65020..5b8b6b06 100644 --- a/ruoyi-framework/src/main/java/com/ruoyi/framework/web/service/SysLoginService.java +++ b/ruoyi-framework/src/main/java/com/ruoyi/framework/web/service/SysLoginService.java @@ -2,6 +2,7 @@ package com.ruoyi.framework.web.service; import javax.annotation.Resource; +import com.ruoyi.common.exception.user.*; import com.ruoyi.framework.security.authentication.SmsCodeAuthenticationToken; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.authentication.AuthenticationManager; @@ -16,11 +17,6 @@ import com.ruoyi.common.core.domain.entity.SysUser; import com.ruoyi.common.core.domain.model.LoginUser; import com.ruoyi.common.core.redis.RedisCache; import com.ruoyi.common.exception.ServiceException; -import com.ruoyi.common.exception.user.BlackListException; -import com.ruoyi.common.exception.user.CaptchaException; -import com.ruoyi.common.exception.user.CaptchaExpireException; -import com.ruoyi.common.exception.user.UserNotExistsException; -import com.ruoyi.common.exception.user.UserPasswordNotMatchException; import com.ruoyi.common.utils.DateUtils; import com.ruoyi.common.utils.MessageUtils; import com.ruoyi.common.utils.StringUtils; @@ -397,15 +393,35 @@ public class SysLoginService if (captchaEnabled) { String verifyKey = CacheConstants.CAPTCHA_CODE_KEY + phone; String captcha = redisCache.getCacheObject(verifyKey); - redisCache.deleteObject(verifyKey); + + // 尝试次数 + String countKey = CacheConstants.CAPTCHA_CODE_KEY_COUNT + phone; + int captchaCount = redisCache.getCacheObject(countKey); + + if(captchaCount >= 3){ + // 删除缓存 + redisCache.deleteObject(verifyKey); + + // 验证码失效 + AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.jcaptcha.expire"))); + throw new CaptchaDisableException(); + } + if (captcha == null) { AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.jcaptcha.expire"))); + throw new CaptchaDisableException(); } if (!code.equalsIgnoreCase(captcha)) { + // 尝试次数加1 + redisCache.setCacheObject(countKey, captchaCount + 1); AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.jcaptcha.error"))); throw new CaptchaException(); } + else{ + // 登录成功删除验证码 + redisCache.deleteObject(verifyKey); + } } }