diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/tomcat/TomcatWebServer.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/tomcat/TomcatWebServer.java index dc272d6a5e..68f245d2b3 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/tomcat/TomcatWebServer.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/tomcat/TomcatWebServer.java @@ -27,6 +27,7 @@ import javax.naming.NamingException; import org.apache.catalina.Container; import org.apache.catalina.Context; import org.apache.catalina.Engine; +import org.apache.catalina.Lifecycle; import org.apache.catalina.LifecycleException; import org.apache.catalina.LifecycleState; import org.apache.catalina.Service; @@ -92,8 +93,15 @@ public class TomcatWebServer implements WebServer { try { addInstanceIdToEngineName(); - // Remove service connectors so that protocol binding doesn't happen yet - removeServiceConnectors(); + Context context = findContext(); + context.addLifecycleListener((event) -> { + if (context.equals(event.getSource()) + && Lifecycle.START_EVENT.equals(event.getType())) { + // Remove service connectors so that protocol binding doesn't + // happen when the service is started. + removeServiceConnectors(); + } + }); // Start the server to trigger initialization listeners this.tomcat.start(); @@ -101,7 +109,6 @@ public class TomcatWebServer implements WebServer { // We can re-throw failure exception directly in the main thread rethrowDeferredStartupExceptions(); - Context context = findContext(); try { ContextBindings.bindClassLoader(context, context.getNamingToken(), getClass().getClassLoader()); diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/servlet/server/AbstractServletWebServerFactoryTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/servlet/server/AbstractServletWebServerFactoryTests.java index ccbbc60e37..d2a88cf65c 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/servlet/server/AbstractServletWebServerFactoryTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/servlet/server/AbstractServletWebServerFactoryTests.java @@ -801,6 +801,21 @@ public abstract class AbstractServletWebServerFactoryTests { assertThat(sessionCookieConfig.getMaxAge()).isEqualTo(60); } + @Test + public void sslSessionTracking() { + AbstractServletWebServerFactory factory = getFactory(); + Ssl ssl = new Ssl(); + ssl.setEnabled(true); + ssl.setKeyStore("src/test/resources/test.jks"); + ssl.setKeyPassword("password"); + factory.setSsl(ssl); + factory.getSession().setTrackingModes(EnumSet.of(SessionTrackingMode.SSL)); + AtomicReference contextReference = new AtomicReference<>(); + this.webServer = factory.getWebServer(contextReference::set); + assertThat(contextReference.get().getEffectiveSessionTrackingModes()) + .isEqualTo(EnumSet.of(javax.servlet.SessionTrackingMode.SSL)); + } + @Test public void compressionOfResponseToGetRequest() throws Exception { assertThat(doTestCompression(10000, null, null)).isTrue();