|
@@ -15,6 +15,7 @@ import com.fdkankan.web.response.ResultData;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.aspectj.lang.JoinPoint;
|
|
import org.aspectj.lang.JoinPoint;
|
|
import org.aspectj.lang.annotation.AfterReturning;
|
|
import org.aspectj.lang.annotation.AfterReturning;
|
|
|
|
+import org.aspectj.lang.annotation.AfterThrowing;
|
|
import org.aspectj.lang.annotation.Aspect;
|
|
import org.aspectj.lang.annotation.Aspect;
|
|
import org.aspectj.lang.annotation.Before;
|
|
import org.aspectj.lang.annotation.Before;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
@@ -42,7 +43,7 @@ public class ValidateApiAOP {
|
|
private LocalOverCache localOverCache;
|
|
private LocalOverCache localOverCache;
|
|
|
|
|
|
@Before("@annotation(validateApi)")
|
|
@Before("@annotation(validateApi)")
|
|
- public void Before(JoinPoint joinPoint, ValidateApi validateApi) {
|
|
|
|
|
|
+ public void doBefore(JoinPoint joinPoint, ValidateApi validateApi) {
|
|
String method = validateApi.method();
|
|
String method = validateApi.method();
|
|
boolean counting = validateApi.counting();
|
|
boolean counting = validateApi.counting();
|
|
|
|
|
|
@@ -77,7 +78,7 @@ public class ValidateApiAOP {
|
|
//预减次数
|
|
//预减次数
|
|
long decrStock = redisUtil.decr(String.format(RedisKey.API_METHOD_COUNT, authorization), 1);
|
|
long decrStock = redisUtil.decr(String.format(RedisKey.API_METHOD_COUNT, authorization), 1);
|
|
log.info("前置减次数后,{}", decrStock);
|
|
log.info("前置减次数后,{}", decrStock);
|
|
- if (decrStock < 1) {
|
|
|
|
|
|
+ if (decrStock < 0) {
|
|
localOverCache.setTimedCache(authorization, true);
|
|
localOverCache.setTimedCache(authorization, true);
|
|
redisUtil.incr(String.format(RedisKey.API_METHOD_COUNT, authorization), 1);
|
|
redisUtil.incr(String.format(RedisKey.API_METHOD_COUNT, authorization), 1);
|
|
throw new ApiBusinessException(HttpStatus.COUNT_OVER, "");
|
|
throw new ApiBusinessException(HttpStatus.COUNT_OVER, "");
|
|
@@ -116,4 +117,28 @@ public class ValidateApiAOP {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 处理请求后执行
|
|
|
|
+ *
|
|
|
|
+ * @param joinPoint
|
|
|
|
+ * @param validateApi
|
|
|
|
+ * @param jsonResult
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ @AfterThrowing(throwing = "ex",pointcut = "@annotation(validateApi)")
|
|
|
|
+ public void doAfterThrowing(Throwable ex, ValidateApi validateApi) throws NoSuchMethodException {
|
|
|
|
+ String method = validateApi.method();
|
|
|
|
+ boolean counting = validateApi.counting();
|
|
|
|
+ log.info("后置拦截截鉴权计数,{}", method);
|
|
|
|
+ //获取请求对象
|
|
|
|
+ ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
|
|
|
|
+ HttpServletRequest request = servletRequestAttributes.getRequest();
|
|
|
|
+ String authorization = request.getHeader("Authorization");
|
|
|
|
+ if(counting){
|
|
|
|
+ redisUtil.incr(String.format(RedisKey.API_METHOD_COUNT, authorization), 1);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
}
|
|
}
|