From dd31e090090ccf2e40580fa8816a81505cb383c4 Mon Sep 17 00:00:00 2001 From: mashuai Date: Mon, 27 May 2024 18:42:32 +0800 Subject: [PATCH] =?UTF-8?q?=E6=89=8B=E6=9C=BA=E5=8F=B7=E9=AA=8C=E8=AF=81?= =?UTF-8?q?=E7=A0=81=E6=AC=A1=E6=95=B0=E9=99=90=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../sgzb/auth/controller/TokenController.java | 8 ++++++ .../common/core/constant/UserConstants.java | 5 ++++ .../service/impl/SysSmsServiceImpl.java | 27 ++++++++++++++++++- 3 files changed, 39 insertions(+), 1 deletion(-) diff --git a/sgzb-auth/src/main/java/com/bonus/sgzb/auth/controller/TokenController.java b/sgzb-auth/src/main/java/com/bonus/sgzb/auth/controller/TokenController.java index 3f3c322c..fc8325c0 100644 --- a/sgzb-auth/src/main/java/com/bonus/sgzb/auth/controller/TokenController.java +++ b/sgzb-auth/src/main/java/com/bonus/sgzb/auth/controller/TokenController.java @@ -94,6 +94,14 @@ public class TokenController { @PostMapping("sendCode") public R sendCode(@RequestBody LoginBody form) { + String uuid = form.getUuid(); + String captcha = redisService.getCacheObject(CacheConstants.CAPTCHA_CODE_KEY + uuid).toString(); + if (StringUtils.isBlank(captcha)) { + return R.fail("验证码超时,请重新刷新"); + } + if (form.getCode() != null && !form.getCode().equals(captcha)) { + return R.fail("验证码错误"); + } R sendState = remoteUserService.sendCode(form.getPhone()); return sendState; } diff --git a/sgzb-common/sgzb-common-core/src/main/java/com/bonus/sgzb/common/core/constant/UserConstants.java b/sgzb-common/sgzb-common-core/src/main/java/com/bonus/sgzb/common/core/constant/UserConstants.java index 6372b7a8..1efbd51d 100644 --- a/sgzb-common/sgzb-common-core/src/main/java/com/bonus/sgzb/common/core/constant/UserConstants.java +++ b/sgzb-common/sgzb-common-core/src/main/java/com/bonus/sgzb/common/core/constant/UserConstants.java @@ -87,4 +87,9 @@ public class UserConstants * 手机号长度限制 */ public static final int PHONE_DEFAULT_LENGTH_LOGIN = 11; + + /** + * 手机1天限制 + */ + public static final String MOBILE_PHONE_1D_LIMIT_DIR = "mobilePhoneDay:"; } diff --git a/sgzb-modules/sgzb-system/src/main/java/com/bonus/sgzb/system/service/impl/SysSmsServiceImpl.java b/sgzb-modules/sgzb-system/src/main/java/com/bonus/sgzb/system/service/impl/SysSmsServiceImpl.java index afe1cd07..8a76cda2 100644 --- a/sgzb-modules/sgzb-system/src/main/java/com/bonus/sgzb/system/service/impl/SysSmsServiceImpl.java +++ b/sgzb-modules/sgzb-system/src/main/java/com/bonus/sgzb/system/service/impl/SysSmsServiceImpl.java @@ -136,6 +136,10 @@ public class SysSmsServiceImpl implements ISysSmsService { if (phone == null || phone.length() != UserConstants.PHONE_DEFAULT_LENGTH_LOGIN) { return AjaxResult.error("手机号格式错误,请输入11位数字号码"); } + // 检查发送次数是否超限 + if (isOverLimit(phone)) { + throw new ServiceException("当天此手机号发送验证码次数超过限制", 1001); + } String code = getSixBitCode(); // 校验验证码 if (code.length() != UserConstants.CODE_MIN_LENGTH_LOGIN) { @@ -151,12 +155,33 @@ public class SysSmsServiceImpl implements ISysSmsService { } // 存储验证码至Redis中,键值为:code_15588886157 , 有效期5,时间颗粒度为MINUTES:分钟 redisService.setCacheObject("code_" + phone, code, 5L, TimeUnit.MINUTES); - return success("手机号:" + phone + ",用户登录验证码:" + code + ",返回结果:" + body); + String key = UserConstants.MOBILE_PHONE_1D_LIMIT_DIR + phone; + Integer sendCount = redisService.getCacheObject(key); + if (sendCount == null) { + sendCount = 1; + } else { + sendCount++; + } + //存储一天手机号发送验证码数据 + redisService.setCacheObject(key, sendCount, 1L, TimeUnit.DAYS); + return success("手机号:" + phone + ",短信验证码发送成功 !"); } catch (Exception e) { return AjaxResult.error("发送失败:" + e.getMessage()); } } + /** + * 检查发送次数是否超过限制 + * @param phone 手机号 + * @return + */ + private boolean isOverLimit(String phone) { + String key = UserConstants.MOBILE_PHONE_1D_LIMIT_DIR + phone; + Integer sendCount = redisService.getCacheObject(key); + // 限制每个手机号每天最多发送10次 + return sendCount != null && sendCount >= 10; + } + /** * 判断验证码是否存在Redis中