Revert "防重复提交"

This reverts commit 23f2d59f1a.
This commit is contained in:
sxu 2024-08-18 07:40:26 +08:00
parent 24852e5b2e
commit c405a96779
5 changed files with 49 additions and 15 deletions

View File

@ -0,0 +1,28 @@
package com.bonus.sgzb.common.core.exception;
import com.bonus.sgzb.common.core.utils.HttpCodeEnum;
public class BusinessException extends RuntimeException {
private int code;
//使用枚举构造
public BusinessException(HttpCodeEnum httpCodeEnum){
super(httpCodeEnum.getMsg());
this.code=httpCodeEnum.getCode();
}
//使用自定义消息体
public BusinessException(HttpCodeEnum httpCodeEnum, String msg){
super(msg);
this.code=httpCodeEnum.getCode();
}
//根据异常构造
public BusinessException(HttpCodeEnum httpCodeEnum, Throwable msg){
super(msg);
this.code=httpCodeEnum.getCode();
}
}

View File

@ -1,7 +1,5 @@
package com.bonus.sgzb.common.core.exception; package com.bonus.sgzb.common.core.exception;
import com.bonus.sgzb.common.core.enums.HttpCodeEnum;
/** /**
* 业务异常 * 业务异常
* *
@ -24,6 +22,7 @@ public final class ServiceException extends RuntimeException
/** /**
* 错误明细内部调试错误 * 错误明细内部调试错误
* *
* {@link CommonResult#getDetailMessage()} 一致的设计
*/ */
private String detailMessage; private String detailMessage;
@ -34,11 +33,6 @@ public final class ServiceException extends RuntimeException
{ {
} }
public ServiceException(HttpCodeEnum httpCodeEnum){
super(httpCodeEnum.getMsg());
this.code=httpCodeEnum.getCode();
}
public ServiceException(String message) public ServiceException(String message)
{ {
this.message = message; this.message = message;

View File

@ -1,4 +1,4 @@
package com.bonus.sgzb.common.core.enums; package com.bonus.sgzb.common.core.utils;
public enum HttpCodeEnum { public enum HttpCodeEnum {
// 成功 // 成功
@ -19,7 +19,7 @@ public enum HttpCodeEnum {
EMAIL_NOT_NULL(511, "邮箱不能为空"), EMAIL_NOT_NULL(511, "邮箱不能为空"),
NICKNAME_EXIST(512, "昵称已存在"), NICKNAME_EXIST(512, "昵称已存在"),
LOGIN_ERROR(505, "用户名或密码错误"), LOGIN_ERROR(505, "用户名或密码错误"),
REPEATE_ERROR(599, "不允许重复提交,请稍候再试"); REPEATE_ERROR(600, "不允许重复提交,请稍候再试");
int code; int code;
String msg; String msg;

View File

@ -2,8 +2,8 @@ package com.bonus.sgzb.common.aspect;
import com.alibaba.fastjson2.JSON; import com.alibaba.fastjson2.JSON;
import com.bonus.sgzb.common.annotation.PreventRepeatSubmit; import com.bonus.sgzb.common.annotation.PreventRepeatSubmit;
import com.bonus.sgzb.common.core.enums.HttpCodeEnum; import com.bonus.sgzb.common.core.exception.BusinessException;
import com.bonus.sgzb.common.core.exception.ServiceException; import com.bonus.sgzb.common.core.utils.HttpCodeEnum;
import com.bonus.sgzb.common.util.RedisCache; import com.bonus.sgzb.common.util.RedisCache;
import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Around;
@ -24,7 +24,7 @@ import java.util.concurrent.TimeUnit;
@Component @Component
public class PreventRepeatSubmitAspect { public class PreventRepeatSubmitAspect {
private static final Logger LOG = LoggerFactory.getLogger(PreventRepeatSubmitAspect.class); private static final Logger LOG = LoggerFactory.getLogger(PreventRepeatSubmitAspect.class);
private static final String HEAD_AUTHORIZATION = "Authorization"; private static final String header = "Authorization";
@Autowired @Autowired
private RedisCache redisCache; private RedisCache redisCache;
@ -37,21 +37,32 @@ public class PreventRepeatSubmitAspect {
@Around("preventRepeatSubmit()") @Around("preventRepeatSubmit()")
public Object checkPrs(ProceedingJoinPoint pjp) throws Throwable { public Object checkPrs(ProceedingJoinPoint pjp) throws Throwable {
LOG.info("进入preventRepeatSubmit切面");
//得到request对象
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
String requestURI = request.getRequestURI(); String requestURI = request.getRequestURI();
LOG.info("防重复提交的请求地址:{} ,请求方式:{}",requestURI,request.getMethod());
LOG.info("防重复提交拦截到的类名:{} ,方法:{}",pjp.getTarget().getClass().getSimpleName(),pjp.getSignature().getName());
//获取请求参数
Object[] args = pjp.getArgs(); Object[] args = pjp.getArgs();
String argStr = JSON.toJSONString(args); String argStr = JSON.toJSONString(args);
//这里替换是为了在redis可视化工具中方便查看
argStr=argStr.replace(":","#"); argStr=argStr.replace(":","#");
String submitKey = request.getHeader(HEAD_AUTHORIZATION).trim(); // 唯一值没有消息头则使用请求地址
String submitKey = request.getHeader(header).trim();
// 唯一标识指定key + url +参数+token
String cacheRepeatKey = "repeat_submit:" + requestURI+":" +argStr+":"+ submitKey; String cacheRepeatKey = "repeat_submit:" + requestURI+":" +argStr+":"+ submitKey;
MethodSignature ms = (MethodSignature) pjp.getSignature(); MethodSignature ms = (MethodSignature) pjp.getSignature();
Method method=ms.getMethod(); Method method=ms.getMethod();
PreventRepeatSubmit preventRepeatSubmit=method.getAnnotation(PreventRepeatSubmit.class); PreventRepeatSubmit preventRepeatSubmit=method.getAnnotation(PreventRepeatSubmit.class);
int interval = preventRepeatSubmit.interval(); int interval = preventRepeatSubmit.interval();
LOG.info("获取到preventRepeatSubmit的有效期时间"+interval); LOG.info("获取到preventRepeatSubmit的有效期时间"+interval);
//redis分布式锁
Boolean aBoolean = redisCache.setNxCacheObject(cacheRepeatKey, 1, preventRepeatSubmit.interval(), TimeUnit.SECONDS); Boolean aBoolean = redisCache.setNxCacheObject(cacheRepeatKey, 1, preventRepeatSubmit.interval(), TimeUnit.SECONDS);
//aBoolean为true则证明没有重复提交
if(!aBoolean){ if(!aBoolean){
throw new ServiceException(HttpCodeEnum.REPEATE_ERROR); throw new BusinessException(HttpCodeEnum.REPEATE_ERROR);
} }
return pjp.proceed(); return pjp.proceed();
} }

View File

@ -1,8 +1,9 @@
package com.bonus.sgzb.system.aspect; package com.bonus.sgzb.system.aspect;
import com.alibaba.fastjson2.JSON; import com.alibaba.fastjson2.JSON;
import com.bonus.sgzb.common.core.enums.HttpCodeEnum; import com.bonus.sgzb.common.core.utils.HttpCodeEnum;
import com.bonus.sgzb.system.annotation.PreventRepeatSubmit; import com.bonus.sgzb.system.annotation.PreventRepeatSubmit;
import com.bonus.sgzb.common.core.exception.BusinessException;
import com.bonus.sgzb.system.util.RedisCache; import com.bonus.sgzb.system.util.RedisCache;
import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Around;