zoukankan      html  css  js  c++  java
  • AOP实现防止接口重复提交

    项目中对于状态变更接口存在重复提交的问题。

    package com.yxx.survey.foundation.aop;
    
    import com.alibaba.fastjson.JSON;
    import com.yxx.survey.foundation.exception.ErrorEnum;
    import com.yxx.survey.foundation.exception.excetion.BusinessException;
    import com.yxx.survey.foundation.redis.RedisService;
    import com.yxx.survey.foundation.util.encryption.HashUtil;
    import lombok.extern.slf4j.Slf4j;
    import org.aspectj.lang.JoinPoint;
    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.Before;
    import org.aspectj.lang.annotation.Pointcut;
    import org.springframework.stereotype.Component;
    
    import javax.annotation.Resource;
    
    
    @Aspect
    @Component
    @Slf4j
    public class RepeatSubmitAspect {
        @Resource
        private RedisService redisService;
    
        @Pointcut("@annotation(RepeatSubmitCheck)")
        public void requestPointcut() {
        }
    
        @Before("requestPointcut() && @annotation(repeatSubmitCheck)")
        public void aroundCheck(JoinPoint joinPoint, RepeatSubmitCheck repeatSubmitCheck) {
            String className = joinPoint.getTarget().getClass().getName();
    
            String methodName = joinPoint.getSignature().getName();
    
            Object[] args = joinPoint.getArgs();
            String paramsJsonStr = JSON.toJSONString(args);
    
            String srcStr = className + "_" + methodName + "_" + paramsJsonStr;
            log.info(srcStr);
    
            String signStr = HashUtil.MD5(srcStr);
    
            if (!redisService.setIfNotExists(signStr, "1", repeatSubmitCheck.keepSeconds())) {
                throw BusinessException.with(ErrorEnum.INTERFACE_INVOKE_FREQUENTLY);
            }
        }
    }
    package com.yxx.survey.foundation.aop;
    
    import java.lang.annotation.*;
    
    @Target(ElementType.METHOD)
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    public @interface RepeatSubmitCheck {
        int keepSeconds() default 1;
    }

     

  • 相关阅读:
    80x86的保护模式
    计算机二进制的表示
    操作系统基本知识(一)
    记录一次在安装双系统的过程(先有debian, 后加windows 8.1)
    LitePal + Gson + Volley的ORM框架尝试方案
    如何使用DDMS Heap查看Android应用内存情况
    测试驱动开发的第一个例子---我的毕业设计
    策略模式的孪生兄弟---状态模式
    面试常备---栈和队列总结篇
    面试常备题---二叉树总结篇
  • 原文地址:https://www.cnblogs.com/watson-ljf/p/10751990.html
Copyright © 2011-2022 走看看