我正在尝试添加一个需要参数FilterConfig的Filter。我在WebApplicationInitializer :: onStartup(ServletContext容器)方法中添加它并且配置得很好(使用正确的FilterConfig调用init方法)。
@Override
public void onStartup(ServletContext container) throws ServletException {
super.onStartup(container);
FilterRegistration.Dynamic urlRewriteFilter = container.addFilter("UrlRewriteFilter", UrlRewriteFilter.class);
urlRewriteFilter.addMappingForServletNames(EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD), false, "/*");
urlRewriteFilter.setInitParameter("confPath", "WEB-INF/urlrewrite-prod.xml");
}
重点是我使用的是Spring Security WebSecurityConfigurerAdapter,我的过滤器永远不会被调用(过滤器链中不存在)。 如果我用addFilterAfter添加它,就无法给他一个FilterConfig。
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.exceptionHandling()
.authenticationEntryPoint(spnegoEntryPoint())
.and()
.authorizeRequests()
.antMatchers("/").permitAll()
.anyRequest().authenticated()
.and()
.httpBasic()
.and()
.logout()
.and()
.addFilterBefore(spnegoAuthenticationProcessingFilter(authenticationManagerBean()), BasicAuthenticationFilter.class)
.addFilterAfter(new CsrfHeaderFilter(), CsrfFilter.class)
.addFilterAfter(new CacheFilter(), CsrfFilter.class)
.addFilterAfter(new UrlRewriteFilter(), CsrfFilter.class);
}
我哪里错了?
答案 0 :(得分:0)
我的错误在于过滤器映射。我正在调用addMappingForServletNames而不是addMappingForUrlPatterns
@Override
public void onStartup(ServletContext container) throws ServletException {
super.onStartup(container);
FilterRegistration.Dynamic urlRewriteFilter = container.addFilter("UrlRewriteFilter", UrlRewriteFilter.class);
//urlRewriteFilter.addMappingForServletNames(EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD), false, "/*");
urlRewriteFilter.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD), false, "/*");
urlRewriteFilter.setInitParameter("confPath", "WEB-INF/urlrewrite-prod.xml");
}