防重复提交

This commit is contained in:
sxu 2024-08-18 07:29:12 +08:00
parent 298446b736
commit 23f2d59f1a
5 changed files with 15 additions and 49 deletions

View File

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

View File

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

View File

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

View File

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