【问题标题】:Unable to change filter mapping spring-boot无法更改过滤器映射 spring-boot
【发布时间】:2015-08-27 13:24:13
【问题描述】:

我有以下课程

@Configuration
public class WebConfiguration {

    @Bean
    public ServletRegistrationBean slspServlet() {
        ServletRegistrationBean testServlet = new ServletRegistrationBean(new TestServlet());
        testServlet.addUrlMappings("/*");
        testServlet.setAsyncSupported(true);
        return TestServlet;
    }

    @Bean
    public ServletRegistrationBean aliveServlet() {
        ServletRegistrationBean aliveServlet = new ServletRegistrationBean(new AliveServlet());
        aliveServlet.addUrlMappings("/alive/*");
        aliveServlet.setLoadOnStartup(3);
        return aliveServlet;
    }

    @Bean
    public ServletRegistrationBean templateDownloaderServlet(){
        ServletRegistrationBean templateDownloader = new ServletRegistrationBean(new TemplateDownloaderServlet());
        templateDownloader.addUrlMappings("/template/download/*");
        templateDownloader.setLoadOnStartup(2);
        return templateDownloader;
    }

    @Bean
    public ServletRegistrationBean endOfGenerationThreadServlet(){
        ServletRegistrationBean endOfGenerationThread = new ServletRegistrationBean(new EndOfGenerationThreadServlet());
        endOfGenerationThread.addUrlMappings("/endofgenerationthread/*");
        endOfGenerationThread.setLoadOnStartup(1);
        return endOfGenerationThread;
    }

    @Bean
    public ServletRegistrationBean dispatcherServlet(){
        ServletRegistrationBean dispatcherServlet = new ServletRegistrationBean(new DispatcherServlet());
        dispatcherServlet.setName("dispatcherServlet");
        dispatcherServlet.addUrlMappings("/dispatcher/*");
        return dispatcherServlet;
    }

    @Bean
    public FilterRegistrationBean errorPageFilter(){
        FilterRegistrationBean errorPageFilter = new FilterRegistrationBean(new ErrorPageFilter(), dispatcherServlet());
        errorPageFilter.addUrlPatterns("/test/*");
        return errorPageFilter;
    }

    @Bean
    public FilterRegistrationBean characterEncodingFilter(){
        FilterRegistrationBean characterEncodingFilter = new FilterRegistrationBean(new CharacterEncodingFilter(), dispatcherServlet());
        characterEncodingFilter.addUrlPatterns("/test/*");
        return characterEncodingFilter;
    }

    @Bean
    public FilterRegistrationBean hiddenHttpMethodFilter(){
        FilterRegistrationBean hiddenHttpMethodFilter = new FilterRegistrationBean(new HiddenHttpMethodFilter(), dispatcherServlet());
        hiddenHttpMethodFilter.addUrlPatterns("/test/*");
        return hiddenHttpMethodFilter;
    }


}

我想要做的是更改 hiddenHttpMethodFilter、characterEncodingFilter、errorPageFilter 的过滤器映射,以便我有映射 /test/*。 当我启动应用程序时,我可以看到它默认为“/*”

Mapping servlet: 'testServlet' to [/*]
Mapping servlet: 'aliveServlet' to [/alive/*]
Mapping servlet: 'templateDownloaderServlet' to [/template/download/*]
Mapping servlet: 'endOfGenerationThreadServlet' to [/endofgenerationthread/*]
Mapping servlet: 'dispatcherServlet' to [/dispatcher/*]
Mapping filter: 'errorPageFilter' to: [/*]
Mapping filter: 'characterEncodingFilter' to: [/*]
Mapping filter: 'hiddenHttpMethodFilter' to: [/*]

我缺少什么或我的配置有什么问题?我正在使用 spring-boot 1.2.5。

过滤器还有另一个问题(我认为这在过滤器中)。当我想在我的 testServlet 中访问 request.getInputstream() 时,它已经被读取了。我还阅读了this question 并尝试实现RequestWrapper,但是在我的testServlet 中包装请求为时已晚,因为已读取输入流。那么有人可以帮我解决这个问题吗?

谢谢

【问题讨论】:

    标签: java spring spring-boot httprequest


    【解决方案1】:

    您的名为 hiddenHttpMethodFilterFilterRegistrationBean bean 被 Spring Boot 的自动配置创建的同名 OrderedHiddenHttpMethodFilter bean 覆盖。有一条日志消息告诉您正在发生这种情况:

    2015-08-27 16:13:54.268  INFO 70942 --- [           main] o.s.b.f.s.DefaultListableBeanFactory     : Overriding bean definition for bean 'hiddenHttpMethodFilter' with a different definition: replacing [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=sampleWebFreeMarkerApplication; factoryMethodName=hiddenHttpMethodFilter; initMethodName=null; destroyMethodName=(inferred); defined in sample.freemarker.SampleWebFreeMarkerApplication] with [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration; factoryMethodName=hiddenHttpMethodFilter; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration.class]]
    

    您需要重命名您的 hiddenHttpMethodFilter 方法。另外,如果您要配置引导自动配置的FilterServlet 的注册,则无需自己创建FilterServlet。相反,您应该将现有的注入到您的 @Bean 方法中:

    @Bean
    public FilterRegistrationBean hiddenHttpMethodFilterRegistration(
            HiddenHttpMethodFilter filter) {
        FilterRegistrationBean registration = new FilterRegistrationBean(filter);
        registration.setFilter(filter);
        registration.addUrlPatterns("/test/*");
        return registration;
    }
    

    您可能还想考虑在application.properties 中设置server.servlet-path 属性作为配置调度程序servlet 映射的更简单方法。

    【讨论】:

    • 没错。重命名方法帮助了我。非常感谢。通过 server.servlet-mapping,您可能认为 server.servlet-path。这只是 DispatcherServlet 的映射?
    • 对不起,我的意思是server.servlet-path。我已经更正了我的答案。而且,是的,它仅用于配置调度程序 servlet 的路径。
    猜你喜欢
    • 2018-09-13
    • 2018-07-12
    • 2017-10-12
    • 1970-01-01
    • 2018-01-07
    • 2017-07-23
    • 1970-01-01
    • 2019-04-09
    • 2021-10-19
    相关资源
    最近更新 更多