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
This commit is contained in:
@@ -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<javax.servlet.SessionTrackingMode> unwrap(
|
||||
Set<Session.SessionTrackingMode> modes) {
|
||||
if (modes == null) {
|
||||
return null;
|
||||
}
|
||||
Set<javax.servlet.SessionTrackingMode> result = new LinkedHashSet<>();
|
||||
for (Session.SessionTrackingMode mode : modes) {
|
||||
result.add(javax.servlet.SessionTrackingMode.valueOf(mode.name()));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static class TomcatCustomizer {
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user