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("/**");
        }
    }
  • 相关阅读:
    性能篇系列—stream详解
    Java正则表达式详细解析
    干货系列性能篇之——序列化
    面试官之问:知道你的接口“QPS”是多少吗?
    Java性能之优化RPC网络通信
    Spring之 JDBC 异常
    Java性能之synchronized锁的优化
    浅谈Java中switch分支语句
    Spring Boot 之异步执行方法
    Java性能 -- Lock优化
  • 原文地址:https://www.cnblogs.com/wangbin2188/p/15070297.html
Copyright © 2011-2022 走看看