gpt4 book ai didi

java - 无法更改 spring-boot 过滤器映射

转载 作者:行者123 更新时间:2023-11-30 07:58:41 26 4
gpt4 key购买 nike

我有以下类(class)

@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中包装请求为时已晚,因为输入流已被读取。那么有人可以帮我解决这个问题吗?

谢谢

最佳答案

您的FilterRegistrationBean名为 hiddenHttpMethodFilter 的 bean被 OrderedHiddenHttpMethodFilter 覆盖由 Spring Boot 自动配置创建的同名 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方法。另外,如果您想配置 Filter 的注册或Servlet引导自动配置,无需创建 FilterServlet你自己。相反,您应该将现有的注入(inject)到 @Bean 中。方法:

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

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

关于java - 无法更改 spring-boot 过滤器映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32250619/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com