diff --git a/jetty-core/jetty-session/src/main/java/org/eclipse/jetty/session/AbstractSessionManager.java b/jetty-core/jetty-session/src/main/java/org/eclipse/jetty/session/AbstractSessionManager.java index 9ff7ed5bec1..8fab13497b3 100644 --- a/jetty-core/jetty-session/src/main/java/org/eclipse/jetty/session/AbstractSessionManager.java +++ b/jetty-core/jetty-session/src/main/java/org/eclipse/jetty/session/AbstractSessionManager.java @@ -239,9 +239,9 @@ public abstract class AbstractSessionManager extends ContainerLifeCycle implemen _context = ContextHandler.getCurrentContext(); _loader = Thread.currentThread().getContextClassLoader(); - // ensure a session path is set for non root contexts + // ensure a session path is set String contextPath = _context == null ? "/" : _context.getContextPath(); - if (!"/".equals(contextPath) && getSessionPath() == null) + if (getSessionPath() == null) setSessionPath(contextPath); // Use a coarser lock to serialize concurrent start of many contexts. diff --git a/jetty-ee10/jetty-ee10-servlet/src/test/java/org/eclipse/jetty/ee10/servlet/SessionHandlerTest.java b/jetty-ee10/jetty-ee10-servlet/src/test/java/org/eclipse/jetty/ee10/servlet/SessionHandlerTest.java index cbb868a144e..65f3b0b496b 100644 --- a/jetty-ee10/jetty-ee10-servlet/src/test/java/org/eclipse/jetty/ee10/servlet/SessionHandlerTest.java +++ b/jetty-ee10/jetty-ee10-servlet/src/test/java/org/eclipse/jetty/ee10/servlet/SessionHandlerTest.java @@ -299,7 +299,7 @@ public class SessionHandlerTest @Test public void testSimpleSessionCreation() throws Exception { - String contextPath = ""; + String contextPath = "/"; String servletMapping = "/server"; Server server = new Server(); @@ -339,7 +339,8 @@ public class SessionHandlerTest client.start(); //make a session - String url = "http://localhost:" + port + contextPath + servletMapping + "?action=create"; + String path = contextPath + (contextPath.endsWith("/") && servletMapping.startsWith("/") ? servletMapping.substring(1) : servletMapping); + String url = "http://localhost:" + port + path + "?action=create"; //make a request to set up a session on the server ContentResponse response = client.GET(url); @@ -347,8 +348,9 @@ public class SessionHandlerTest String sessionCookie = response.getHeaders().get("Set-Cookie"); assertTrue(sessionCookie != null); + assertThat(sessionCookie, containsString("Path=/")); - ContentResponse response2 = client.GET("http://localhost:" + port + contextPath + servletMapping + "?action=test"); + ContentResponse response2 = client.GET("http://localhost:" + port + path + "?action=test"); assertEquals(HttpServletResponse.SC_OK, response2.getStatus()); } finally diff --git a/jetty-ee9/jetty-ee9-nested/src/test/java/org/eclipse/jetty/ee9/nested/SessionHandlerTest.java b/jetty-ee9/jetty-ee9-nested/src/test/java/org/eclipse/jetty/ee9/nested/SessionHandlerTest.java index f476ad1d80b..df054d810f7 100644 --- a/jetty-ee9/jetty-ee9-nested/src/test/java/org/eclipse/jetty/ee9/nested/SessionHandlerTest.java +++ b/jetty-ee9/jetty-ee9-nested/src/test/java/org/eclipse/jetty/ee9/nested/SessionHandlerTest.java @@ -60,6 +60,7 @@ public class SessionHandlerTest private Server _server; private LocalConnector _connector; private SessionHandler _sessionHandler; + private ContextHandler _contextHandler; @BeforeEach public void beforeEach() throws Exception @@ -67,15 +68,15 @@ public class SessionHandlerTest _server = new Server(); _connector = new LocalConnector(_server); _server.addConnector(_connector); - ContextHandler contextHandler = new ContextHandler(); - _server.setHandler(contextHandler); + _contextHandler = new ContextHandler(); + _server.setHandler(_contextHandler); _sessionHandler = new SessionHandler(); _sessionHandler.setSessionCookie("JSESSIONID"); _sessionHandler.setUsingCookies(true); _sessionHandler.setUsingURLs(false); _sessionHandler.setSessionPath("/"); - contextHandler.setHandler(_sessionHandler); + _contextHandler.setHandler(_sessionHandler); _sessionHandler.setHandler(new AbstractHandler() { @@ -283,6 +284,10 @@ public class SessionHandlerTest @Test public void testCreateSession() throws Exception { + _server.stop(); + _sessionHandler.setSessionPath(null); + _contextHandler.setContextPath("/"); + _server.start(); LocalConnector.LocalEndPoint endPoint = _connector.connect(); endPoint.addInput(""" GET / HTTP/1.1 @@ -301,6 +306,8 @@ public class SessionHandlerTest response = HttpTester.parseResponse(endPoint.getResponse()); assertThat(response.getStatus(), equalTo(200)); + String setCookie = response.get("SET-COOKIE"); + assertThat(setCookie, containsString("Path=/")); String content = response.getContent(); assertThat(content, startsWith("Session=")); String id = content.substring(content.indexOf('=') + 1, content.indexOf('\n'));