Merge branch '3.1.x'

Closes gh-38286
This commit is contained in:
Andy Wilkinson 2023-11-09 20:31:53 +00:00
commit 9247104830
2 changed files with 21 additions and 9 deletions

View File

@ -34,7 +34,8 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnNotWarDeplo
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type; import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type;
import org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryAutoConfiguration; import org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryAutoConfiguration;
import org.springframework.boot.web.servlet.ServletContextInitializer; import org.springframework.boot.web.embedded.jetty.JettyServletWebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered; import org.springframework.core.Ordered;
@ -91,13 +92,15 @@ public class WebSocketServletAutoConfiguration {
@Bean @Bean
@ConditionalOnNotWarDeployment @ConditionalOnNotWarDeployment
@Order(Ordered.LOWEST_PRECEDENCE) @Order(Ordered.LOWEST_PRECEDENCE)
@ConditionalOnMissingBean(name = "websocketUpgradeFilterServletContextInitializer") @ConditionalOnMissingBean(name = "websocketUpgradeFilterWebServerCustomizer")
ServletContextInitializer websocketUpgradeFilterServletContextInitializer() { WebServerFactoryCustomizer<JettyServletWebServerFactory> websocketUpgradeFilterWebServerCustomizer() {
return (servletContext) -> { return (factory) -> {
Dynamic registration = servletContext.addFilter(WebSocketUpgradeFilter.class.getName(), factory.addInitializers((servletContext) -> {
new WebSocketUpgradeFilter()); Dynamic registration = servletContext.addFilter(WebSocketUpgradeFilter.class.getName(),
registration.setAsyncSupported(true); new WebSocketUpgradeFilter());
registration.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), false, "/*"); registration.setAsyncSupported(true);
registration.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), false, "/*");
});
}; };
} }

View File

@ -106,7 +106,7 @@ class WebSocketServletAutoConfigurationTests {
} }
@Test @Test
void jettyWebSocketUpgradeFilterIsAddedToServletContext() { void jettyWebSocketUpgradeFilterIsAddedToServletContextOfJettyServer() {
try (AnnotationConfigServletWebServerApplicationContext context = new AnnotationConfigServletWebServerApplicationContext( try (AnnotationConfigServletWebServerApplicationContext context = new AnnotationConfigServletWebServerApplicationContext(
JettyConfiguration.class, WebSocketServletAutoConfiguration.JettyWebSocketConfiguration.class)) { JettyConfiguration.class, WebSocketServletAutoConfiguration.JettyWebSocketConfiguration.class)) {
assertThat(context.getServletContext().getFilterRegistration(WebSocketUpgradeFilter.class.getName())) assertThat(context.getServletContext().getFilterRegistration(WebSocketUpgradeFilter.class.getName()))
@ -114,6 +114,15 @@ class WebSocketServletAutoConfigurationTests {
} }
} }
@Test
void jettyWebSocketUpgradeFilterIsNotAddedToServletContextOfTomcatServer() {
try (AnnotationConfigServletWebServerApplicationContext context = new AnnotationConfigServletWebServerApplicationContext(
TomcatConfiguration.class, WebSocketServletAutoConfiguration.JettyWebSocketConfiguration.class)) {
assertThat(context.getServletContext().getFilterRegistration(WebSocketUpgradeFilter.class.getName()))
.isNull();
}
}
@Test @Test
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
void jettyWebSocketUpgradeFilterIsNotExposedAsABean() { void jettyWebSocketUpgradeFilterIsNotExposedAsABean() {