From ca23b5ab33e02df78a5fc4eec102a01eb8a181ff Mon Sep 17 00:00:00 2001 From: Chopper711 Date: Fri, 28 Oct 2022 17:58:28 +0800 Subject: [PATCH] =?UTF-8?q?=E9=98=B2=E9=87=8D=E5=A4=8D=E6=8F=90=E4=BA=A4?= =?UTF-8?q?=E9=97=AE=E9=A2=98=C3=A5=C2=91=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../PreventDuplicateSubmissions.java | 7 ++- ...reventDuplicateSubmissionsInterceptor.java | 52 ++++++++++++++----- 2 files changed, 45 insertions(+), 14 deletions(-) diff --git a/framework/src/main/java/cn/lili/common/aop/annotation/PreventDuplicateSubmissions.java b/framework/src/main/java/cn/lili/common/aop/annotation/PreventDuplicateSubmissions.java index ba884dfe1..5ee36a051 100644 --- a/framework/src/main/java/cn/lili/common/aop/annotation/PreventDuplicateSubmissions.java +++ b/framework/src/main/java/cn/lili/common/aop/annotation/PreventDuplicateSubmissions.java @@ -18,7 +18,12 @@ /** - * 过期时间 + * 过期时间 默认3秒,即3秒内无法重复点击。 */ long expire() default 3; + /** + * 用户间隔离,默认false。 + * 如果为true则全局限制,为true需要用户登录状态,否则则是全局隔离 + */ + boolean userIsolation() default false; } diff --git a/framework/src/main/java/cn/lili/common/aop/interceptor/PreventDuplicateSubmissionsInterceptor.java b/framework/src/main/java/cn/lili/common/aop/interceptor/PreventDuplicateSubmissionsInterceptor.java index 9a8604442..33e2fde74 100644 --- a/framework/src/main/java/cn/lili/common/aop/interceptor/PreventDuplicateSubmissionsInterceptor.java +++ b/framework/src/main/java/cn/lili/common/aop/interceptor/PreventDuplicateSubmissionsInterceptor.java @@ -1,9 +1,19 @@ package cn.lili.common.aop.interceptor; +/** + * 防重复提交业务 + * + * @author Chopper + * @version v1.0 + * 2022-01-25 09:20 + */ + +import cn.hutool.json.JSONUtil; import cn.lili.cache.Cache; import cn.lili.common.aop.annotation.PreventDuplicateSubmissions; import cn.lili.common.enums.ResultCode; import cn.lili.common.exception.ServiceException; +import cn.lili.common.security.AuthUser; import cn.lili.common.security.context.UserContext; import lombok.extern.slf4j.Slf4j; import org.aspectj.lang.annotation.Aspect; @@ -15,13 +25,6 @@ import javax.servlet.http.HttpServletRequest; -/** - * 防重复提交业务 - * - * @author Chopper - * @version v1.0 - * 2022-01-25 09:20 - */ @Aspect @Component @Slf4j @@ -35,9 +38,11 @@ public class PreventDuplicateSubmissionsInterceptor { public void interceptor(PreventDuplicateSubmissions preventDuplicateSubmissions) { try { - Long count = cache.incr(getParams(), preventDuplicateSubmissions.expire()); - //如果超过2或者设置的参数,则表示重复提交了 - if (count.intValue() >= 2) { + String redisKey = getParams(preventDuplicateSubmissions.userIsolation()); + Long count = cache.incr(redisKey, preventDuplicateSubmissions.expire()); + log.debug("防重复提交:params-{},value-{}", redisKey, count); + //如果超过0或者设置的参数,则表示重复提交了 + if (count.intValue() > 0) { throw new ServiceException(ResultCode.LIMIT_ERROR); } } @@ -55,12 +60,33 @@ public void interceptor(PreventDuplicateSubmissions preventDuplicateSubmissions) /** * 获取表单参数 * - * @return + * @param userIsolation 用户是否隔离 + * @return 计数器key */ - private String getParams() { + private String getParams(Boolean userIsolation) { HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); + StringBuilder stringBuilder = new StringBuilder(); + //拼接请求地址 + stringBuilder.append(request.getRequestURI()); + + //参数不为空则拼接参数 + if (!request.getParameterMap().isEmpty()) { + stringBuilder.append(JSONUtil.toJsonStr(request.getParameterMap())); + } + //用户隔离设置为开启,则选择当前用回顾 + if (userIsolation) { + AuthUser authUser = UserContext.getCurrentUser(); + //用户为空则发出警告,但不拼接,否则拼接用户id + if (authUser == null) { + log.warn("user isolation settings are on,but current user is null"); + } +// 不为空则拼接用户id + else { + stringBuilder.append(authUser.getId()); + } + } //请求地址 - return request.getRequestURI() + UserContext.getCurrentUser().getId() + UserContext.getCurrentUser().getUsername(); + return stringBuilder.toString(); }