1. 自定义注解 RepeatSubmit
package com.dechnic.oms.common.annotation;
import java.lang.annotation.*;
@Inherited
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface RepeatSubmit
{
public int interval() default 3000;
String value() default "value" ;
public String message() default "不允许重复提交,请稍候再试";
}
2. Aspect AOP 定义 NoRepeatSubmitAspect
package com.dechnic.oms.framework.aspectj;
import com.dechnic.oms.common.annotation.RepeatSubmit;
import com.dechnic.oms.common.constant.CacheConstants;
import com.dechnic.oms.common.constant.ExceptionEnum;
import com.dechnic.oms.common.core.redis.RedisCache;
import com.dechnic.oms.common.exception.ServiceException;
import com.dechnic.oms.common.utils.MapperUtil;
import com.dechnic.oms.framework.service.TokenService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
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;
@Slf4j
@Component
@Aspect
public class NoRepeatSubmitAspect {
@Autowired
private RedisCache redisCache;
@Autowired
private TokenService tokenService;
@Pointcut("@annotation(com.dechnic.oms.common.annotation.RepeatSubmit)")
public void preventDuplication(){
};
@Around("preventDuplication()")
public Object around(ProceedingJoinPoint joinPoint) throws Exception{
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
Method method = ((MethodSignature) joinPoint.getSignature()).getMethod();
RepeatSubmit annotation = method.getAnnotation(RepeatSubmit.class);
String token = tokenService.getToken(request);
String url = request.getRequestURI();
String redisKey = CacheConstants.REPEAT_SUBMIT_KEY
.concat(url)
.concat(token)
.concat(getMethodSign(method,joinPoint.getArgs()));
String redisValue = redisKey.concat(annotation.value()).concat("submit duplication");
if (!redisCache.hasKey(redisKey)){
redisCache.setCacheObject(redisKey,redisValue,annotation.interval(), TimeUnit.MILLISECONDS);
try {
return joinPoint.proceed();
} catch (Throwable throwable) {
redisCache.deleteObject(redisKey);
throw new ServiceException(throwable.getMessage());
}
}else {
throw new ServiceException(ExceptionEnum.REPEAT_SUBMIT.getError(),ExceptionEnum.REPEAT_SUBMIT.getCode());
}
}
private String getMethodSign(Method method, Object... args) {
StringBuilder sb = new StringBuilder(method.toString());
for (Object arg : args) {
sb.append(toString(arg));
}
return DigestUtils.sha1Hex(sb.toString());
}
private String toString(Object arg) {
if (Objects.isNull(arg)) {
return "null";
}
if (arg instanceof Number) {
return arg.toString();
}
return MapperUtil.objectToJson(arg);
}
}
3. 在所需要添加防重复校验的地方添加注解
![在这里插入图片描述](https://img-blog.csdnimg.cn/20890821f19b491d89217f5305c6070b.png)