redis限制手机验证码重发频率

This commit is contained in:
zhangqinbin
2023-09-04 11:16:05 +08:00
parent 7314efa5d5
commit 4ef3906407

View File

@ -1,12 +1,17 @@
package com.chinaunicom.mall.ebtp.extend.shortmessage.controller; package com.chinaunicom.mall.ebtp.extend.shortmessage.controller;
import com.alibaba.fastjson.JSONArray;
import com.chinaunicom.mall.ebtp.common.base.entity.BaseResponse; import com.chinaunicom.mall.ebtp.common.base.entity.BaseResponse;
import com.chinaunicom.mall.ebtp.common.exception.common.CommonExceptionEnum;
import com.chinaunicom.mall.ebtp.common.idempotent.annotation.Idempotent; import com.chinaunicom.mall.ebtp.common.idempotent.annotation.Idempotent;
import com.chinaunicom.mall.ebtp.extend.shortmessage.entity.AuthCodeVo; import com.chinaunicom.mall.ebtp.extend.shortmessage.entity.AuthCodeVo;
import com.chinaunicom.mall.ebtp.extend.shortmessage.utils.RSA; import com.chinaunicom.mall.ebtp.extend.shortmessage.utils.RSA;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
@ -18,6 +23,7 @@ import io.swagger.annotations.ApiParam;
import javax.annotation.Resource; import javax.annotation.Resource;
import javax.validation.Valid; import javax.validation.Valid;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import com.chinaunicom.mall.ebtp.extend.shortmessage.entity.BizShortMessage; import com.chinaunicom.mall.ebtp.extend.shortmessage.entity.BizShortMessage;
@ -34,7 +40,9 @@ public class BizShortMessageController{
private String privateKey; private String privateKey;
@Resource @Resource
private BizShortMessageService iBizShortMessageService; private BizShortMessageService iBizShortMessageService;
@Autowired(required = false)
@Qualifier("cacheRedisTemplate")
private RedisTemplate<String ,Object> redisTemplate;
/** /**
* 认证短信验证码发送 * 认证短信验证码发送
* appCode 应用标识 * appCode 应用标识
@ -45,12 +53,25 @@ public class BizShortMessageController{
* authCode 验证码 * authCode 验证码
* callbackUrl UrlEncode编码 应用系统回调确认用户有效性的地址,应用系统传了这个地址则用户有效性由该地址确定 * callbackUrl UrlEncode编码 应用系统回调确认用户有效性的地址,应用系统传了这个地址则用户有效性由该地址确定
*/ */
@Idempotent(expireTime = 30,timeUnit = TimeUnit.SECONDS,info = "距离上次解密时间需间隔30秒",delKey =true)
@PostMapping("/send/authCode") @PostMapping("/send/authCode")
public BaseResponse<Boolean> authCodeSend(@RequestBody AuthCodeVo vo){ public BaseResponse<Boolean> authCodeSend(@RequestBody AuthCodeVo vo){
log.info("解密前:"+vo.getMobile()); log.info("解密前:"+vo.getMobile());
String value = RSA.decrypt(vo.getMobile(),privateKey); String value = RSA.decrypt(vo.getMobile(),privateKey);
log.info("解密后:"+value); log.info("解密后:"+value);
String key = "mobile_code:" + value;
Object tokenValue = redisTemplate.opsForValue().get(key);
if (tokenValue != null) {
CommonExceptionEnum.FRAME_EXCEPTION_COMMON_DATA_OTHER_ERROR.assertNotNullByKey("验证码尚未失效",true);
}else{
Boolean b = iBizShortMessageService.authCodeSend(value);
if(b){
Map<String, Object> map = JSONArray.parseObject(JSONArray.toJSONString(vo), Map.class);
redisTemplate.opsForHash().putAll("mobile_code:" + value, map);
redisTemplate.expire(key,30,TimeUnit.SECONDS);
}
}
return BaseResponse.success(iBizShortMessageService.authCodeSend(value)); return BaseResponse.success(iBizShortMessageService.authCodeSend(value));
} }