From 2d97dbfd6005c6824d25b0f625d7bea22f146294 Mon Sep 17 00:00:00 2001 From: zhaoqifeng Date: Tue, 24 Nov 2020 17:01:42 +0800 Subject: [PATCH] =?UTF-8?q?=E9=98=B2=E6=AD=A2=E9=87=8D=E5=A4=8D=E6=8F=90?= =?UTF-8?q?=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../commons/tools/aop/NoRepeatSubmit.java | 17 ++++ .../commons/tools/aop/NoRepeatSubmitAop.java | 82 +++++++++++++++++++ .../tools/exception/EpmetErrorCode.java | 2 +- .../com/epmet/controller/BadgeController.java | 3 + 4 files changed, 103 insertions(+), 1 deletion(-) create mode 100644 epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/aop/NoRepeatSubmit.java create mode 100644 epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/aop/NoRepeatSubmitAop.java diff --git a/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/aop/NoRepeatSubmit.java b/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/aop/NoRepeatSubmit.java new file mode 100644 index 0000000000..aad5ee733a --- /dev/null +++ b/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/aop/NoRepeatSubmit.java @@ -0,0 +1,17 @@ +package com.epmet.commons.tools.aop; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * @author zhaoqifeng + * @dscription + * @date 2020/11/24 9:56 + */ +@Target(ElementType.METHOD) +@Retention(RetentionPolicy.RUNTIME) +public @interface NoRepeatSubmit { + +} \ No newline at end of file diff --git a/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/aop/NoRepeatSubmitAop.java b/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/aop/NoRepeatSubmitAop.java new file mode 100644 index 0000000000..42f1950d5f --- /dev/null +++ b/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/aop/NoRepeatSubmitAop.java @@ -0,0 +1,82 @@ +package com.epmet.commons.tools.aop; + +import com.epmet.commons.tools.constant.NumConstant; +import com.epmet.commons.tools.exception.EpmetErrorCode; +import com.epmet.commons.tools.exception.RenException; +import com.google.common.cache.Cache; +import com.google.common.cache.CacheBuilder; +import lombok.extern.slf4j.Slf4j; +import org.aspectj.lang.JoinPoint; +import org.aspectj.lang.ProceedingJoinPoint; +import org.aspectj.lang.annotation.AfterThrowing; +import org.aspectj.lang.annotation.Around; +import org.aspectj.lang.annotation.Aspect; +import org.aspectj.lang.annotation.Before; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.context.request.RequestContextHolder; +import org.springframework.web.context.request.ServletRequestAttributes; + +import javax.servlet.http.HttpServletRequest; +import java.lang.reflect.Method; +import java.util.Objects; +import java.util.concurrent.TimeUnit; + +/** + * @author zhaoqifeng + * @dscription + * @date 2020/11/24 9:59 + */ +@Aspect +@Configuration +@Slf4j +public class NoRepeatSubmitAop { + + + /** + * 重复提交判断时间为2s + */ + private static final Cache CACHES = CacheBuilder.newBuilder() + // 最大缓存 100 个 + .maximumSize(1000) + // 设置写缓存后 5 秒钟过期 + .expireAfterWrite(5, TimeUnit.SECONDS) + .build(); + @Before("execution(public * com.epmet..*Controller.*(..))") + public void before(JoinPoint joinPoint) { + System.out.println(joinPoint.getSignature().getName()); + } + + @Around("execution(public * com.epmet..*Controller.*(..)) && @annotation(com.epmet.commons.tools.aop.NoRepeatSubmit)") + public Object around(ProceedingJoinPoint pjp) { + try { + ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); + assert attributes != null; + HttpServletRequest request = attributes.getRequest(); + String key = getKey(request.getRequestURI(), pjp.getArgs()); + // 如果缓存中有这个url视为重复提交 + if (CACHES.getIfPresent(key) == null) { + Object o = pjp.proceed(); + CACHES.put(key, NumConstant.ZERO); + return o; + } else { + log.error("重复提交"); + throw new RenException(EpmetErrorCode.REPEATED_SUBMIT_ERROR.getCode()); + } + } catch (RenException e) { + throw e; + } catch (Throwable e) { + log.error("验证重复提交时出现未知异常!"); + throw new RenException(EpmetErrorCode.SERVER_ERROR.getCode()); + } + + } + + private String getKey(String keyExpress, Object[] args) { + for (int i = 0; i < args.length; i++) { + keyExpress = keyExpress.replace("arg[" + i + "]", args[i].toString()); + } + return keyExpress; + } + + +} \ No newline at end of file diff --git a/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/exception/EpmetErrorCode.java b/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/exception/EpmetErrorCode.java index a1ead085c7..0638929b20 100644 --- a/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/exception/EpmetErrorCode.java +++ b/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/exception/EpmetErrorCode.java @@ -123,7 +123,7 @@ public enum EpmetErrorCode { // 党建声音 前端提示 88段 DRAFT_CONTENT_IS_NULL(8801, "至少需要添加一个段落"), ARTICLE_PUBLISH_ERROR(8801, "发布文章失败,请刷新重试"), - + REPEATED_SUBMIT_ERROR(8998, "请勿重复提交"), CUSTOMER_VALIDATE_ERROR(8999, "内部数据校验异常"), //公众号 865..开头的码 diff --git a/epmet-user/epmet-user-server/src/main/java/com/epmet/controller/BadgeController.java b/epmet-user/epmet-user-server/src/main/java/com/epmet/controller/BadgeController.java index f4e2c3feee..edf52d1f97 100644 --- a/epmet-user/epmet-user-server/src/main/java/com/epmet/controller/BadgeController.java +++ b/epmet-user/epmet-user-server/src/main/java/com/epmet/controller/BadgeController.java @@ -1,6 +1,7 @@ package com.epmet.controller; import com.epmet.commons.tools.annotation.LoginUser; +import com.epmet.commons.tools.aop.NoRepeatSubmit; import com.epmet.commons.tools.security.dto.TokenDto; import com.epmet.commons.tools.utils.Result; import com.epmet.commons.tools.validator.ValidatorUtils; @@ -82,6 +83,7 @@ public class BadgeController { * @return com.epmet.commons.tools.utils.Result */ @PostMapping("edit") + @NoRepeatSubmit public Result edit(@LoginUser TokenDto tokenDto, @RequestBody EditBadgeFormDTO formDTO) { ValidatorUtils.validateEntity(formDTO); badgeService.edit(tokenDto, formDTO); @@ -147,6 +149,7 @@ public class BadgeController { * @return com.epmet.commons.tools.utils.Result */ @PostMapping("audit") + @NoRepeatSubmit public Result audit(@LoginUser TokenDto tokenDto, @RequestBody BadgeAuditFormDTO formDTO) { ValidatorUtils.validateEntity(formDTO); badgeService.audit(tokenDto, formDTO);