zoukankan      html  css  js  c++  java
  • SpringBoot 自定义注解

     新增注解类

    NotRepeatSubmit.java
    package com.example.demo.annotation;
    
    import java.lang.annotation.ElementType;
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    import java.lang.annotation.Target;
    
    /**
     * 自定义注解,防止重复提交。
     */
    @Target(ElementType.METHOD)
    @Retention(RetentionPolicy.RUNTIME)
    public @interface NotRepeatSubmit {
    
        /** 过期时间,单位毫秒 **/
        long value() default 5000;
    }

    ApiUti.java

    package com.example.demo.annotation;
    
    import com.example.demo.token.NotRepeatSubmit;
    import org.springframework.messaging.handler.HandlerMethod;
    
    import java.lang.reflect.Method;
    
    /**
     * API工具类
     */
    public class ApiUtil {
    
    
        /**
         * 获取方法上的@NotRepeatSubmit注解
         *
         * @param handler
         * @return
         */
        public static NotRepeatSubmit getNotRepeatSubmit(Object handler) {
            if (handler instanceof HandlerMethod) {
                HandlerMethod handlerMethod = (HandlerMethod) handler;
                Method method = handlerMethod.getMethod();
                NotRepeatSubmit annotation = method.getAnnotation(NotRepeatSubmit.class);
                return annotation;
            }
            return null;
        }
    }

    拦截器类

    MyInterceptor.java

    package com.example.demo.annotation;
    
    
    import com.example.demo.token.NotRepeatSubmit;
    import io.jsonwebtoken.lang.Assert;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.data.redis.core.RedisTemplate;
    import org.springframework.stereotype.Component;
    import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.util.concurrent.TimeUnit;
    
    
    /**
     * 拦截器
     */
    @Component
    public class MyInterceptor extends HandlerInterceptorAdapter {
    
        @Autowired
        private RedisTemplate redisTemplate;
    
        /**
         * @param request
         * @param response
         * @param handler  访问的目标方法
         * @return
         * @throws Exception
         */
        @Override
        public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
    
            //这是请求接口时带的随机字符串
            String sign = request.getHeader("sign");
    
            // 获取注解上的超时时间 默认为5分钟
            NotRepeatSubmit notRepeatSubmit = ApiUtil.getNotRepeatSubmit(handler);
            long expireTime = notRepeatSubmit == null ? 5 * 60 * 1000 : notRepeatSubmit.value();
    
    
            // 拒绝重复调用(第一次访问时存储,过期时间和请求超时时间保持一致), 只有标注不允许重复提交注解的才会校验
            if (notRepeatSubmit != null) {
                boolean exists = redisTemplate.hasKey(sign);
                Assert.isTrue(!exists, "请勿重复提交");
                redisTemplate.opsForValue().set(sign, 0, expireTime, TimeUnit.MILLISECONDS);
            }
    
            return super.preHandle(request, response, handler);
        }
    }
    WebMvcConfiguration.java
    package com.example.demo.annotation;
    
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
    
    /**
     * 将自己写的MyInterceptor添加到拦截配置器中
     */
    @Configuration
    public class WebMvcConfiguration extends WebMvcConfigurationSupport {
    
        @Autowired
        private MyInterceptor myInterceptor;
    
        @Override
        public void addInterceptors(InterceptorRegistry registry) {
            super.addInterceptors(registry);
            registry.addInterceptor(myInterceptor)
                    .addPathPatterns("/api/**");
        }
    }

    使用方式 控制器类

    package com.example.demo.annotation;
    
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.web.bind.annotation.PostMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    
    
    /**
     * 请求控制器
     */
    @Slf4j
    @RestController
    public class TestController {
    
    
        /**
         * @NotRepeatSubmit 是自定义注解 防止重复提交
         * @return
         */
        @NotRepeatSubmit(5000)
        @PostMapping("/test")
        public void test(String name) {
    
        }
    
    
    
    }
  • 相关阅读:
    Java实现 蓝桥杯 算法提高 小X的购物计划
    Java实现 蓝桥杯 算法提高 小X的购物计划
    Java实现 第十一届 蓝桥杯 (高职专科组)省内模拟赛
    Java实现 第十一届 蓝桥杯 (高职专科组)省内模拟赛
    Java实现 第十一届 蓝桥杯 (高职专科组)省内模拟赛
    Java 第十一届 蓝桥杯 省模拟赛 小明的城堡
    Java 第十一届 蓝桥杯 省模拟赛 小明的城堡
    Java 第十一届 蓝桥杯 省模拟赛 小明的城堡
    129. Sum Root to Leaf Numbers
    117. Populating Next Right Pointers in Each Node II
  • 原文地址:https://www.cnblogs.com/pxblog/p/12977404.html
Copyright © 2011-2022 走看看