短信-限流-开关
This commit is contained in:
@@ -167,7 +167,7 @@ public class AdminUserService extends BaseService {
|
|||||||
logger.info("sendCodeAliyunSmsOrThrowEx");
|
logger.info("sendCodeAliyunSmsOrThrowEx");
|
||||||
DeviceInfo deviceInfo = new DeviceInfo();
|
DeviceInfo deviceInfo = new DeviceInfo();
|
||||||
deviceInfo.setApp("admin");
|
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);
|
saveSmsCode(adminUser, smsCode, realIpAddress);
|
||||||
return new BusiResult<>(BusiStatus.SUCCESS);
|
return new BusiResult<>(BusiStatus.SUCCESS);
|
||||||
}
|
}
|
||||||
|
@@ -70,7 +70,7 @@ public class FlowTeamAdminUserService extends BaseService {
|
|||||||
}
|
}
|
||||||
String smsCode = String.format("%d", RandomUtil.getFiveRandomNumber());
|
String smsCode = String.format("%d", RandomUtil.getFiveRandomNumber());
|
||||||
smsService.sendSmsCode(phone,
|
smsService.sendSmsCode(phone,
|
||||||
SmsTypeEnum.SUPER_ADMIN_LOGIN.value, null, realIpAddress, smsCode);
|
SmsTypeEnum.SUPER_ADMIN_LOGIN.value, null, realIpAddress, smsCode, false);
|
||||||
saveSmsCode(phone, smsCode, realIpAddress);
|
saveSmsCode(phone, smsCode, realIpAddress);
|
||||||
return new BusiResult(BusiStatus.SUCCESS);
|
return new BusiResult(BusiStatus.SUCCESS);
|
||||||
}
|
}
|
||||||
|
@@ -45,7 +45,7 @@ public class SmsService extends BaseService {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private SysConfService sysConfService;
|
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)) {
|
if (StringUtils.isBlank(code)) {
|
||||||
code = String.format("%d", RandomUtil.getFiveRandomNumber());
|
code = String.format("%d", RandomUtil.getFiveRandomNumber());
|
||||||
}
|
}
|
||||||
@@ -55,15 +55,18 @@ public class SmsService extends BaseService {
|
|||||||
throw new ServiceException(BusiStatus.SMS_NOT_EXPIRED);
|
throw new ServiceException(BusiStatus.SMS_NOT_EXPIRED);
|
||||||
}
|
}
|
||||||
|
|
||||||
String ipKey = RedisKey.sms_send_interval.getKey(ip);
|
RRateLimiter rateLimiter = null;
|
||||||
RRateLimiter rateLimiter = redissonClient.getRateLimiter(ipKey);
|
if (needRateLimit){
|
||||||
if (!rateLimiter.isExists()) {
|
String ipKey = RedisKey.sms_send_interval.getKey(ip);
|
||||||
rateLimiter.trySetRate(RateType.OVERALL, 5, 1, RateIntervalUnit.DAYS);
|
rateLimiter = redissonClient.getRateLimiter(ipKey);
|
||||||
}
|
if (!rateLimiter.isExists()) {
|
||||||
|
rateLimiter.trySetRate(RateType.OVERALL, 5, 1, RateIntervalUnit.DAYS);
|
||||||
|
}
|
||||||
|
|
||||||
if (!rateLimiter.tryAcquire()){
|
if (!rateLimiter.tryAcquire()){
|
||||||
log.error("[sms rateLimiter] {} {} 被限流了", ip, type);
|
log.error("[sms rateLimiter] {} {} 被限流了", ip, type);
|
||||||
throw new ServiceException(BusiStatus.SMS_IP_TO_OFTEN);
|
throw new ServiceException(BusiStatus.SMS_IP_TO_OFTEN);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -98,7 +101,9 @@ public class SmsService extends BaseService {
|
|||||||
// 写入缓存
|
// 写入缓存
|
||||||
jedisService.setex(getSmsKey(mobile), SmsConstant.SMS_EXPIRE_SECONDS, code);
|
jedisService.setex(getSmsKey(mobile), SmsConstant.SMS_EXPIRE_SECONDS, code);
|
||||||
|
|
||||||
rateLimiter.expire(1, TimeUnit.DAYS);
|
if (null != rateLimiter){
|
||||||
|
rateLimiter.expire(1, TimeUnit.DAYS);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean verifySmsCode(String mobile, String code) {
|
public boolean verifySmsCode(String mobile, String code) {
|
||||||
|
@@ -103,7 +103,7 @@ public abstract class AbstractCertifyService {
|
|||||||
if (StringUtils.isNotBlank(userLimit) && Integer.parseInt(userLimit) > Constant.USER_CERTIFY_LIMIT_TIMES) {
|
if (StringUtils.isNotBlank(userLimit) && Integer.parseInt(userLimit) > Constant.USER_CERTIFY_LIMIT_TIMES) {
|
||||||
throw new ServiceException(BusiStatus.SMS_DEVICE_LIMIT);
|
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) {
|
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()));
|
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);
|
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