相应的切面
@Slf4j
@Aspect
@Component
public class RepeatSubmitAspect {
@Pointcut("@annotation(noRepeatSubmit)")
public void pointCut(NoRepeatSubmit noRepeatSubmit) {
}
@Around("pointCut(noRepeatSubmit)")
public ResultNoEncryptVo around(ProceedingJoinPoint pjp, NoRepeatSubmit noRepeatSubmit) throws Throwable {
int lockSeconds = noRepeatSubmit.lockTime();
ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = requestAttributes.getRequest();
Assert.notNull(request, "request can not null");
String token = request.getHeader("CQ-TOKEN");
String path = request.getServletPath();
String key = getKey(token, path);
String s = RedisUtils.get(key);
log.info("Set Redis = [{}]", key);
if (s == null) {
log.info("getRedisKey == null , key = [{}]", key);
RedisUtils.set(key,"datas");
RedisUtils.expire(key,lockSeconds, TimeUnit.SECONDS);
pjp.proceed();
return ResultNoEncryptVo.ok("你已提交成功");
} else {
log.info("重复提交, key = [{}]", key);
return ResultNoEncryptVo.ok("亲,请不要重复提交哦");
}
}
private String getKey(String token, String path) {
return token + path;
}
}
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface NoRepeatSubmit {
int lockTime() default 3;
}
|