From 2fc1ccb05971d91a7988ef331901891d4dec060e Mon Sep 17 00:00:00 2001 From: zhangdaiscott Date: Tue, 13 Oct 2026 07:24:49 -0400 Subject: [PATCH] =?UTF-8?q?--author:scott--date:20261013--for:=E3=80=90?= =?UTF-8?q?=E5=AE=89=E5=85=A8=20GHSA-5qr3-c98m-9767=E3=80=91=E7=9F=AD?= =?UTF-8?q?=E4=BF=A1=E6=8E=A5=E5=8F=A3=E5=A2=9E=E5=8A=A0=E6=8C=89=E6=89=8B?= =?UTF-8?q?=E6=9C=BA=E5=8F=B7=E9=99=90=E6=B5=81=EF=BC=8C=E9=98=B2=E6=AD=A2?= =?UTF-8?q?=E4=BC=AA=E9=80=A0X-Forwarded-For=E7=BB=95=E8=BF=87IP=E9=99=90?= =?UTF-8?q?=E6=B5=81=E8=BF=9B=E8=A1=8C=E7=9F=AD=E4=BF=A1=E8=BD=B0=E7=82=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Opus 4.8 --- .../org/jeecg/common/util/DySmsLimit.java | 40 +++++++++++++++++++ .../system/controller/LoginController.java | 15 +++++++ .../service/impl/SysUserServiceImpl.java | 5 +++ 3 files changed, 60 insertions(+) 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);