diff --git a/jeecg-boot/jeecg-boot-base-core/src/main/java/org/jeecg/common/util/DySmsLimit.java b/jeecg-boot/jeecg-boot-base-core/src/main/java/org/jeecg/common/util/DySmsLimit.java index b7a24c8a9..055ff59be 100644 --- a/jeecg-boot/jeecg-boot-base-core/src/main/java/org/jeecg/common/util/DySmsLimit.java +++ b/jeecg-boot/jeecg-boot-base-core/src/main/java/org/jeecg/common/util/DySmsLimit.java @@ -30,6 +30,46 @@ public class DySmsLimit { private static ConcurrentHashMap ipRequestCount = new ConcurrentHashMap<>(); private static ConcurrentHashMap ipBlacklist = new ConcurrentHashMap<>(); + // 同一手机号限流:60秒内最多1条,1小时内最多10条(基于Redis,集群共享) + private static final String PHONE_LIMIT_KEY_PRE = "sys:sms:limit:phone:"; + private static final int PHONE_MINUTE_SECONDS = 60; + private static final int PHONE_HOUR_SECONDS = 3600; + private static final int MAX_MESSAGE_PER_HOUR_PER_PHONE = 10; + + /** + * 按接收短信的手机号限流(不依赖客户端IP):同一手机号60秒内最多1条,1小时内最多10条 + * + * @param phone 接收短信的手机号 + * @return 允许发送返回true + * @author scott + * @since 2026-10-13 【安全 GHSA-5qr3-c98m-9767】增加按手机号限流 + */ + public static boolean canSendSmsToPhone(String phone) { + if (oConvertUtils.isEmpty(phone)) { + return true; + } + RedisUtil redisUtil = SpringContextUtils.getBean(RedisUtil.class); + String minuteKey = PHONE_LIMIT_KEY_PRE + "min:" + phone; + long minuteCount = redisUtil.incr(minuteKey, 1); + if (minuteCount == 1) { + redisUtil.expire(minuteKey, PHONE_MINUTE_SECONDS); + } + if (minuteCount > 1) { + log.warn("手机号:{},{}秒内已发送过短信,请稍后再试", phone, PHONE_MINUTE_SECONDS); + return false; + } + String hourKey = PHONE_LIMIT_KEY_PRE + "hour:" + phone; + long hourCount = redisUtil.incr(hourKey, 1); + if (hourCount == 1) { + redisUtil.expire(hourKey, PHONE_HOUR_SECONDS); + } + if (hourCount > MAX_MESSAGE_PER_HOUR_PER_PHONE) { + log.warn("手机号:{},1小时内短信发送超过{}条", phone, MAX_MESSAGE_PER_HOUR_PER_PHONE); + return false; + } + return true; + } + /** * @param ip 请求发短信的IP地址 * @return diff --git a/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/system/controller/LoginController.java b/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/system/controller/LoginController.java index 203a97a1a..5dff18684 100644 --- a/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/system/controller/LoginController.java +++ b/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/system/controller/LoginController.java @@ -379,6 +379,14 @@ public class LoginController { result.setSuccess(false); return result; } + //update-begin---author:scott ---date:20261013 for:【安全 GHSA-5qr3-c98m-9767】增加按手机号限流,不依赖客户端IP----------- + if(!DySmsLimit.canSendSmsToPhone(mobile)){ + result.setMessage("短信发送过于频繁,请稍后再试!"); + result.setCode(CommonConstant.PHONE_SMS_FAIL_CODE); + result.setSuccess(false); + return result; + } + //update-end---author:scott ---date:20261013 for:【安全 GHSA-5qr3-c98m-9767】增加按手机号限流,不依赖客户端IP----------- //------------------------------------------------------------------------------------- //随机数 @@ -898,6 +906,13 @@ public class LoginController { result.setSuccess(false); return result; } + //update-begin---author:scott ---date:20261013 for:【安全 GHSA-5qr3-c98m-9767】增加按手机号限流----------- + if (!DySmsLimit.canSendSmsToPhone(mobile)) { + result.setMessage("短信发送过于频繁,请稍后再试!"); + result.setSuccess(false); + return result; + } + //update-end---author:scott ---date:20261013 for:【安全 GHSA-5qr3-c98m-9767】增加按手机号限流----------- //随机数 String captcha = RandomUtil.randomNumbers(6); JSONObject obj = new JSONObject(); diff --git a/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/system/service/impl/SysUserServiceImpl.java b/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/system/service/impl/SysUserServiceImpl.java index 5ae513a6e..0f5e4775d 100644 --- a/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/system/service/impl/SysUserServiceImpl.java +++ b/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/system/service/impl/SysUserServiceImpl.java @@ -2258,6 +2258,11 @@ public class SysUserServiceImpl extends ServiceImpl impl log.warn("--------[警告] IP地址:{}, 短信接口请求太多-------", clientIp); throw new JeecgBootException("短信接口请求太多,请稍后再试!", CommonConstant.PHONE_SMS_FAIL_CODE); } + //update-begin---author:scott ---date:20261013 for:【安全 GHSA-5qr3-c98m-9767】增加按手机号限流----------- + if(!DySmsLimit.canSendSmsToPhone(phone)){ + throw new JeecgBootException("短信发送过于频繁,请稍后再试!", CommonConstant.PHONE_SMS_FAIL_CODE); + } + //update-end---author:scott ---date:20261013 for:【安全 GHSA-5qr3-c98m-9767】增加按手机号限流----------- //随机数 String captcha = RandomUtil.randomNumbers(6);