经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » Java相关 » Java » 查看文章
java实现幂等性校验
来源:cnblogs  作者:名一  时间:2024/2/19 9:22:27  对本文有异议

我们在做web应用的时候通常会遇到前端提交按钮重复点击的场景,在某些新增操作上就需要做幂等性限制来保证数据的可靠性。下面来用java aop实现幂等性校验。

一:首先我们需要一个自定义注解

  1. package com.yuku.yuku_erp.annotation;
  2. import java.lang.annotation.*;
  3. /**
  4. * @author 名一
  5. * @ClassName IdempotentAnnotation
  6. * @description: 用来标记需要校验幂等性的接口
  7. * @datetime 2024年 02月 03日 14:48
  8. * @version: 1.0
  9. */
  10. @Target({ElementType.METHOD})
  11. @Retention(RetentionPolicy.RUNTIME)
  12. @Documented
  13. public @interface IdempotentAnnotation {
  14. String idempotentType();
  15. }

二:创建一个幂等校验的切面类

  1. package com.yuku.yuku_erp.aop;
  2. import com.yuku.yuku_erp.annotation.IdempotentAnnotation;
  3. import com.yuku.yuku_erp.constant.RedisKeyConstant;
  4. import com.yuku.yuku_erp.exception.MyException;
  5. import com.yuku.yuku_erp.utils.RedisShardedPoolUtil;
  6. import com.yuku.yuku_erp.utils.TokenUtil;
  7. import lombok.extern.slf4j.Slf4j;
  8. import org.aspectj.lang.JoinPoint;
  9. import org.aspectj.lang.annotation.Aspect;
  10. import org.aspectj.lang.annotation.Before;
  11. import org.aspectj.lang.annotation.Pointcut;
  12. import org.aspectj.lang.reflect.MethodSignature;
  13. import org.springframework.stereotype.Component;
  14. import java.lang.reflect.Method;
  15. /**
  16. * @author 名一
  17. * @ClassName CheckIdempotentAop
  18. * @description: 幂等性校验
  19. * @datetime 2024年 02月 03日 14:59
  20. * @version: 1.0
  21. */
  22. @Slf4j
  23. @Aspect
  24. @Component
  25. public class CheckIdempotentAop {
  26. @Pointcut("execution(* com.yuku.yuku_erp.controller..*.*(..))")
  27. public void checkCut(){
  28. }
  29. @Before("checkCut()")
  30. public void checkIdempotent(JoinPoint joinPoint){
  31. MethodSignature signature = (MethodSignature) joinPoint.getSignature();
  32. Method method = signature.getMethod();
  33. if (method.isAnnotationPresent(IdempotentAnnotation.class)){
  34. IdempotentAnnotation annotation = method.getAnnotation(IdempotentAnnotation.class);
  35. String idempotentType = annotation.idempotentType();
  36. String idempotentToken = TokenUtil.getRequest().getHeader("idempotentToken");
  37. String idemToken = idempotentType + idempotentToken;
  38. log.info("checkIdempotent idempotentType:{}, idempotentToken:{}", idempotentType, idempotentToken);
  39. Boolean flag = RedisShardedPoolUtil.sismember(RedisKeyConstant.IDEMPOTENT_TOKEN_LIST, idemToken);
  40. if (!flag){
  41. log.error("checkIdempotent error idempotentType:{}, idempotentToken:{}, flag:{}", idempotentType, idempotentToken, flag);
  42. throw new MyException("该接口已提交过,请不要重复提交");
  43. }
  44. RedisShardedPoolUtil.delSetByValue(RedisKeyConstant.IDEMPOTENT_TOKEN_LIST, idemToken);
  45. log.info("checkIdempotent idempotentType:{}, idempotentToken:{}, flag:{}", idempotentType, idempotentToken, flag);
  46. }
  47. }
  48. }

三:在需要切面的接口上使用幂等校验注解

  1. @IdempotentAnnotation(idempotentType = "checkIdempotentToken")
  2. @GetMapping("/checkIdempotentToken")
  3. @ApiOperation(value = "校验幂等性示例")
  4. public CommonResult<String> checkIdempotentToken(){
  5. return CommonResult.success();
  6. }

到此幂等校验就完成了

 

原文链接:https://www.cnblogs.com/mingyi-wang/p/18018873

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号