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





    @Target(ElementType.METHOD)
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    @Inherited
    public @interface DuplicateSubmitToken {

    String key() default "";
    }





    @Aspect
    @Configuration
    public class LockMethodAop {

    private static final Cache<String, Object> CACHES = CacheBuilder.newBuilder()
    // 最大缓存 100 个
    .maximumSize(1000)
    // 设置写缓存后 5 秒钟过期
    .expireAfterWrite(1, TimeUnit.SECONDS)
    .build();

    @Autowired
    HttpServletRequest request;

    @Around("execution(* tz.lion.inv.manage.controller..*.*(..)) && @annotation(tz.lion.inv.manage.annotation.DuplicateSubmitToken)")
    public Object interceptor(ProceedingJoinPoint pjp) {
    MethodSignature signature = (MethodSignature) pjp.getSignature();
    Method method = signature.getMethod();
    DuplicateSubmitToken localLock = method.getAnnotation(DuplicateSubmitToken.class);
    String key = localLock.key()+request.getSession().getId();
    if (!StringUtils.isEmpty(key)) {
    if (CACHES.getIfPresent(key) != null) {
    throw new Exception(ErrorMsg.FORM_SUBMIT_REPEAT);
    }
    // 如果是第一次请求,就将 key 当前对象压入缓存中
    CACHES.put(key, key);
    }
    try {
    return pjp.proceed();
    } catch (Throwable throwable) {
    throw new RuntimeException("服务器异常");
    } finally {
    // CACHES.invalidate(key);
    }
    }
    }
  • 相关阅读:
    「BZOJ 1000」A+B Problem
    「HNOI 2008」越狱
    蓝桥杯 拼音字母
    蓝桥杯 抽签
    蓝桥杯 快速排序
    [蓝桥杯] 最大比例
    [蓝桥杯] 交换瓶子
    [蓝桥杯] 四平方和
    [蓝桥杯] 剪邮票
    [蓝桥杯] 方格填数
  • 原文地址:https://www.cnblogs.com/CaptainLin/p/14205438.html
Copyright © 2011-2022 走看看