当前位置:   article > 正文

防重复提交注解

防重复提交注解

防重复提交 标记注解

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

防止重复提交切面类

  1. @Aspect
  2. @Component
  3. public class NoRepeatSubmitAop {
  4. @Autowired
  5. private RedisUtil redisUtil;
  6. /**
  7. * 调用controller包下的任意类的任意方法时均会调用此方法
  8. */
  9. @Around("execution(public * com.ciip.cloud.core.usercenter.controller..*.*(..)) && @annotation(nrs)")
  10. public Object arround(ProceedingJoinPoint pjp, NoRepeatSubmit nrs) {
  11. try {
  12. ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
  13. String sessionId = RequestContextHolder.getRequestAttributes().getSessionId();
  14. HttpServletRequest request = attributes.getRequest();
  15. String key ="-" + request.getServletPath();
  16. // 如果缓存中有这个url视为重复提交
  17. if (redisUtil.get(sessionId,key) == null) {
  18. redisUtil.setWithExpireTime(sessionId,key,"0",2);
  19. Console.log("请求=========" + sessionId + key);
  20. //继续下一个通知或目标方法调用
  21. Object o = pjp.proceed();
  22. return o;
  23. } else {
  24. Console.log("重复请求=========" + sessionId + key);
  25. return ResultUtil.error(UserCenterStatusCode.REPEAT_SUBMIT.getCode(),UserCenterStatusCode.REPEAT_SUBMIT.getMsg());
  26. }
  27. } catch (Throwable e) {...}
  28. }
  29. }

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

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/234320
推荐阅读
相关标签
  

闽ICP备14008679号