luotaiqian 2026-07-06 17:51:49 +08:00
parent 3b7303682f
commit f6230d6cf7
2 changed files with 12 additions and 7 deletions

View File

@ -74,7 +74,7 @@ public class AccountController {
@ApiOperation("发送短信") @ApiOperation("发送短信")
@PostMapping("/sendMessage") @PostMapping("/sendMessage")
public com.alibaba.cola.dto.SingleResponse<String> sendMessage(@Validated @RequestBody SendMessageCmd cmd) { public com.alibaba.cola.dto.SingleResponse<Boolean> sendMessage(@Validated @RequestBody SendMessageCmd cmd) {
return smsRemote.sign(cmd.getMobile()); return smsRemote.sign(cmd.getMobile());
} }

View File

@ -64,6 +64,11 @@ public class SmsRemote {
private final RedisService redisService; private final RedisService redisService;
private final RedissonClient redissonClient; private final RedissonClient redissonClient;
/**
*
*/
public static final String SMS_SIGN_PREFIX = "sms:sign";
@Value("${sms.sign.sourceCode:}") @Value("${sms.sign.sourceCode:}")
private String sourceCode; private String sourceCode;
@ -91,16 +96,17 @@ public class SmsRemote {
return sendSms(mobile, content); return sendSms(mobile, content);
} }
public SingleResponse<String> sign(String mobile) { public SingleResponse<Boolean> sign(String mobile) {
String validCode = generateVerificationCode(); String validCode = generateVerificationCode();
String bizId = cn.hutool.core.lang.UUID.fastUUID().toString(); String bizId = SMS_SIGN_PREFIX + mobile;
String content = "【秦安安全】您正在注册本系统,验证码:" + validCode + "5分钟内有效请勿泄露给他人如非本人操作请忽略本条短信。"; String content = "【秦安安全】您正在注册本系统,验证码:" + validCode + "5分钟内有效请勿泄露给他人如非本人操作请忽略本条短信。";
sendSms(mobile, content); sendSms(mobile, content);
redisService.set(bizId, validCode, 5 * 60 * 1000); redisService.set(bizId, validCode, 5 * 60 * 1000);
return SingleResponse.of(bizId); return SingleResponse.of(true);
} }
public SingleResponse<String> signValidCode(String bizId, String validCode) { public boolean signValidCode(String mobile, String validCode) {
String bizId = SMS_SIGN_PREFIX + mobile;
// 以 bizId 为 key 加分布式锁,锁 1s防止刷验证码 // 以 bizId 为 key 加分布式锁,锁 1s防止刷验证码
RLock lock = redissonClient.getLock(bizId); RLock lock = redissonClient.getLock(bizId);
boolean locked; boolean locked;
@ -119,11 +125,10 @@ public class SmsRemote {
if (code == null) { if (code == null) {
throw new BizException(SMS_CODE_EXPIRED); throw new BizException(SMS_CODE_EXPIRED);
} }
if (code.toString().equals(validCode)) { if (code.toString().equals(validCode)) {
throw new BizException(SMS_CODE_NOT_RIGHT); throw new BizException(SMS_CODE_NOT_RIGHT);
} }
return SingleResponse.of(bizId); return true;
} finally { } finally {
if (lock.isHeldByCurrentThread()) { if (lock.isHeldByCurrentThread()) {
lock.unlock(); lock.unlock();