zoukankan      html  css  js  c++  java
  • Springboot整合Filter

    1,方式一

      【1】通过注解扫描完成Filter组件注册

    /*@WebFilter(filterName = "FirstFilter",urlPatterns = {"*.do","*.jsp"})*/
    @WebFilter(filterName = "FirstFilter",urlPatterns = "/first")
    public class FirstFilter implements Filter {
    
    
        @Override
        public void init(FilterConfig filterConfig) throws ServletException {
    
        }
    
        @Override
        public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
            System.out.println("进入FirstFilter...");
         filterChain.doFilter(servletRequest,servletResponse); System.out.println(
    "退出FirstFilter..."); } @Override public void destroy() { } }

      【2】修改启动类

    @SpringBootApplication
    @ServletComponentScan//在Spring Boot启动时会扫描@WebFiter注解,并将该类实例
    public class SpringbootwebApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(SpringbootwebApplication.class, args);
        }
    
    }

    2,方式二

      【1】通过方法完成Filter组件注册

    public class SecondFilter implements Filter {
        @Override
        public void init(FilterConfig filterConfig) throws ServletException {
    
        }
    
        @Override
        public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
            System.out.println("进入SecondFilter...");
            filterChain.doFilter(servletRequest,servletResponse);
            System.out.println("退出SecondFilter...");
        }
    
        @Override
        public void destroy() {
    
        }
    }

      【2】创建Filter配置类

    @Configuration
    public class FilterConfig {
        @Bean
        public FilterRegistrationBean getFilterRegistrationBean()
        {
            FilterRegistrationBean bean = new FilterRegistrationBean(new SecondFilter());
            /*bean.addUrlPatterns(new String[]{"*.do","*.jsp"});*/
            bean.addUrlPatterns("/second");
            return bean;
        };
    }
  • 相关阅读:
    Manthan, Codefest 19 (open for everyone, rated, Div. 1 + Div. 2) D
    HDU 2036 求任意多边形面积板子题
    HDU 6703 array(主席树)
    2019牛客暑期多校训练营(第九场)H Cutting BamboosO(二分、主席树)
    lintcode-425-电话号码的字母组合
    lintcode-81-数据流中位数
    lintcode-424-逆波兰表达式求值
    lintcode-423-有效的括号序列
    lintcode-422-最后一个单词的长度
    lintcode-421-简化路径
  • 原文地址:https://www.cnblogs.com/yz-bky/p/12690821.html
Copyright © 2011-2022 走看看