zoukankan      html  css  js  c++  java
  • 自定义注解拦截(比如自定义注解,登陆拦截)

    1.注解类

    @Target(ElementType.METHOD)
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    public @interface Mytest {
    
        String value();
    }

    2.拦截类

    @Configuration
    public class Login extends WebMvcConfigurerAdapter {
    
        @Bean
        public SessionInterceptor getSessionInterceptor() {
            System.out.println("bean加载了!");
            return new SessionInterceptor();
        }
        
        
        @Override
        public void addInterceptors(InterceptorRegistry registry) {
            InterceptorRegistration addInterceptor = registry.addInterceptor(getSessionInterceptor());
            addInterceptor.addPathPatterns("/**");
        }
    
    
        private class SessionInterceptor extends HandlerInterceptorAdapter {
    
            @Override
            public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { 
                System.out.println("==================>");
                if(!(handler instanceof HandlerMethod)) {
                    System.out.println(0);
                    return true;
                }
                HandlerMethod handlerMethod = (HandlerMethod)handler;            
                Mytest methodAnnotation = handlerMethod.getMethodAnnotation(Mytest.class);            
                if(methodAnnotation == null){
                    System.out.println(1);
                    return true; 
                }
                String str = methodAnnotation.value(); 
                System.out.println("拦截value:"+str);
                System.out.println(2);
                return true;
            }
    
            @Override
            public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
                System.out.println("<=======*=======*========*======华丽的请求分割线======*=========*========*=======>");
            }
        }
        
    }

    3.使用类

    @RestController
    public class MyController {
    
        @Mytest(value = "true")
        @RequestMapping(value = "/test1/{id}", method = RequestMethod.GET)
        public  String getById(@PathVariable("id") Long id) {
             System.out.println("请求成功");
            return "1";
        }
        
        @RequestMapping(value = "/test2/{id2}", method = RequestMethod.GET)
        public  String getById2(@PathVariable("id2") Long id2) {
            System.out.println("请求成功");
            return "2";
        }
        
    }

    通过使用@Mytest注解,完成登陆拦截校验.

  • 相关阅读:
    Windows内存管理系列
    Windows C/C++ 内存泄露检测
    TCP/IP协议学习(六) 链路层详解
    TCP/IP协议学习(五) 基于C# Socket的C/S模型
    TCP/IP协议学习(四) 协议概述
    STM32学习笔记(十) CAN通讯测试(环回模式)
    STM32学习笔记(九) 外部中断,待机模式和事件唤醒
    STM32学习笔记(八) SPI总线(操作外部flash)
    TCP/IP协议学习(三) STM32中ETH驱动配置注意事项
    TCP/IP协议学习(二) LWIP用户自定义配置文件解析
  • 原文地址:https://www.cnblogs.com/wonder2636/p/9639108.html
Copyright © 2011-2022 走看看