Browse Source

防重复提交调整

feature-1.1
wuxicheng 3 years ago
parent
commit
55a683a11e
  1. 4
      bnyer-services/bnyer-img/src/main/java/com/bnyer/img/query/UserVipRecordQuery.java
  2. 2
      bnyer-services/bnyer-order/src/main/java/com/bnyer/order/annotation/LimitRepeatRequest.java
  3. 39
      bnyer-services/bnyer-order/src/main/java/com/bnyer/order/aop/LimitRepeatRequestAspect.java
  4. 2
      bnyer-services/bnyer-pay/src/main/java/com/bnyer/pay/annotation/LimitRepeatRequest.java
  5. 37
      bnyer-services/bnyer-pay/src/main/java/com/bnyer/pay/aop/LimitRepeatRequestAspect.java
  6. 2
      bnyer-services/bnyer-pay/src/main/java/com/bnyer/pay/enums/EnumVerificationKey.java
  7. 8
      bnyer-services/bnyer-pay/src/main/java/com/bnyer/pay/utils/PaymentRefundUtil.java

4
bnyer-services/bnyer-img/src/main/java/com/bnyer/img/query/UserVipRecordQuery.java

@ -17,7 +17,9 @@ public class UserVipRecordQuery {
@ApiModelProperty(value = "id")
private Long id;
@ApiModelProperty(value = "用户id")
/**
* 用户id
*/
private Long userId;
/**
* 用户客户端类型

2
bnyer-services/bnyer-order/src/main/java/com/bnyer/order/annotation/LimitRepeatRequest.java

@ -31,7 +31,7 @@ public @interface LimitRepeatRequest {
* 当前时间内 api 只能请求一次单位秒
* @return
*/
int time() default 5;
long time() default 5;
/**
* 对部分参数做重复请求限制

39
bnyer-services/bnyer-order/src/main/java/com/bnyer/order/aop/LimitRepeatRequestAspect.java

@ -1,14 +1,15 @@
package com.bnyer.order.aop;
package com.bnyer.pay.aop;
import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bnyer.common.core.domain.R;
import com.bnyer.common.core.enums.ResponseEnum;
import com.bnyer.common.core.exception.ServiceException;
import com.bnyer.common.core.utils.MD5Util;
import com.bnyer.common.core.utils.StringUtils;
import com.bnyer.common.core.vo.UserInfoVo;
import com.bnyer.common.redis.service.RedisService;
import com.bnyer.common.redis.service.RedissonService;
import com.bnyer.common.security.utils.SecurityUtils;
import com.bnyer.order.annotation.LimitRepeatRequest;
import lombok.extern.slf4j.Slf4j;
@ -17,9 +18,10 @@ import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.redisson.api.RLock;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.lang.reflect.Method;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
@ -34,25 +36,24 @@ import java.util.concurrent.TimeUnit;
@Component
public class LimitRepeatRequestAspect {
@Autowired
private RedisService redisService;
@Resource
private RedissonService redissonService;
@Around("@annotation(limitRepeatRequest)")
public Object around(ProceedingJoinPoint joinPoint, LimitRepeatRequest limitRepeatRequest) throws Throwable{
String key = getKey(joinPoint,limitRepeatRequest);
Object cacheObject = redisService.getCacheObject(key);
Object message;
if (Objects.nonNull(cacheObject)){
RLock rLock = redissonService.getRLock(key);
boolean flag = rLock.tryLock(0,limitRepeatRequest.time(), TimeUnit.SECONDS);
if (!flag){
if (StringUtils.isNotBlank(limitRepeatRequest.message())){
message = limitRepeatRequest.message();
throw new ServiceException(limitRepeatRequest.message(),ResponseEnum.REPEAT_REQUEST_ERROR.getCode());
}else {
message = new R().buildRepeatRequest(limitRepeatRequest.time());
throw new ServiceException(limitRepeatRequest.time()+"分钟内"+ResponseEnum.REPEAT_REQUEST_ERROR.getMsg(),
ResponseEnum.REPEAT_REQUEST_ERROR.getCode());
}
}else {
redisService.setCacheObject(key,"1",(long)limitRepeatRequest.time(), TimeUnit.SECONDS);
message = joinPoint.proceed();
}
return message;
Object proceed = joinPoint.proceed();
return proceed;
}
/**
@ -63,13 +64,13 @@ public class LimitRepeatRequestAspect {
* @return
*/
private String getKey(ProceedingJoinPoint joinPoint, LimitRepeatRequest limitRepeatRequest) {
UserInfoVo userInfo = SecurityUtils.getUserInfo();
Method currentMethod = getCurrentMethod(joinPoint);
//最后拼接好的key
StringBuilder key = new StringBuilder("LimitRepeatRequestAspect#" + currentMethod.getName());
//限制范围
String userRange = limitRepeatRequest.userRange();
if (LimitRepeatRequest.SELF.equals(userRange)){
UserInfoVo userInfo = SecurityUtils.getUserInfo();
key.append("#");
key.append(userInfo.getUserClientType());
key.append("#");
@ -97,7 +98,7 @@ public class LimitRepeatRequestAspect {
String value = requestParams.containsKey(param)?requestParams.getString(param):"";
key.append(value);
}else if(obj instanceof Integer){
String value = requestParams.containsKey(param)?requestParams.getString(param):"";
String value = requestParams.containsKey(param)?String.valueOf(requestParams.getInteger(param)):"";
key.append(value);
}
}
@ -120,9 +121,7 @@ public class LimitRepeatRequestAspect {
private JSONObject getRequestParams(ProceedingJoinPoint joinPoint) {
Object[] args = joinPoint.getArgs();
if (args != null && args.length != 0){
String jsonString = JSON.toJSONString(args);
JSONArray parseArray = JSON.parseArray(jsonString);
return parseArray.getJSONObject(0);
return JSON.parseObject(args[0].toString());
}
return null;
}

2
bnyer-services/bnyer-pay/src/main/java/com/bnyer/pay/annotation/LimitRepeatRequest.java

@ -31,7 +31,7 @@ public @interface LimitRepeatRequest {
* 当前时间内 api 只能请求一次单位秒
* @return
*/
int time() default 5;
long time() default 5;
/**
* 对部分参数做重复请求限制

37
bnyer-services/bnyer-pay/src/main/java/com/bnyer/pay/aop/LimitRepeatRequestAspect.java

@ -4,11 +4,12 @@ import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bnyer.common.core.domain.R;
import com.bnyer.common.core.enums.ResponseEnum;
import com.bnyer.common.core.exception.ServiceException;
import com.bnyer.common.core.utils.MD5Util;
import com.bnyer.common.core.utils.StringUtils;
import com.bnyer.common.core.vo.UserInfoVo;
import com.bnyer.common.redis.service.RedisService;
import com.bnyer.common.redis.service.RedissonService;
import com.bnyer.common.security.utils.SecurityUtils;
import com.bnyer.pay.annotation.LimitRepeatRequest;
import lombok.extern.slf4j.Slf4j;
@ -17,9 +18,10 @@ import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.redisson.api.RLock;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.lang.reflect.Method;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
@ -34,25 +36,24 @@ import java.util.concurrent.TimeUnit;
@Component
public class LimitRepeatRequestAspect {
@Autowired
private RedisService redisService;
@Resource
private RedissonService redissonService;
@Around("@annotation(limitRepeatRequest)")
public Object around(ProceedingJoinPoint joinPoint, LimitRepeatRequest limitRepeatRequest) throws Throwable{
String key = getKey(joinPoint,limitRepeatRequest);
Object cacheObject = redisService.getCacheObject(key);
Object message;
if (Objects.nonNull(cacheObject)){
RLock rLock = redissonService.getRLock(key);
boolean flag = rLock.tryLock(0,limitRepeatRequest.time(), TimeUnit.SECONDS);
if (!flag){
if (StringUtils.isNotBlank(limitRepeatRequest.message())){
message = limitRepeatRequest.message();
throw new ServiceException(limitRepeatRequest.message(),ResponseEnum.REPEAT_REQUEST_ERROR.getCode());
}else {
message = new R().buildRepeatRequest(limitRepeatRequest.time());
throw new ServiceException(limitRepeatRequest.time()+"分钟内"+ResponseEnum.REPEAT_REQUEST_ERROR.getMsg(),
ResponseEnum.REPEAT_REQUEST_ERROR.getCode());
}
}else {
redisService.setCacheObject(key,"1",(long)limitRepeatRequest.time(), TimeUnit.SECONDS);
message = joinPoint.proceed();
}
return message;
Object proceed = joinPoint.proceed();
return proceed;
}
/**
@ -63,13 +64,13 @@ public class LimitRepeatRequestAspect {
* @return
*/
private String getKey(ProceedingJoinPoint joinPoint, LimitRepeatRequest limitRepeatRequest) {
UserInfoVo userInfo = SecurityUtils.getUserInfo();
Method currentMethod = getCurrentMethod(joinPoint);
//最后拼接好的key
StringBuilder key = new StringBuilder("LimitRepeatRequestAspect#" + currentMethod.getName());
//限制范围
String userRange = limitRepeatRequest.userRange();
if (LimitRepeatRequest.SELF.equals(userRange)){
UserInfoVo userInfo = SecurityUtils.getUserInfo();
key.append("#");
key.append(userInfo.getUserClientType());
key.append("#");
@ -97,7 +98,7 @@ public class LimitRepeatRequestAspect {
String value = requestParams.containsKey(param)?requestParams.getString(param):"";
key.append(value);
}else if(obj instanceof Integer){
String value = requestParams.containsKey(param)?requestParams.getString(param):"";
String value = requestParams.containsKey(param)?String.valueOf(requestParams.getInteger(param)):"";
key.append(value);
}
}
@ -120,9 +121,7 @@ public class LimitRepeatRequestAspect {
private JSONObject getRequestParams(ProceedingJoinPoint joinPoint) {
Object[] args = joinPoint.getArgs();
if (args != null && args.length != 0){
String jsonString = JSON.toJSONString(args);
JSONArray parseArray = JSON.parseArray(jsonString);
return parseArray.getJSONObject(0);
return JSON.parseObject(args[0].toString());
}
return null;
}

2
bnyer-services/bnyer-pay/src/main/java/com/bnyer/pay/enums/EnumVerificationKey.java

@ -13,7 +13,7 @@ import lombok.NoArgsConstructor;
@NoArgsConstructor
@AllArgsConstructor
public enum EnumVerificationKey {
VIP("vip", "VipInOrder20230512Key"),
BNYER("bnyer", "BnyerInOrder20230512Key"),
;
private String key;
private String value;

8
bnyer-services/bnyer-pay/src/main/java/com/bnyer/pay/utils/PaymentRefundUtil.java

@ -17,7 +17,7 @@ public class PaymentRefundUtil {
* @return
*/
public static String getSign(RefundDto dto) {
return MD5Util.getMD5String(dto.getPayId() + EnumVerificationKey.VIP.getValue());
return MD5Util.getMD5String(dto.getPayId() + EnumVerificationKey.BNYER.getValue());
}
/**
@ -27,13 +27,13 @@ public class PaymentRefundUtil {
*/
public static boolean checkSign(RefundDto dto) {
return MD5Util.getMD5String(dto.getPayId() + EnumVerificationKey.VIP.getValue()).equals(dto.getSign());
return MD5Util.getMD5String(dto.getPayId() + EnumVerificationKey.BNYER.getValue()).equals(dto.getSign());
}
public static void main(String[] args) {
String sign = MD5Util.getMD5String("RVWU202305121022211042" + EnumVerificationKey.VIP.getValue());
String sign = MD5Util.getMD5String("RVWU202305121022211042" + EnumVerificationKey.BNYER.getValue());
System.out.println(sign);
System.out.println(MD5Util.getMD5String("RVWU202305121022211042" + EnumVerificationKey.VIP.getValue()).equals(sign));
System.out.println(MD5Util.getMD5String("RVWU202305121022211042" + EnumVerificationKey.BNYER.getValue()).equals(sign));
}
}

Loading…
Cancel
Save