|
|
|
@ -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; |
|
|
|
} |
|
|
|
|