防重复提交注解

防重复提交 标记注解

@Target(ElementType.METHOD) // 作用到方法上
@Retention(RetentionPolicy.RUNTIME) // 运行时有效
public @interface NoRepeatSubmit {
}

防止重复提交切面类

@Aspect
@Component
public class NoRepeatSubmitAop {

    @Autowired
    private RedisUtil redisUtil;
	 /**
     * 调用controller包下的任意类的任意方法时均会调用此方法
     */
    @Around("execution(public * com.ciip.cloud.core.usercenter.controller..*.*(..)) && @annotation(nrs)")
    public Object arround(ProceedingJoinPoint pjp, NoRepeatSubmit nrs) {
        try {
            ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
            String sessionId = RequestContextHolder.getRequestAttributes().getSessionId();
            HttpServletRequest request = attributes.getRequest();
            String key ="-" + request.getServletPath();
            // 如果缓存中有这个url视为重复提交
            if (redisUtil.get(sessionId,key) == null) {
                redisUtil.setWithExpireTime(sessionId,key,"0",2);
                Console.log("请求=========" + sessionId + key);
                //继续下一个通知或目标方法调用
                Object o = pjp.proceed();
                return o;
            } else {
                Console.log("重复请求=========" + sessionId + key);
                return ResultUtil.error(UserCenterStatusCode.REPEAT_SUBMIT.getCode(),UserCenterStatusCode.REPEAT_SUBMIT.getMsg());
            }
        } catch (Throwable e) {...}
    }
}

实例:在Controller层上加上注解@NoRepeatSubmit,即可

    @NoRepeatSubmit
    @PostMapping("/xx")
    public ResponseMessage againJoinCooperationRecord(){...}

你可能感兴趣的:(spring,mybatis,java)