zoukankan      html  css  js  c++  java
  • 防止重复提交

    /**
             * 防止重复提交
             */
            @Aspect
            @Component
            public class AvoidRepeatableCommitAspect {
            
                @Autowired
                private RedisTemplate redisTemplate;
            
                /**
                 * @param point
                 */
                @Around("@annotation(com.xwolf.boot.annotation.AvoidRepeatableCommit)")
                public Object around(ProceedingJoinPoint point) throws Throwable {
            
                    HttpServletRequest request  = ((ServletRequestAttributes)RequestContextHolder.currentRequestAttributes()).getRequest();
                    String ip = IPUtil.getIP(request);
                    //获取注解
                    MethodSignature signature = (MethodSignature) point.getSignature();
                    Method method = signature.getMethod();
                    //目标类、方法
                    String className = method.getDeclaringClass().getName();
                    String name = method.getName();
                    String ipKey = String.format("%s#%s",className,name);
                    int hashCode = Math.abs(ipKey.hashCode());
                    String key = String.format("%s_%d",ip,hashCode);
                    log.info("ipKey={},hashCode={},key={}",ipKey,hashCode,key);
                    AvoidRepeatableCommit avoidRepeatableCommit =  method.getAnnotation(AvoidRepeatableCommit.class);
                    long timeout = avoidRepeatableCommit.timeout();
                    if (timeout < 0){
                                    //过期时间5分钟
                        timeout = 60*5;
                    }
                    String value = (String) redisTemplate.opsForValue().get(key);
                    if (StringUtils.isNotBlank(value)){
                        return "请勿重复提交";
                    }
                    redisTemplate.opsForValue().set(key, UUIDUtil.uuid(),timeout,TimeUnit.MILLISECONDS);
                    //执行方法
                    Object object = point.proceed();
                    return object;
                }
            
            }

     

  • 相关阅读:
    生成器
    迭代器
    闭包函数
    装饰器(2)
    装饰器(1)
    名称空间与作用域(2)
    110.网络编程-mail
    109.网络编程-FTP
    108.网络编程-TCP/UDP
    107.xpath
  • 原文地址:https://www.cnblogs.com/sidesky/p/12877318.html
Copyright © 2011-2022 走看看