1

给定这个过滤器:

public class MyFilter implements Filter{

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        System.out.println("FILTERING!!!!!!!!!!!!!!!!!!!"+((HttpServletRequest)request).getRequestURI());
        chain.doFilter(request, response);
    }

我已经看到在同一个请求中被多次调用

春季启动应用程序:

@SpringBootApplication(scanBasePackageClasses={MySpringBootDemoApplication .class, TilesConfiguration.class})
public class MySpringBootDemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(MySpringBootDemoApplication .class, args);
    }

    @Bean
    public Filter myFilter(){
        return new MyFilter();
    }
}

瓷砖配置:

@Configuration
public class TilesConfiguration {

     @Bean
     public TilesConfigurer tilesConfigurer() {
         final TilesConfigurer configurer = new TilesConfigurer();
         configurer.setDefinitions(new String[] { "WEB-INF/**/tiles.xml" });
         configurer.setCheckRefresh(true);
         return configurer;
     }

     @Bean
     public TilesViewResolver tilesViewResolver() {
         final TilesViewResolver resolver = new TilesViewResolver();
         resolver.setViewClass(TilesView.class);
         return resolver;
     }

}

这是我在日志中看到的:

FILTERING!!!!!!!!!!!!!!!!!!!/spring-boot-demo/home
FILTERING!!!!!!!!!!!!!!!!!!!/spring-boot-demo/WEB-INF/layouts/standard.jsp
FILTERING!!!!!!!!!!!!!!!!!!!/spring-boot-demo/WEB-INF/layouts/standard.jsp
FILTERING!!!!!!!!!!!!!!!!!!!/spring-boot-demo/WEB-INF/layouts/standard.jsp

浏览器发出的唯一真实请求是:

FILTERING!!!!!!!!!!!!!!!!!!!/spring-boot-demo/home

与 ( ) 相关的图块/standard.jsp从何而来?在传统的 Spring MVC 应用程序中,它们从未出现过。

我知道 MyFilter 可以扩展 OncePerRequestFilter 但我无法修改我必须使用的真实过滤器,它们来自遗留库。

4

1 回答 1

1

我刚刚意识到,由于 MyFilter 被映射到/*它也将在容器转发上被调用(因此 Tile 的 standard.jsp ...)。我将 DispatcherServlet 更改为 match /action/*,过滤器也是如此:

@Bean
public FilterRegistrationBean myFilter(){
    FilterRegistrationBean frb = new FilterRegistrationBean();
    frb.setFilter(new MyFilter());
    frb.setUrlPatterns(Arrays.asList(new String[]{"/action/*"}));
    return frb;

@Bean
public ServletRegistrationBean dispatcherRegistration(DispatcherServlet dispatcherServlet) {
    ServletRegistrationBean registration = new ServletRegistrationBean(dispatcherServlet);
    registration.addUrlMappings("/action/*");
    return registration;
}
于 2016-04-07T15:08:33.397 回答