当作为WAR部署在Tomcat容器中并使用SpringBootServletInitializer自定义时,该应用程序运行良好。
但是当将ServletContextInitializer用作可执行JAR时,它不起作用。
对于任何URL,甚至是静态内容,我都会从其中一个过滤器中收到400错误的请求,并显示一条错误消息。
ServletContextInitializer和SpringBootServletInitializer的代码相同。
@Configuration
public class WarConfiguration extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(WebApplication.class);
}
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
// AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext();
WebApplicationContext curCtx = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
// dispatcherContext.register(ServletConfig.class);
ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher", new DispatcherServlet(curCtx));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/*");
ServletRegistration.Dynamic defaultServlet = servletContext.addServlet("default", new DefaultServlet());
defaultServlet.setInitParameter("debug", "1");
defaultServlet.setInitParameter("listings", "true");
defaultServlet.setLoadOnStartup(1);
defaultServlet.addMapping("/content/*");
FilterRegistration.Dynamic logRequestFilter = servletContext.addFilter("logRequestFilter", new LogRequestFilter());
logRequestFilter.addMappingForServletNames(null, true, "dispatcher");
logRequestFilter.addMappingForUrlPatterns(null, true, "/*");
FilterRegistration.Dynamic delegateFilterProxy = servletContext.addFilter("delegateFilterProxy", new DelegatingFilterProxy());
delegateFilterProxy.setInitParameter("targetFilterLifecycle", "true");
delegateFilterProxy.addMappingForUrlPatterns(null, true, "/*");
}
}
这是authentication-context.xml
<bean id="filterChainProxy" class="org.springframework.security.web.FilterChainProxy">
<constructor-arg>
<list>
<security:filter-chain pattern="/oauth/**"
filters="filter1,filter2" />
<security:filter-chain pattern="/**"
filters="filter1,filter2,filter3,filter4" />
</list>
</constructor-arg>
</bean>
有一个@Configuration注释的类