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("/**");
        }
    }
  • 相关阅读:
    write to logfile
    open and read a file content to a variable
    strategy
    Android 开机启动程序
    消息队列
    卡机音乐功能实现
    Android 2.0 开机动画文件分析
    多线程实例
    消息队列
    多线程实例
  • 原文地址:https://www.cnblogs.com/wangbin2188/p/15070297.html
Copyright © 2011-2022 走看看