Compare commits
2 Commits
d3471e5618
...
3b7303682f
| Author | SHA1 | Date |
|---|---|---|
|
|
3b7303682f | |
|
|
3f4414c0f9 |
|
|
@ -97,6 +97,7 @@ public enum ErrorCode {
|
|||
SMS_SOURCE_CODE_NOT_EXIST("04-01-001", "短信码不存在"),
|
||||
SMS_CODE_EXPIRED("04-01-002", "验证码过期,请重新发送"),
|
||||
SMS_CODE_NOT_RIGHT("04-01-003", "验证码不正确"),
|
||||
SMS_CODE_TOO_FREQUENT("04-01-004", "操作过于频繁,请稍后再试"),
|
||||
|
||||
// gbs
|
||||
GBS_USER_ERROR("04-01-001", "gbs用户未登录"),
|
||||
|
|
|
|||
|
|
@ -13,6 +13,8 @@ import org.apache.commons.lang3.StringUtils;
|
|||
import org.apache.dubbo.config.annotation.DubboReference;
|
||||
import org.qinan.safetyeval.client.dto.SendMessageCmd;
|
||||
import org.qinan.safetyeval.domain.exception.BizException;
|
||||
import org.redisson.api.RLock;
|
||||
import org.redisson.api.RedissonClient;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.http.HttpEntity;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
|
|
@ -33,6 +35,7 @@ import java.util.HashMap;
|
|||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static org.qinan.safetyeval.domain.exception.ErrorCode.*;
|
||||
|
||||
|
|
@ -59,6 +62,7 @@ public class SmsRemote {
|
|||
@DubboReference
|
||||
private MessageFacade messageFacade;
|
||||
private final RedisService redisService;
|
||||
private final RedissonClient redissonClient;
|
||||
|
||||
|
||||
@Value("${sms.sign.sourceCode:}")
|
||||
|
|
@ -97,14 +101,34 @@ public class SmsRemote {
|
|||
}
|
||||
|
||||
public SingleResponse<String> signValidCode(String bizId, String validCode) {
|
||||
Object code = redisService.get(bizId);
|
||||
if (code == null) {
|
||||
throw new BizException(SMS_CODE_EXPIRED);
|
||||
// 以 bizId 为 key 加分布式锁,锁 1s,防止刷验证码
|
||||
RLock lock = redissonClient.getLock(bizId);
|
||||
boolean locked;
|
||||
try {
|
||||
// 不等待,拿不到锁即视为频繁操作;锁自动 1s 后释放
|
||||
locked = lock.tryLock(0, 1, TimeUnit.SECONDS);
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
throw new BizException(SMS_CODE_TOO_FREQUENT);
|
||||
}
|
||||
if (code.toString().equals(validCode)) {
|
||||
throw new BizException(SMS_CODE_NOT_RIGHT);
|
||||
if (!locked) {
|
||||
throw new BizException(SMS_CODE_TOO_FREQUENT);
|
||||
}
|
||||
try {
|
||||
Object code = redisService.get(bizId);
|
||||
if (code == null) {
|
||||
throw new BizException(SMS_CODE_EXPIRED);
|
||||
}
|
||||
|
||||
if (code.toString().equals(validCode)) {
|
||||
throw new BizException(SMS_CODE_NOT_RIGHT);
|
||||
}
|
||||
return SingleResponse.of(bizId);
|
||||
} finally {
|
||||
if (lock.isHeldByCurrentThread()) {
|
||||
lock.unlock();
|
||||
}
|
||||
}
|
||||
return SingleResponse.of(bizId);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
Loading…
Reference in New Issue