无法覆盖或禁用spring boot-instantiated Filter,导致过滤器链

时间:2017-10-16 21:44:14

标签: java spring spring-boot filter servlet-filters

我有一个组件类:

public class FooFilter implements Filter {
    private FooService fooService; //spring-injected dependency
    @Override public void init(FilterConfig filterConfig) {}
    @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain){}
    @Override public void destroy() {}
}

在springConfigured.xml中定义:

<context:spring-configured />
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
                           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">

    <bean class="com.foo.filter.FooFilter" scope="prototype" autowire="byType"/>

</beans>

使用FilterRegistrationBean在我们的过滤器链中配置过滤器:

@Configuration
public class FooFilterRegistrationConfig {
    private List<String> URL_PATTERNS = Arrays.asList("foo", "bar");
    @Bean
    public FilterRegistrationBean fooFilter(FooFilter filter) {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(filter);
        registration.setName("FooFilter");
        registration.setUrlPatterns(URL_PATTERNS);
        registration.setOrder(4);
        return registration;
    }

    //and some more
}

使用此设置,我们在过滤器链中最终得到两个FooFilter。经度:

//a bunch of filters
2017-10-16 16:34:37,468 [RMI TCP Connection(3)-127.0.0.1] INFO  servlet.FilterRegistrationBean - Mapping filter: 'FooFilter' to urls: [*.jsp, *.do, *.faces, *.action, /mvc/*]
//some more filters
2017-10-16 16:34:37,468 [RMI TCP Connection(3)-127.0.0.1] INFO  servlet.FilterRegistrationBean - Mapping filter: 'com.foo.web.filter.FooFilter#0' to: [/*]

对此类问题的其他一些答案建议使用RegistrationConfig禁用过滤器。如果我尝试使用registration.setEnabled(false)禁用FilterRegistrationConfig中的过滤器,我仍然会在最后获得额外的过滤器。

2017-10-16 16:22:46,078 [RMI TCP Connection(3)-127.0.0.1] INFO  servlet.FilterRegistrationBean - Filter FooFilter was not registered (disabled)
...
2017-10-16 16:22:46,079 [RMI TCP Connection(3)-127.0.0.1] INFO  servlet.FilterRegistrationBean - Mapping filter: 'com.foo.filter.FooFilter#0' to: [/*]

我已经尝试完全删除了我们FilterRegistrationConfig中对此过滤器的引用,只是直接在FooFilter类上使用@Order注释,但是因为我们在xml中指定了这个bean而没有使用组件扫描,我似乎无法获得@WebFilter注释来正确应用Url模式。

如何从我的过滤器链中删除这个春天启动创建的FooFilter#0的任何提示都将非常感激!

编辑:

我尝试从xml中删除bean定义。现在我不能将它注入FilterRegistrationBean方法,因为这给了我“找不到'FooFilter'类型的bean'错误。

@Bean
public FilterRegistrationBean fooFilter(FooFilter filter) { //no beans of 'FooFilter' type found
    FilterRegistrationBean registration = new FilterRegistrationBean();
    registration.setFilter(filter);
    registration.setName("FooFilter");
    registration.setUrlPatterns(URL_PATTERNS);
    registration.setOrder(4);
    return registration;
}

但是,如果我在这里手动实例化一个FooFilter,我会在以后遇到问题,因为FooFilter包含许多未注入的弹簧管理依赖项:

@Bean
public FilterRegistrationBean fooFilter() {
    FilterRegistrationBean registration = new FilterRegistrationBean();
    registration.setFilter(new FooFilter()); //works fine here, but Spring does not inject dependencies into this object I created, so null pointers later
    registration.setName("FooFilter");
    registration.setUrlPatterns(URL_PATTERNS);
    registration.setOrder(4);
    return registration;
}

1 个答案:

答案 0 :(得分:1)

FilterRegistrationBean创建你的FooFilter bean,你不必单独创建它,为FooFilter删除bean(跟随行)

<bean class="com.foo.filter.FooFilter" scope="prototype" autowire="byType"/>

注意: 当spring容器创建bean FilterRegistrationBean时,它也会像你提到的那样创建一个bean FooFilter      registration.setName( “FooFilter”); // FooFilter是bean的名称