统一防止重复提交处理
依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
注解
import java.lang.annotation.*;
/**
* 防止重复提交
*/
@Documented
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Submit {
/**
* 有效期
*/
long time() default 1;
/**
* 提示信息
*/
String msg() default "请勿重复提交";
}
处理
import cn.dev33.satoken.stp.StpUtil;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
/**
* 提交方面
*/
@Aspect
@Slf4j
@Component
public class SubmitAspect {
private final static String SUBMIT_KEY = "submit:";
@Resource
private RedisService redisService;
@Pointcut("@annotation(cn.gcfc.admin.conf.submit.Submit)")
public void pointCut() {
}
/**
* 拦截
*
* @param pjp pjp
* @param submit 提交
* @return {@link Object}
* @throws Throwable throwable
*/
@Around("pointCut() && @annotation(submit)")
public Object intercept(ProceedingJoinPoint pjp, Submit submit) throws Throwable {
String key = generateKey();
if (redisService.hasKey(key)) {
throw new CustomException(submit.msg());
}
redisService.set(key, "", submit.time());
return pjp.proceed();
}
/**
* 生成key
*
* @return {@link String}
*/
private String generateKey() {
String path = HttpServletRequestUtil.getRequestURI();
return SUBMIT_KEY + StpUtil.getLoginId() + ":" + path;
}
}
使用
放在controller处理即可