|
@@ -0,0 +1,63 @@
|
|
|
+package com.tianhu.system.aspect;
|
|
|
+
|
|
|
+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.ProceedingJoinPoint;
|
|
|
+import org.aspectj.lang.annotation.*;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 防重复提交AOP
|
|
|
+ * @author tianhu
|
|
|
+ */
|
|
|
+@Aspect
|
|
|
+@Component
|
|
|
+public class RepeatAspect {
|
|
|
+
|
|
|
+ private static final Logger log = LoggerFactory.getLogger(com.tianhu.system.aspect.RepeatAspect.class);
|
|
|
+ private static final String REPEAT_TOKEN = "repeatToken";
|
|
|
+ private static final Long TIME_OUT = 15L;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private RedisService redisService;
|
|
|
+
|
|
|
+ @Before("@annotation(com.tianhu.system.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);
|
|
|
+
|
|
|
+ if (!StringUtils.isEmpty(redisValue)) {
|
|
|
+ log.info(ServletUtils.getRequest().getRequestURI() + "重复提交! repeatToken : 【" + repeatToken + "】");
|
|
|
+ throw new RepeatException();
|
|
|
+ }
|
|
|
+ redisService.setCacheObject(REPEAT_TOKEN + repeatToken, DateUtils.getTime(), TIME_OUT, TimeUnit.SECONDS);
|
|
|
+ }
|
|
|
+
|
|
|
+ @AfterReturning("@annotation(com.tianhu.system.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);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @AfterThrowing("@annotation(com.tianhu.system.annotation.Repeat)")
|
|
|
+ public void repeatRequestAfterThrowing() {
|
|
|
+ String repeatToken = ServletUtils.getParameter("repeatToken");
|
|
|
+ if (!StringUtils.isEmpty(repeatToken)) {
|
|
|
+ redisService.setCacheObject(REPEAT_TOKEN + repeatToken, null, TIME_OUT, TimeUnit.SECONDS);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+}
|