簡體   English   中英

無法更改過濾器映射彈簧啟動

[英]Unable to change filter mapping spring-boot

我上了課

@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() ,它已經被讀取。 我也閱讀了這個問題並嘗試實現RequestWrapper,但是在testServlet中包裝請求已經太遲了,因為已讀取了inputstream。 那么有人可以幫我解決這個問題嗎?

謝謝

與Spring Boot的自動配置創建的同名OrderedHiddenHttpMethodFilter bean覆蓋了您的名為hiddenHttpMethodFilter FilterRegistrationBean 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方法。 另外,如果要配置Boot自動配置的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映射的一種簡便方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM