|
@@ -1,19 +1,24 @@
|
|
|
package com.huyi.service.aspect;
|
|
|
|
|
|
+import com.huyi.service.annotation.Repeat;
|
|
|
import com.tianhu.common.core.exception.RepeatException;
|
|
|
import com.tianhu.common.core.utils.DateUtils;
|
|
|
import com.tianhu.common.core.utils.ServletUtils;
|
|
|
import com.tianhu.common.core.utils.StringUtils;
|
|
|
import com.tianhu.common.redis.service.RedisService;
|
|
|
+import org.aspectj.lang.JoinPoint;
|
|
|
+import org.aspectj.lang.Signature;
|
|
|
import org.aspectj.lang.annotation.AfterReturning;
|
|
|
import org.aspectj.lang.annotation.AfterThrowing;
|
|
|
import org.aspectj.lang.annotation.Aspect;
|
|
|
import org.aspectj.lang.annotation.Before;
|
|
|
+import org.aspectj.lang.reflect.MethodSignature;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
+import java.lang.reflect.Method;
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
/**
|
|
@@ -26,39 +31,64 @@ public class RepeatAspect {
|
|
|
|
|
|
private static final Logger log = LoggerFactory.getLogger(com.huyi.service.aspect.RepeatAspect.class);
|
|
|
private static final String REPEAT_TOKEN = "repeatToken";
|
|
|
- private static final Long TIME_OUT = 15L;
|
|
|
|
|
|
@Autowired
|
|
|
private RedisService redisService;
|
|
|
|
|
|
@Before("@annotation(com.huyi.service.annotation.Repeat)")
|
|
|
- public void repeatBefore() {
|
|
|
- String repeatToken = ServletUtils.getParameter(REPEAT_TOKEN);
|
|
|
- if (StringUtils.isEmpty(repeatToken)) {
|
|
|
- throw new RepeatException("repeatToken 不可为空!");
|
|
|
- }
|
|
|
- String redisValue = redisService.getCacheObject(REPEAT_TOKEN + repeatToken);
|
|
|
+ public void repeatBefore(JoinPoint joinPoint) {
|
|
|
+ // 获得注解
|
|
|
+ Signature signature = joinPoint.getSignature();
|
|
|
+ MethodSignature methodSignature = (MethodSignature) signature;
|
|
|
+ Method method = methodSignature.getMethod();
|
|
|
+ if (method != null)
|
|
|
+ {
|
|
|
+ Repeat repeat = method.getAnnotation(Repeat.class);
|
|
|
+ long timeout = repeat.timeOut();
|
|
|
+ String repeatToken = ServletUtils.getRequest().getHeader(REPEAT_TOKEN);
|
|
|
+ if (StringUtils.isEmpty(repeatToken)) {
|
|
|
+ throw new RepeatException("repeatToken 不可为空!");
|
|
|
+ }
|
|
|
+ String redisValue = redisService.getCacheObject(REPEAT_TOKEN + repeatToken);
|
|
|
|
|
|
- if (!StringUtils.isEmpty(redisValue)) {
|
|
|
- log.info(ServletUtils.getRequest().getRequestURI() + "重复提交! repeatToken : 【" + repeatToken + "】");
|
|
|
- throw new RepeatException();
|
|
|
+ if (!StringUtils.isEmpty(redisValue)) {
|
|
|
+ log.info(ServletUtils.getRequest().getRequestURI() + "重复提交! repeatToken : 【" + repeatToken + "】");
|
|
|
+ throw new RepeatException("请勿重复提交");
|
|
|
+ }
|
|
|
+ redisService.setCacheObject(REPEAT_TOKEN + repeatToken, DateUtils.getTime(), timeout, TimeUnit.SECONDS);
|
|
|
}
|
|
|
- redisService.setCacheObject(REPEAT_TOKEN + repeatToken, DateUtils.getTime(), TIME_OUT, TimeUnit.SECONDS);
|
|
|
}
|
|
|
|
|
|
@AfterReturning("@annotation(com.huyi.service.annotation.Repeat)")
|
|
|
- public void repeatRequestAfterReturning() throws Throwable {
|
|
|
- String repeatToken = ServletUtils.getParameter("repeatToken");
|
|
|
- if (!StringUtils.isEmpty(repeatToken)) {
|
|
|
- redisService.setCacheObject(REPEAT_TOKEN + repeatToken, null, TIME_OUT, TimeUnit.SECONDS);
|
|
|
+ public void repeatRequestAfterReturning(JoinPoint joinPoint) throws Throwable {
|
|
|
+ // 获得注解
|
|
|
+ Signature signature = joinPoint.getSignature();
|
|
|
+ MethodSignature methodSignature = (MethodSignature) signature;
|
|
|
+ Method method = methodSignature.getMethod();
|
|
|
+ if (method != null)
|
|
|
+ {
|
|
|
+ Repeat repeat = method.getAnnotation(Repeat.class);
|
|
|
+ long timeout = repeat.timeOut();
|
|
|
+ String repeatToken = ServletUtils.getRequest().getHeader(REPEAT_TOKEN);
|
|
|
+ if (!StringUtils.isEmpty(repeatToken)) {
|
|
|
+ redisService.setCacheObject(REPEAT_TOKEN + repeatToken, null, timeout, TimeUnit.SECONDS);
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@AfterThrowing("@annotation(com.huyi.service.annotation.Repeat)")
|
|
|
- public void repeatRequestAfterThrowing() {
|
|
|
- String repeatToken = ServletUtils.getParameter("repeatToken");
|
|
|
- if (!StringUtils.isEmpty(repeatToken)) {
|
|
|
- redisService.setCacheObject(REPEAT_TOKEN + repeatToken, null, TIME_OUT, TimeUnit.SECONDS);
|
|
|
+ public void repeatRequestAfterThrowing(JoinPoint joinPoint) {
|
|
|
+ Signature signature = joinPoint.getSignature();
|
|
|
+ MethodSignature methodSignature = (MethodSignature) signature;
|
|
|
+ Method method = methodSignature.getMethod();
|
|
|
+ if (method != null)
|
|
|
+ {
|
|
|
+ Repeat repeat = method.getAnnotation(Repeat.class);
|
|
|
+ long timeout = repeat.timeOut();
|
|
|
+ String repeatToken = ServletUtils.getRequest().getHeader(REPEAT_TOKEN);
|
|
|
+ if (!StringUtils.isEmpty(repeatToken)) {
|
|
|
+ redisService.setCacheObject(REPEAT_TOKEN + repeatToken, null, timeout, TimeUnit.SECONDS);
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|