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