diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/websocket/servlet/WebSocketServletAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/websocket/servlet/WebSocketServletAutoConfiguration.java index 1a3f0458638..f7fed2b0d9d 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/websocket/servlet/WebSocketServletAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/websocket/servlet/WebSocketServletAutoConfiguration.java @@ -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.Type; 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.Configuration; import org.springframework.core.Ordered; @@ -91,13 +92,15 @@ public class WebSocketServletAutoConfiguration { @Bean @ConditionalOnNotWarDeployment @Order(Ordered.LOWEST_PRECEDENCE) - @ConditionalOnMissingBean(name = "websocketUpgradeFilterServletContextInitializer") - ServletContextInitializer websocketUpgradeFilterServletContextInitializer() { - return (servletContext) -> { - Dynamic registration = servletContext.addFilter(WebSocketUpgradeFilter.class.getName(), - new WebSocketUpgradeFilter()); - registration.setAsyncSupported(true); - registration.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), false, "/*"); + @ConditionalOnMissingBean(name = "websocketUpgradeFilterWebServerCustomizer") + WebServerFactoryCustomizer websocketUpgradeFilterWebServerCustomizer() { + return (factory) -> { + factory.addInitializers((servletContext) -> { + Dynamic registration = servletContext.addFilter(WebSocketUpgradeFilter.class.getName(), + new WebSocketUpgradeFilter()); + registration.setAsyncSupported(true); + registration.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), false, "/*"); + }); }; } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/websocket/servlet/WebSocketServletAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/websocket/servlet/WebSocketServletAutoConfigurationTests.java index f38bbc9e8bb..c2112deeddd 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/websocket/servlet/WebSocketServletAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/websocket/servlet/WebSocketServletAutoConfigurationTests.java @@ -108,7 +108,7 @@ class WebSocketServletAutoConfigurationTests { @Test @Servlet5ClassPathOverrides - void jettyWebSocketUpgradeFilterIsAddedToServletContext() { + void jettyWebSocketUpgradeFilterIsAddedToServletContextOfJettyServer() { try (AnnotationConfigServletWebServerApplicationContext context = new AnnotationConfigServletWebServerApplicationContext( JettyConfiguration.class, WebSocketServletAutoConfiguration.JettyWebSocketConfiguration.class)) { assertThat(context.getServletContext().getFilterRegistration(WebSocketUpgradeFilter.class.getName())) @@ -116,6 +116,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 @SuppressWarnings("rawtypes") void jettyWebSocketUpgradeFilterIsNotExposedAsABean() {