短信-限流-开关
This commit is contained in:
@@ -167,7 +167,7 @@ public class AdminUserService extends BaseService {
|
||||
logger.info("sendCodeAliyunSmsOrThrowEx");
|
||||
DeviceInfo deviceInfo = new DeviceInfo();
|
||||
deviceInfo.setApp("admin");
|
||||
smsService.sendSmsCode(adminUser.getPhone(), SmsTypeEnum.SUPER_ADMIN_LOGIN.value, deviceInfo, realIpAddress, smsCode);
|
||||
smsService.sendSmsCode(adminUser.getPhone(), SmsTypeEnum.SUPER_ADMIN_LOGIN.value, deviceInfo, realIpAddress, smsCode, false);
|
||||
saveSmsCode(adminUser, smsCode, realIpAddress);
|
||||
return new BusiResult<>(BusiStatus.SUCCESS);
|
||||
}
|
||||
|
@@ -70,7 +70,7 @@ public class FlowTeamAdminUserService extends BaseService {
|
||||
}
|
||||
String smsCode = String.format("%d", RandomUtil.getFiveRandomNumber());
|
||||
smsService.sendSmsCode(phone,
|
||||
SmsTypeEnum.SUPER_ADMIN_LOGIN.value, null, realIpAddress, smsCode);
|
||||
SmsTypeEnum.SUPER_ADMIN_LOGIN.value, null, realIpAddress, smsCode, false);
|
||||
saveSmsCode(phone, smsCode, realIpAddress);
|
||||
return new BusiResult(BusiStatus.SUCCESS);
|
||||
}
|
||||
|
@@ -45,7 +45,7 @@ public class SmsService extends BaseService {
|
||||
@Autowired
|
||||
private SysConfService sysConfService;
|
||||
|
||||
public void sendSmsCode(String mobile, Integer type, DeviceInfo deviceInfo, String ip, String code) {
|
||||
public void sendSmsCode(String mobile, Integer type, DeviceInfo deviceInfo, String ip, String code, boolean needRateLimit) {
|
||||
if (StringUtils.isBlank(code)) {
|
||||
code = String.format("%d", RandomUtil.getFiveRandomNumber());
|
||||
}
|
||||
@@ -55,8 +55,10 @@ public class SmsService extends BaseService {
|
||||
throw new ServiceException(BusiStatus.SMS_NOT_EXPIRED);
|
||||
}
|
||||
|
||||
RRateLimiter rateLimiter = null;
|
||||
if (needRateLimit){
|
||||
String ipKey = RedisKey.sms_send_interval.getKey(ip);
|
||||
RRateLimiter rateLimiter = redissonClient.getRateLimiter(ipKey);
|
||||
rateLimiter = redissonClient.getRateLimiter(ipKey);
|
||||
if (!rateLimiter.isExists()) {
|
||||
rateLimiter.trySetRate(RateType.OVERALL, 5, 1, RateIntervalUnit.DAYS);
|
||||
}
|
||||
@@ -65,6 +67,7 @@ public class SmsService extends BaseService {
|
||||
log.error("[sms rateLimiter] {} {} 被限流了", ip, type);
|
||||
throw new ServiceException(BusiStatus.SMS_IP_TO_OFTEN);
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
// 格式化手机号
|
||||
@@ -98,8 +101,10 @@ public class SmsService extends BaseService {
|
||||
// 写入缓存
|
||||
jedisService.setex(getSmsKey(mobile), SmsConstant.SMS_EXPIRE_SECONDS, code);
|
||||
|
||||
if (null != rateLimiter){
|
||||
rateLimiter.expire(1, TimeUnit.DAYS);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean verifySmsCode(String mobile, String code) {
|
||||
logger.info("verifySmsCode mobile : {}, code : {}", mobile, code);
|
||||
|
@@ -103,7 +103,7 @@ public abstract class AbstractCertifyService {
|
||||
if (StringUtils.isNotBlank(userLimit) && Integer.parseInt(userLimit) > Constant.USER_CERTIFY_LIMIT_TIMES) {
|
||||
throw new ServiceException(BusiStatus.SMS_DEVICE_LIMIT);
|
||||
}
|
||||
smsService.sendSmsCode(phone, SmsTypeEnum.CERTIFICATION.value, deviceInfo, ip, null);
|
||||
smsService.sendSmsCode(phone, SmsTypeEnum.CERTIFICATION.value, deviceInfo, ip, null, false);
|
||||
}
|
||||
|
||||
protected String getDeviceId(Long uid) {
|
||||
|
@@ -95,7 +95,7 @@ public class SmsController extends BaseController {
|
||||
return new BusiResult<>(BusiStatus.ACCOUNT_BLOCK_ERROR, I18NMessageSourceUtil.getMessage(ACCOUNT_LOGIN_BLOCK_MSG, new Object[]{mobile}, PartitionEnum.ENGLISH.getId()));
|
||||
}
|
||||
|
||||
smsService.sendSmsCode(mobile, type, deviceInfo, ip, null);
|
||||
smsService.sendSmsCode(mobile, type, deviceInfo, ip, null, true);
|
||||
return new BusiResult<>(BusiStatus.SMS_SEND_SUCCESS);
|
||||
}
|
||||
|
||||
|
@@ -1,17 +0,0 @@
|
||||
package servicetest;
|
||||
|
||||
import com.accompany.sms.service.SmsService;
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
public class SmsSendTest extends CommonTest {
|
||||
|
||||
@Autowired
|
||||
private SmsService smsService;
|
||||
|
||||
@Test
|
||||
public void sendSms() throws InterruptedException {
|
||||
smsService.sendSmsCode("8617817447469",15,null,"127.0.0.1","51145");
|
||||
smsService.sendSmsCode("17817447469",15,null,"127.0.0.1","88154");
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user