Преглед на файлове

发送短信验证码

master
niohe·erbao преди 1 година
родител
ревизия
595d0e53b9
променени са 4 файла, в които са добавени 70 реда и са изтрити 60 реда
  1. +5
    -0
      pmapi/pom.xml
  2. +1
    -1
      pmapi/src/main/java/com/ningdatech/pmapi/sms/controller/VerificationCodeController.java
  3. +62
    -59
      pmapi/src/main/java/com/ningdatech/pmapi/sms/manage/SmsManage.java
  4. +2
    -0
      pmapi/src/main/resources/security/auth-dev.yml

+ 5
- 0
pmapi/pom.xml Целия файл

@@ -191,6 +191,11 @@
<artifactId>nd-basic</artifactId>
</dependency>
<dependency>
<groupId>com.ningdatech</groupId>
<artifactId>nd-yxt-starter</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel-core</artifactId>
</dependency>


+ 1
- 1
pmapi/src/main/java/com/ningdatech/pmapi/sms/controller/VerificationCodeController.java Целия файл

@@ -35,7 +35,7 @@ public class VerificationCodeController {
@ApiOperation(value = "发送验证码", notes = "发送验证码")
@PostMapping(value = "/send")
public void send(@Validated @RequestBody ReqVerificationCodePO request) {
// smsManage.sendVerificationCode(request);
smsManage.sendVerificationCode(request);
}

}

+ 62
- 59
pmapi/src/main/java/com/ningdatech/pmapi/sms/manage/SmsManage.java Целия файл

@@ -11,6 +11,9 @@ import com.ningdatech.pmapi.sms.model.dto.VerifyCodeCacheDTO;
import com.ningdatech.pmapi.sms.model.po.ReqVerificationCodePO;
import com.ningdatech.pmapi.sms.utils.DateUtil;
import com.ningdatech.pmapi.sms.utils.SmsRedisKeyUtils;
import com.ningdatech.yxt.client.YxtClient;
import com.ningdatech.yxt.constants.YxtSmsSignEnum;
import com.ningdatech.yxt.model.cmd.SendSmsCmd;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
@@ -29,65 +32,65 @@ import java.util.Collections;
@Component
@RequiredArgsConstructor
public class SmsManage {
//
// private final YxtClient yxtClient;
// private final CachePlusOps cachePlusOps;
//
// public void sendVerificationCode(ReqVerificationCodePO request) {
// Assert.isTrue(PhoneUtil.isMobile(request.getMobile()), "手机号码格式不正确");
// String verificationType = request.getVerificationType();
// VerificationCodeType verificationCodeTypeEnum = VerificationCodeType.of(verificationType);
//
// // 验证是否被锁定
// String lockKey = SmsRedisKeyUtils.smsSendLockKey(verificationCodeTypeEnum, request.getMobile());
// if (StringUtils.isNotBlank(cachePlusOps.get(lockKey))) {
// throw BizException.wrap("今日" + verificationCodeTypeEnum.getDesc() + "的验证码发送次数过多,已被锁定");
// }
// // 验证发送间隔
// String cacheKey = SmsRedisKeyUtils.smsCodeVerifyKey(verificationCodeTypeEnum, request.getMobile());
// VerifyCodeCacheDTO preCache = (VerifyCodeCacheDTO) cachePlusOps.get(cacheKey);
// if (preCache != null) {
// if (LocalDateTime.now().minusMinutes(verificationCodeTypeEnum.getSendInterval())
// .isBefore(preCache.getSendTime())) {
// throw BizException.wrap(verificationCodeTypeEnum.getSendInterval() + "分钟之内已发送过验证码,请稍后重试");
// }
// }
// String code = RandomUtil.randomNumbers(6);
// VerifyCodeCacheDTO cache = VerifyCodeCacheDTO.builder()
// .code(code)
// .sendTime(LocalDateTime.now())
// .mobile(request.getMobile())
// .build();
//
// // 创建短信内容
// SendSmsCmd sendSmsCmd = new SendSmsCmd();
// switch (verificationCodeTypeEnum) {
// case LOGIN:
// SendSmsCmd.SendSmsContext sendSmsContext = new SendSmsCmd.SendSmsContext();
// sendSmsContext.setReceiveNumber(request.getMobile());
// sendSmsContext.setContent(String.format(YxtSmsTemplateConst.SMS_LOGIN_TEMPLATE, code, verificationCodeTypeEnum.getExpireTime()));
// sendSmsCmd.setContextList(Collections.singletonList(sendSmsContext));
// sendSmsCmd.setSmsSignEnum(YxtSmsSignEnum.ZJS_ELECTRONIC_EXPERT_LIB);
// break;
// default:
// throw new IllegalArgumentException("非法的短信发送类型");
// }
// // 发送 短信
// yxtClient.submitSmsTask(sendSmsCmd);
// log.info("send verificationCode mobile = {},code = {}", request.getMobile(), code);
//
// cachePlusOps.set(new CacheKey(cacheKey, Duration.ofMinutes(verificationCodeTypeEnum.getExpireTime())), cache);
// String limitKey = SmsRedisKeyUtils.smsSendLimitKey(verificationCodeTypeEnum, request.getMobile());
// if (StringUtils.isNotBlank(cachePlusOps.get(limitKey))) {
// long limitCount = cachePlusOps.incr(new CacheKey(limitKey, Duration.ofSeconds(DateUtil.restSecondsFromNowToNoon())));
// // 超出单日发送次数之后直接锁定
// if (limitCount >= verificationCodeTypeEnum.getSendTimesByDay().longValue()) {
// cachePlusOps.set(new CacheKey(lockKey, Duration.ofSeconds(DateUtil.restSecondsFromNowToNoon())), request.getMobile());
// }
// } else {
// cachePlusOps.set(new CacheKey(limitKey, Duration.ofSeconds(DateUtil.restSecondsFromNowToNoon())), 1);
// }
// }
private final YxtClient yxtClient;
private final CachePlusOps cachePlusOps;
public void sendVerificationCode(ReqVerificationCodePO request) {
Assert.isTrue(PhoneUtil.isMobile(request.getMobile()), "手机号码格式不正确");
String verificationType = request.getVerificationType();
VerificationCodeType verificationCodeTypeEnum = VerificationCodeType.of(verificationType);
// 验证是否被锁定
String lockKey = SmsRedisKeyUtils.smsSendLockKey(verificationCodeTypeEnum, request.getMobile());
if (StringUtils.isNotBlank(cachePlusOps.get(lockKey))) {
throw BizException.wrap("今日" + verificationCodeTypeEnum.getDesc() + "的验证码发送次数过多,已被锁定");
}
// 验证发送间隔
String cacheKey = SmsRedisKeyUtils.smsCodeVerifyKey(verificationCodeTypeEnum, request.getMobile());
VerifyCodeCacheDTO preCache = (VerifyCodeCacheDTO) cachePlusOps.get(cacheKey);
if (preCache != null) {
if (LocalDateTime.now().minusMinutes(verificationCodeTypeEnum.getSendInterval())
.isBefore(preCache.getSendTime())) {
throw BizException.wrap(verificationCodeTypeEnum.getSendInterval() + "分钟之内已发送过验证码,请稍后重试");
}
}
String code = RandomUtil.randomNumbers(6);
VerifyCodeCacheDTO cache = VerifyCodeCacheDTO.builder()
.code(code)
.sendTime(LocalDateTime.now())
.mobile(request.getMobile())
.build();
// 创建短信内容
SendSmsCmd sendSmsCmd = new SendSmsCmd();
switch (verificationCodeTypeEnum) {
case LOGIN:
SendSmsCmd.SendSmsContext sendSmsContext = new SendSmsCmd.SendSmsContext();
sendSmsContext.setReceiveNumber(request.getMobile());
sendSmsContext.setContent(String.format(YxtSmsTemplateConst.SMS_LOGIN_TEMPLATE, code, verificationCodeTypeEnum.getExpireTime()));
sendSmsCmd.setContextList(Collections.singletonList(sendSmsContext));
sendSmsCmd.setSmsSignEnum(YxtSmsSignEnum.ZJS_ELECTRONIC_EXPERT_LIB);
break;
default:
throw new IllegalArgumentException("非法的短信发送类型");
}
// 发送 短信
yxtClient.submitSmsTask(sendSmsCmd);
log.info("send verificationCode mobile = {},code = {}", request.getMobile(), code);
cachePlusOps.set(new CacheKey(cacheKey, Duration.ofMinutes(verificationCodeTypeEnum.getExpireTime())), cache);
String limitKey = SmsRedisKeyUtils.smsSendLimitKey(verificationCodeTypeEnum, request.getMobile());
if (StringUtils.isNotBlank(cachePlusOps.get(limitKey))) {
long limitCount = cachePlusOps.incr(new CacheKey(limitKey, Duration.ofSeconds(DateUtil.restSecondsFromNowToNoon())));
// 超出单日发送次数之后直接锁定
if (limitCount >= verificationCodeTypeEnum.getSendTimesByDay().longValue()) {
cachePlusOps.set(new CacheKey(lockKey, Duration.ofSeconds(DateUtil.restSecondsFromNowToNoon())), request.getMobile());
}
} else {
cachePlusOps.set(new CacheKey(limitKey, Duration.ofSeconds(DateUtil.restSecondsFromNowToNoon())), 1);
}
}


}

+ 2
- 0
pmapi/src/main/resources/security/auth-dev.yml Целия файл

@@ -21,6 +21,7 @@ security:
- /oa/**
- /wflow/**
- /sys/**
- /api/v1/verification/**
ignore-csrf-urls:
- /api/v1/user/auth/**
- /v2/api-docs
@@ -37,6 +38,7 @@ security:
- /oa/**
- /wflow/**
- /sys/**
- /api/v1/verification/**
role-map:
"engineer":
"project_manager":


Loading…
Отказ
Запис