From f1d4d8434b92f29d714528ac4d62f8884a831104 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Fri, 24 Feb 2017 17:41:39 +0100 Subject: [PATCH] Hide javax.servlet.SessionTrackingMode The server's session can now be configured in both a servlet and a reactive environment. The latter has not requirement on the servlet API and this commit removes the requirement to the `SessionTrackingMode` enum. Closes gh-8402 --- .../DefaultServletContainerCustomizer.java | 28 +++++++++++++++---- .../autoconfigure/web/ServerProperties.java | 24 ++++++++++++++-- 2 files changed, 45 insertions(+), 7 deletions(-) diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/DefaultServletContainerCustomizer.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/DefaultServletContainerCustomizer.java index bb49799788..c738e7f0b2 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/DefaultServletContainerCustomizer.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/DefaultServletContainerCustomizer.java @@ -16,6 +16,9 @@ package org.springframework.boot.autoconfigure.web; +import java.util.LinkedHashSet; +import java.util.Set; + import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.SessionCookieConfig; @@ -38,6 +41,7 @@ import org.eclipse.jetty.server.handler.ContextHandler; import org.eclipse.jetty.server.handler.HandlerCollection; import org.eclipse.jetty.server.handler.HandlerWrapper; +import org.springframework.boot.autoconfigure.web.ServerProperties.Session; import org.springframework.boot.cloud.CloudPlatform; import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer; import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer; @@ -63,6 +67,7 @@ import org.springframework.util.StringUtils; * {@link EmbeddedServletContainerCustomizerBeanPostProcessor} is active. * * @author Brian Clozel + * @author Stephane Nicoll * @since 2.0.0 */ public class DefaultServletContainerCustomizer @@ -147,28 +152,29 @@ public class DefaultServletContainerCustomizer } /** - * {@link ServletContextInitializer} to apply appropriate parts of the {@link ServerProperties.Session} + * {@link ServletContextInitializer} to apply appropriate parts of the {@link Session} * configuration. */ private static class SessionConfiguringInitializer implements ServletContextInitializer { - private final ServerProperties.Session session; + private final Session session; - SessionConfiguringInitializer(ServerProperties.Session session) { + SessionConfiguringInitializer(Session session) { this.session = session; } @Override public void onStartup(ServletContext servletContext) throws ServletException { if (this.session.getTrackingModes() != null) { - servletContext.setSessionTrackingModes(this.session.getTrackingModes()); + servletContext.setSessionTrackingModes( + unwrap(this.session.getTrackingModes())); } configureSessionCookie(servletContext.getSessionCookieConfig()); } private void configureSessionCookie(SessionCookieConfig config) { - ServerProperties.Session.Cookie cookie = this.session.getCookie(); + Session.Cookie cookie = this.session.getCookie(); if (cookie.getName() != null) { config.setName(cookie.getName()); } @@ -192,6 +198,18 @@ public class DefaultServletContainerCustomizer } } + private Set unwrap( + Set modes) { + if (modes == null) { + return null; + } + Set result = new LinkedHashSet<>(); + for (Session.SessionTrackingMode mode : modes) { + result.add(javax.servlet.SessionTrackingMode.valueOf(mode.name())); + } + return result; + } + } private static class TomcatCustomizer { diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java index 9e466aab7a..0eb32e1705 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java @@ -23,8 +23,6 @@ import java.util.ArrayList; import java.util.List; import java.util.Set; -import javax.servlet.SessionTrackingMode; - import org.springframework.boot.context.embedded.Compression; import org.springframework.boot.context.embedded.Servlet; import org.springframework.boot.context.embedded.Ssl; @@ -361,6 +359,28 @@ public class ServerProperties { } + /** + * Available session tracking modes (mirrors + * {@link javax.servlet.SessionTrackingMode}. + */ + public enum SessionTrackingMode { + /** + * Send a cookie in response to the client's first request. + */ + COOKIE, + + /** + * Rewrite the URL to append a session ID. + */ + URL, + + /** + * Use SSL build-in mechanism to track the session. + */ + SSL + + } + } public static class Tomcat {