zoukankan      html  css  js  c++  java
  • springboot过滤器和拦截器

     一、过滤器

    @Component
    public class MyFilter extends OncePerRequestFilter {
        @Override
        protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
    
            String cookie = response.getHeader("Set-Cookie");
            System.out.println("+++++++++++++++自定义过滤器++++++++++++++++");
            System.out.println(cookie);
            System.out.println("+++++++++++++++自定义过滤器++++++++++++++++");
            filterChain.doFilter(request, response);
        }
    }

     二、拦截器

     1.拦截器定义

    @Component
    public class MyInterceptor implements HandlerInterceptor {
        @Override
        public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
            String cookie = response.getHeader("Set-Cookie");
            System.out.println("*************自定义拦截器**************");
            System.out.println(cookie);
            System.out.println("*************自定义拦截器**************");
            return true;
        }
    }

    2.注册拦截器

    @Configuration
    public class RegisterInterceptorConfig implements WebMvcConfigurer {
    
        @Autowired
        private MyInterceptor myInterceptor;
    
        @Override
        public void addInterceptors(InterceptorRegistry registry) {
            InterceptorRegistration registration = registry.addInterceptor(myInterceptor);
            registration.addPathPatterns("/**");
        }
    }
  • 相关阅读:
    04机器学习实战之朴素贝叶斯
    06Web服务
    03机器学习实战之决策树scikit-learn实现
    03机器学习实战之决策树
    将两个列表合并为字典_其中一个列表为Key_一个列表为Value
    17反射
    16网络通信协议
    百练 2733 判断闰年 解题报告
    百练 2799 浮点数格式 解题报告
    lucene详细介绍
  • 原文地址:https://www.cnblogs.com/wangbin2188/p/15070297.html
Copyright © 2011-2022 走看看