diff --git a/pom.xml b/pom.xml index b3670c9..cd49a34 100644 --- a/pom.xml +++ b/pom.xml @@ -149,6 +149,12 @@ aspectjweaver 1.9.4 + + + com.google.guava + guava + 20.0 + diff --git a/src/main/java/com/gitlink/softbot/annotation/NoRepeatSubmit.java b/src/main/java/com/gitlink/softbot/annotation/NoRepeatSubmit.java new file mode 100644 index 0000000..dd0aae2 --- /dev/null +++ b/src/main/java/com/gitlink/softbot/annotation/NoRepeatSubmit.java @@ -0,0 +1,14 @@ +package com.gitlink.softbot.annotation; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + + +@Target(ElementType.METHOD) // 作用到方法上 +@Retention(RetentionPolicy.RUNTIME) // 运行时有效 +public @interface NoRepeatSubmit { + +} + diff --git a/src/main/java/com/gitlink/softbot/annotation/UrlCache.java b/src/main/java/com/gitlink/softbot/annotation/UrlCache.java new file mode 100644 index 0000000..bade18c --- /dev/null +++ b/src/main/java/com/gitlink/softbot/annotation/UrlCache.java @@ -0,0 +1,21 @@ +package com.gitlink.softbot.annotation; + +import java.util.concurrent.TimeUnit; + +import com.google.common.cache.Cache; +import com.google.common.cache.CacheBuilder; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + + +/** + * @Description: 内存缓存配置类 + */ + +@Configuration +public class UrlCache { + @Bean + public Cache getCache() { + return CacheBuilder.newBuilder().expireAfterWrite(2L, TimeUnit.SECONDS).build();// 缓存有效期为2秒 + } +} diff --git a/src/main/java/com/gitlink/softbot/annotation/aop/NoRepeatSubmitAop.java b/src/main/java/com/gitlink/softbot/annotation/aop/NoRepeatSubmitAop.java new file mode 100644 index 0000000..ddea47c --- /dev/null +++ b/src/main/java/com/gitlink/softbot/annotation/aop/NoRepeatSubmitAop.java @@ -0,0 +1,61 @@ +package com.gitlink.softbot.annotation.aop; + +import javax.servlet.http.HttpServletRequest; + +import com.gitlink.softbot.annotation.NoRepeatSubmit; +import com.gitlink.softbot.global.exception.BotException; +import com.gitlink.softbot.global.vo.Result; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.aspectj.lang.ProceedingJoinPoint; +import org.aspectj.lang.annotation.Around; +import org.aspectj.lang.annotation.Aspect; +import org.aspectj.lang.annotation.Pointcut; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; +import org.springframework.web.context.request.RequestContextHolder; +import org.springframework.web.context.request.ServletRequestAttributes; + +import com.google.common.cache.Cache; + +/** + * @Description: aop解析注解-配合google的Cache缓存机制 + * @Author: Zoutao + * @Date: 2020/4/14 + */ +@Aspect +@Component +public class NoRepeatSubmitAop { + + private Log logger = LogFactory.getLog(getClass()); + + @Autowired + private Cache cache; + + @Pointcut("@annotation(noRepeatSubmit)") + public void pointCut(NoRepeatSubmit noRepeatSubmit) { + } + + @Around("pointCut(noRepeatSubmit)") + public Object around(ProceedingJoinPoint pjp, NoRepeatSubmit noRepeatSubmit) { + Result result = new Result<>(); + try { + ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); + String sessionId = RequestContextHolder.getRequestAttributes().getSessionId(); + HttpServletRequest request = attributes.getRequest(); + String key = sessionId + "-" + request.getServletPath(); + if (cache.getIfPresent(key) == null) {// 如果缓存中有这个url视为重复提交 + Object o = pjp.proceed(); + cache.put(key, 0); + return o; + } else { + logger.error("重复请求,请稍后再试!"); + return result.build(500).build("重复请求,请稍后再试!"); + } + } catch (Throwable e) { + e.printStackTrace(); + logger.error("验证重复提交时出现未知异常!"); + return result.build(500).build("验证重复提交时出现未知异常!"); + } + } +} diff --git a/src/main/java/com/gitlink/softbot/controller/user/UserController.java b/src/main/java/com/gitlink/softbot/controller/user/UserController.java index f817a60..3d68b5a 100644 --- a/src/main/java/com/gitlink/softbot/controller/user/UserController.java +++ b/src/main/java/com/gitlink/softbot/controller/user/UserController.java @@ -1,6 +1,7 @@ package com.gitlink.softbot.controller.user; import com.fasterxml.jackson.core.JsonProcessingException; +import com.gitlink.softbot.annotation.NoRepeatSubmit; import com.gitlink.softbot.global.exception.BotException; import com.gitlink.softbot.global.vo.Response; import com.gitlink.softbot.global.vo.Result; @@ -9,6 +10,7 @@ import com.gitlink.softbot.utils.AuthOperate; import com.gitlink.softbot.utils.GitLinkApi; import com.gitlink.softbot.vo.*; +import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.web.bind.annotation.*; import com.gitlink.softbot.vo.BotInputVO; import javax.annotation.Resource; @@ -181,6 +183,7 @@ public class UserController { * @return */ @PostMapping("/updateInstallBot") + @NoRepeatSubmit public Result updateInstallBot(@Valid @RequestBody UpdateInstallBotRequest updateInstallBotRequest) throws Exception{ Result result = new Result<>(); userService.updateInstallBot(updateInstallBotRequest);