Spring Boot之过滤器 Filter注入的方式解析

过滤器 Filter注入的方式

第一种

@WebFilter(filterName = "myFilter",urlPatterns = "/*")
public class MyFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) {
    }
    @Override
    public void destroy() {
    }
}
@SpringBootApplication
@EnableAutoConfiguration
@EnableWebMvc
@ServletComponentScan(basePackages = "com.fanyin.eghm")
public class EghmApplication {
    public static void main(String[] args) {
        SpringApplication.run(EghmApplication.class, args);
    }
}

@ServletComponentScan 所扫描的包路径必须包含该Filter

第二种

@Configuration
public class FilterConfig {
    @Bean
    public FilterRegistrationBean filterRegistrationBean(){
        FilterRegistrationBean bean = new FilterRegistrationBean();
        bean.setFilter(new MyFilter2());
        bean.addUrlPatterns("/*");
        return bean;
    }
}

第三种

@Bean("proxyFilter")
    public Filter filter (){
        return new Filter() {
            @Override
            public void init(javax.servlet.FilterConfig filterConfig) throws ServletException {
            }
            @Override
            public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) {
            }
            @Override
            public void destroy() {
            }
        };
    }
    @Bean
    public DelegatingFilterProxyRegistrationBean delegatingFilterProxyRegistrationBean(){
        DelegatingFilterProxyRegistrationBean bean = new DelegatingFilterProxyRegistrationBean("proxyFilter");
        bean.addUrlPatterns("/*");
        return bean;
    }

方法四

@Bean("myFilter")
    public Filter filter (){
        return new Filter() {
            @Override
            public void init(javax.servlet.FilterConfig filterConfig) throws ServletException {
            }
            @Override
            public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) {
            }
            @Override
            public void destroy() {
            }
        };
    }

说明:

第二种和第三种类似,均实现了AbstractFilterRegistrationBean接口,而该接口间接实现了ServletContextInitializer,springboot在启动容器后会查找实现该接口的bean,并调用**onStartup()**方法添加自定义的Filter,两则的区别 DelegatingFilterProxyRegistrationBean 通过传入的proxyFilter名字,在WebApplicationContext查找该Fillter Bean,并通过DelegatingFilterProxy生成基于该Bean的代理Filter对象,

而FilterRegistrationBean 则是直接设置一个Filter,因此该Filter可以有spring容器管理,也可不用spring管理

注意:如果Filter声明为一个Bean,则不需要定义为FilterRegistrationBean,也会被spring发现并添加,就是方法四,该方式无法定义拦截规则等,默认全局,慎用!

注入自定义Filter

直接举个例子,我现在想实现一个Filter,过滤所有访问/admin前缀的请求

public class AdminFilter implements Filter { 
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.out.println("此时有人访问admin");
        filterChain.doFilter(servletRequest,servletResponse);
        System.out.println("该人以及访问结束");
    }
}

这里我虽然自定义了一个Filter,但是是无法扫描到的。

有两种方式可以解决这个,一个是在启动类增加

@ServletComponentScan

这个注解不管是Filter还是Servlet都适用

另一种方式就是通过注入

FilterRegistrationBean

如下:

@Bean
    public FilterRegistrationBean filterRegistrationBean(){
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new AdminFilter());
        filterRegistrationBean.addUrlPatterns("/admin/*");
        return filterRegistrationBean;
    }

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

你可能感兴趣的:(Spring Boot之过滤器 Filter注入的方式解析)