springboot-FilterRegistrationBean

主要用来对servlet filter进行自定义,比如设置order。

先写一个普通的filter:

public class FilterDemo implements Filter {  
    private final Logger log = LoggerFactory.getLogger(FilterDemo.class);  
  
  
    @Override  
    public void destroy() {  
        log.info("" + getClass() + " destroy");  
  
    }  
  
    @Override  
    public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2) throws IOException, ServletException {  
        log.info( " doFilter " );  
        arg2.doFilter(arg0, arg1);  
  
    }  
  
    @Override  
    public void init(FilterConfig arg0) throws ServletException {  
        log.info("" + getClass() + " init");  
  
    }  
  
}  

 

配置如下:

@Configuration  
public class WebComponent2Config   {  
  
    @Bean  
    public FilterRegistrationBean filterDemo3Registration() {  
        FilterRegistrationBean registration = new FilterRegistrationBean();  
        registration.setFilter(filterDemo3());  
        registration.addUrlPatterns("/*");   
        registration.setName("filterDemo3");  
        registration.setOrder(6);  
        return registration;  
    }  
  
    @Bean  
    public FilterRegistrationBean filterDemo4Registration() {  
        FilterRegistrationBean registration = new FilterRegistrationBean();  
        registration.setFilter(filterDemo4());  
        registration.addUrlPatterns("/*");  
        registration.setName("filterDemo4");  
        registration.setOrder(7);  
        return registration;  
    }  
  
    @Bean  
    public Filter filterDemo3() {  
        return new FilterDemo3();  
    }  
  
    @Bean  
    public Filter filterDemo4() {  
        return new FilterDemo4();  
    }  
  
}  

 

posted @ 2018-03-06 10:46  开拖拉机的蜡笔小新  阅读(453)  评论(0编辑  收藏  举报