zoukankan      html  css  js  c++  java
  • aop 幂等验证(二)

    1 创建IIdempotent

    @Target({ElementType.PARAMETER, ElementType.METHOD})
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    public @interface IIdempotent {
    }

    2 创建aop

    @Component
    @Aspect
    public class IdempotentAction {
     
        public final static String ERROR_REPEATSUBMIT = "Repeated submission";
     
        //redis
        @Autowired
        protected StringRedisTemplate idempotentTemplate;
     
        //配置接入点,如果不知道怎么配置,可以百度一下规则
        @Pointcut("execution(* com.kps.webAPI*.controller..*.*(..))&&@annotation(com.kps.web.aop.Idempotent.IIdempotent)")
        private void controllerAspect() {
        }
     
        //成功处理请求
        @AfterReturning("controllerAspect()")
        public void AfterReturning(JoinPoint jp) throws Exception {
            IdempotentCheck(jp, ComContants.OP_RESULT[1]);
        }
     
     
        //后置异常通知
        @AfterThrowing("controllerAspect()")
        public void AfterThrowing(JoinPoint jp) throws Exception{
            IdempotentCheck(jp,ComContants.OP_RESULT[2]);
        }
     
        private void IdempotentCheck(JoinPoint jp, String opResult) throws Exception {
            String controllerName = jp.getTarget().getClass().getName();
            controllerName = controllerName.substring(controllerName.lastIndexOf(".") + 1);
            Signature sig = jp.getSignature();
            MethodSignature msig = (MethodSignature) sig;
            // 获得被拦截的方法
            Method method = msig.getMethod();
            // webUI控制器 @Controller注解
            IIdempotent systemlog = method.getAnnotation(IIdempotent.class);
            HttpServletRequest request = WebUtil.getHttpServletRequest();
            String body = NetUtil.getBodyString(request);
            String signMD5= MD5.md5(body);
            if (Boolean.parseBoolean(idempotentTemplate.opsForValue().get(signMD5))) {
                throw new ErrorSignException(ERROR_REPEATSUBMIT);
            } else {
                idempotentTemplate.opsForValue().set(signMD5, "true", ComContants.IDEMPOTENT_EXTIME, TimeUnit.SECONDS);
            }
     
        }
    }

    测试:

    //幂等apo,测试实例,30秒不可重复提交相同数据
        @IIdempotent
        @RequestMapping(value = "test", method = RequestMethod.POST)
        public ApiResult<String> test(@RequestBody ApiRequest<String> requestVO) {
            ApiResult<String> r = new ApiResult<String>();
            r.setData(requestVO.getData());
            r.setCodeToSuccessed();
            return r;
        }
  • 相关阅读:
    LeetCode 35 搜索插入位置
    LeetCode 69 x 的平方根
    LeetCode 61 旋转链表
    LeetCode 876 链表的中间结点
    LeetCode 142 环形链表 II
    LeetCode 206 反转链表
    LeetCode 237 删除链表中的节点
    LeetCode 83 删除排序链表中的重复元素
    元素的隐藏与显示与判断 js jquery aspx.cs
    判断对象是否为空 js与Jquery区别
  • 原文地址:https://www.cnblogs.com/hanjun0612/p/10768482.html
Copyright © 2011-2022 走看看