From d714b9deb38e38365eb0463abbc820028a08737c Mon Sep 17 00:00:00 2001 From: "[li_she]" <[li.she@xujiesoft.com]> Date: Wed, 26 Oct 2022 15:13:21 +0800 Subject: [PATCH] =?UTF-8?q?alteams=20=E4=B8=8A=E7=BA=BF=E7=89=88=E6=9C=AC?= =?UTF-8?q?=202022=E5=B9=B410=E6=9C=8826=E6=97=A5=20sxm?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/aop/RepeatSubmitAspect.java | 107 +++++++++--------- .../impl/OrderTimeHistServiceImpl.java | 22 ++-- 2 files changed, 61 insertions(+), 68 deletions(-) diff --git a/src/main/java/com/alteams/common/aop/RepeatSubmitAspect.java b/src/main/java/com/alteams/common/aop/RepeatSubmitAspect.java index 6671c67..d8f9ca4 100644 --- a/src/main/java/com/alteams/common/aop/RepeatSubmitAspect.java +++ b/src/main/java/com/alteams/common/aop/RepeatSubmitAspect.java @@ -1,11 +1,9 @@ package com.alteams.common.aop; -import java.util.concurrent.TimeUnit; - -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - +import com.alteams.common.exception.XJException; import com.alteams.modules.sys.entity.SysUserEntity; +import com.google.common.cache.Cache; +import com.google.common.cache.CacheBuilder; import org.apache.commons.lang3.StringUtils; import org.apache.shiro.SecurityUtils; import org.aspectj.lang.ProceedingJoinPoint; @@ -16,61 +14,58 @@ 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; -import com.google.common.cache.CacheBuilder; +import javax.servlet.http.HttpServletRequest; +import java.util.concurrent.TimeUnit; -/** - * @ClassName: RepeatSubmitAspect - * @Description: 自定义注释切点 - * @author LR - * @date 2020年4月17日 - * @version V1.0 +/** + * @author LR + * @version V1.0 + * @ClassName: RepeatSubmitAspect + * @Description: 自定义注释切点 + * @date 2020年4月17日 */ @Component @Aspect public class RepeatSubmitAspect { - //缓存数据存放 - private static final Cache caches = CacheBuilder.newBuilder() - .maximumSize(10000).expireAfterWrite(10, TimeUnit.SECONDS).build(); - - //切点 - @Pointcut("@annotation(com.alteams.common.annotation.RepeatSubmit)") - public void pointCut() {} - - //处理业务逻辑 - @Around("pointCut()") - public Object submitAop(ProceedingJoinPoint pjp) throws Exception { - //获取request - ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); - HttpServletRequest request = attributes.getRequest(); - String url = request.getRequestURI(); - SysUserEntity currentUser = (SysUserEntity) SecurityUtils.getSubject().getPrincipal(); - //判断是否是否null - if(null == currentUser) { - HttpServletResponse response = attributes.getResponse(); - response.sendError(HttpServletResponse.SC_FORBIDDEN, "Session is expired!"); - return response; - } - String username = currentUser.getUsername(); - //根据用户id+url作为唯一识别的请求 - String key = username + "-" + url; - //空值校验 - if(!StringUtils.isEmpty(key)) { - //判断是否患有当前的key - if(caches.getIfPresent(key) == null) { - caches.put(key, key); - }else { - HttpServletResponse response = attributes.getResponse(); - response.sendError(500, "重复提交!"); - return response; - } - } - //通过检验继续操作 - try { - return pjp.proceed(); - } catch (Throwable e) { - throw new RuntimeException(e.getMessage()); - } - } + //缓存数据存放 后续要改小时间 目前十秒钟 + private static final Cache caches = CacheBuilder.newBuilder() + .maximumSize(10000).expireAfterWrite(5, TimeUnit.SECONDS).build(); + + //切点 + @Pointcut("@annotation(com.alteams.common.annotation.RepeatSubmit)") + public void pointCut() { + } + + //处理业务逻辑 + @Around("pointCut()") + public Object submitAop(ProceedingJoinPoint pjp) throws Exception { + //获取request + ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); + HttpServletRequest request = attributes.getRequest(); + String url = request.getRequestURI(); + SysUserEntity currentUser = (SysUserEntity) SecurityUtils.getSubject().getPrincipal(); + //判断是否是否null + if (null == currentUser) { + throw new XJException("Session is expired!", 500); + } + String username = currentUser.getUsername(); + //根据用户id+url作为唯一识别的请求 + String key = username + "-" + url; + //空值校验 + if (!StringUtils.isEmpty(key)) { + //判断是否患有当前的key + if (caches.getIfPresent(key) == null) { + caches.put(key, key); + } else { + throw new XJException("重复提交!", 500); + } + } + //通过检验继续操作 + try { + return pjp.proceed(); + } catch (Throwable e) { + throw new XJException(e.getMessage()); + } + } } \ No newline at end of file diff --git a/src/main/java/com/alteams/modules/shoporder/service/impl/OrderTimeHistServiceImpl.java b/src/main/java/com/alteams/modules/shoporder/service/impl/OrderTimeHistServiceImpl.java index 80b95d3..4ad5d59 100644 --- a/src/main/java/com/alteams/modules/shoporder/service/impl/OrderTimeHistServiceImpl.java +++ b/src/main/java/com/alteams/modules/shoporder/service/impl/OrderTimeHistServiceImpl.java @@ -109,7 +109,7 @@ public class OrderTimeHistServiceImpl extends ServiceImpl