From 55a683a11e6b3c29fc9f20480040375f98d792ba Mon Sep 17 00:00:00 2001 From: wuxicheng <1441859745@qq.com> Date: Mon, 15 May 2023 14:35:05 +0800 Subject: [PATCH] =?UTF-8?q?=E9=98=B2=E9=87=8D=E5=A4=8D=E6=8F=90=E4=BA=A4?= =?UTF-8?q?=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../bnyer/img/query/UserVipRecordQuery.java | 4 +- .../order/annotation/LimitRepeatRequest.java | 2 +- .../order/aop/LimitRepeatRequestAspect.java | 39 +++++++++---------- .../pay/annotation/LimitRepeatRequest.java | 2 +- .../pay/aop/LimitRepeatRequestAspect.java | 37 +++++++++--------- .../bnyer/pay/enums/EnumVerificationKey.java | 2 +- .../bnyer/pay/utils/PaymentRefundUtil.java | 8 ++-- 7 files changed, 47 insertions(+), 47 deletions(-) diff --git a/bnyer-services/bnyer-img/src/main/java/com/bnyer/img/query/UserVipRecordQuery.java b/bnyer-services/bnyer-img/src/main/java/com/bnyer/img/query/UserVipRecordQuery.java index 3f2c6a6..14b89f1 100644 --- a/bnyer-services/bnyer-img/src/main/java/com/bnyer/img/query/UserVipRecordQuery.java +++ b/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; /** * 用户客户端类型 diff --git a/bnyer-services/bnyer-order/src/main/java/com/bnyer/order/annotation/LimitRepeatRequest.java b/bnyer-services/bnyer-order/src/main/java/com/bnyer/order/annotation/LimitRepeatRequest.java index c27d369..a300398 100644 --- a/bnyer-services/bnyer-order/src/main/java/com/bnyer/order/annotation/LimitRepeatRequest.java +++ b/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; /** * 对部分参数做重复请求限制 diff --git a/bnyer-services/bnyer-order/src/main/java/com/bnyer/order/aop/LimitRepeatRequestAspect.java b/bnyer-services/bnyer-order/src/main/java/com/bnyer/order/aop/LimitRepeatRequestAspect.java index b13f80b..eaf7d8c 100644 --- a/bnyer-services/bnyer-order/src/main/java/com/bnyer/order/aop/LimitRepeatRequestAspect.java +++ b/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; } diff --git a/bnyer-services/bnyer-pay/src/main/java/com/bnyer/pay/annotation/LimitRepeatRequest.java b/bnyer-services/bnyer-pay/src/main/java/com/bnyer/pay/annotation/LimitRepeatRequest.java index 605b466..c226583 100644 --- a/bnyer-services/bnyer-pay/src/main/java/com/bnyer/pay/annotation/LimitRepeatRequest.java +++ b/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; /** * 对部分参数做重复请求限制 diff --git a/bnyer-services/bnyer-pay/src/main/java/com/bnyer/pay/aop/LimitRepeatRequestAspect.java b/bnyer-services/bnyer-pay/src/main/java/com/bnyer/pay/aop/LimitRepeatRequestAspect.java index e3116f0..26e05a4 100644 --- a/bnyer-services/bnyer-pay/src/main/java/com/bnyer/pay/aop/LimitRepeatRequestAspect.java +++ b/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; } diff --git a/bnyer-services/bnyer-pay/src/main/java/com/bnyer/pay/enums/EnumVerificationKey.java b/bnyer-services/bnyer-pay/src/main/java/com/bnyer/pay/enums/EnumVerificationKey.java index 279434e..2234160 100644 --- a/bnyer-services/bnyer-pay/src/main/java/com/bnyer/pay/enums/EnumVerificationKey.java +++ b/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; diff --git a/bnyer-services/bnyer-pay/src/main/java/com/bnyer/pay/utils/PaymentRefundUtil.java b/bnyer-services/bnyer-pay/src/main/java/com/bnyer/pay/utils/PaymentRefundUtil.java index cadee6e..5d04337 100644 --- a/bnyer-services/bnyer-pay/src/main/java/com/bnyer/pay/utils/PaymentRefundUtil.java +++ b/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)); } }