From 32b80e0d6cc94ab2da5a55e9b00614844fa3145c Mon Sep 17 00:00:00 2001 From: mashuai Date: Mon, 27 May 2024 15:52:20 +0800 Subject: [PATCH] =?UTF-8?q?=E6=89=8B=E6=9C=BA=E5=8F=B7=E6=AC=A1=E6=95=B0?= =?UTF-8?q?=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 | 31 ++++++++++++++++--- 3 files changed, 39 insertions(+), 5 deletions(-) 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 f9f9f5ca..891e67d0 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 @@ -89,6 +89,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 5d44e70e..b8b52fd0 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 @@ -84,18 +84,43 @@ public class SysSmsServiceImpl implements ISysSmsService { public AjaxResult codeLogin(String phone) { try { + // 检查发送次数是否超限 + if (isOverLimit(phone)) { + throw new ServiceException("当天此手机号发送验证码次数超过限制", 1001); + } //获取六位验证码 String code = getSixBitCode(); //调用发送短信的方法 String body = sendMessageNew(phone,tencentSmsConfig.getTemplateId().get(1),code); // 存储验证码至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; + } + /** * 发送消息msg到手机 @@ -177,10 +202,6 @@ public class SysSmsServiceImpl implements ISysSmsService { } } - public static void main(String[] args) { - System.out.println(getSixBitCode()); - } - /** * 随机生成6位验证码 */