Ensure that lazy session id generator is initialized by manager

Closes gh-6679
pull/6683/merge
Andy Wilkinson 8 years ago
parent 60697561aa
commit a6f777e941

@ -17,6 +17,7 @@
package org.springframework.boot.context.embedded.tomcat;
import org.apache.catalina.Container;
import org.apache.catalina.Manager;
import org.apache.catalina.core.StandardContext;
import org.springframework.util.ClassUtils;
@ -49,6 +50,14 @@ class TomcatEmbeddedContext extends StandardContext {
return super.loadOnStartup(children);
}
@Override
public void setManager(Manager manager) {
if (manager != null) {
manager.setSessionIdGenerator(new LazySessionIdGenerator());
}
super.setManager(manager);
}
public void deferredLoadOnStartup() {
// Some older Servlet frameworks (e.g. Struts, BIRT) use the Thread context class
// loader to create servlet instances in this phase. If they do that and then try

@ -46,7 +46,6 @@ import org.apache.catalina.Valve;
import org.apache.catalina.Wrapper;
import org.apache.catalina.connector.Connector;
import org.apache.catalina.loader.WebappLoader;
import org.apache.catalina.session.ManagerBase;
import org.apache.catalina.session.StandardManager;
import org.apache.catalina.startup.Tomcat;
import org.apache.catalina.startup.Tomcat.FixContextListener;
@ -488,7 +487,6 @@ public class TomcatEmbeddedServletContainerFactory
else {
context.addLifecycleListener(new DisablePersistSessionListener());
}
context.addLifecycleListener(new LazySessionIdGeneratorListener());
}
private void configurePersistSession(Manager manager) {
@ -830,20 +828,4 @@ public class TomcatEmbeddedServletContainerFactory
}
private static class LazySessionIdGeneratorListener implements LifecycleListener {
@Override
public void lifecycleEvent(LifecycleEvent event) {
if (event.getType().equals(Lifecycle.START_EVENT)) {
Context context = (Context) event.getLifecycle();
Manager manager = context.getManager();
if (manager instanceof ManagerBase) {
((ManagerBase) manager)
.setSessionIdGenerator(new LazySessionIdGenerator());
}
}
}
}
}

@ -34,6 +34,7 @@ import org.apache.catalina.LifecycleEvent;
import org.apache.catalina.LifecycleListener;
import org.apache.catalina.LifecycleState;
import org.apache.catalina.Service;
import org.apache.catalina.SessionIdGenerator;
import org.apache.catalina.Valve;
import org.apache.catalina.Wrapper;
import org.apache.catalina.connector.Connector;
@ -441,6 +442,25 @@ public class TomcatEmbeddedServletContainerFactoryTests
assertThat(getCharset(Locale.FRENCH).toString()).isEqualTo("UTF-8");
}
@Test
public void sessionIdGeneratorIsConfiguredWithAttributesFromTheManager() {
System.setProperty("jvmRoute", "test");
try {
TomcatEmbeddedServletContainerFactory factory = getFactory();
this.container = factory.getEmbeddedServletContainer();
this.container.start();
}
finally {
System.clearProperty("jvmRoute");
}
Tomcat tomcat = ((TomcatEmbeddedServletContainer) this.container).getTomcat();
Context context = (Context) tomcat.getHost().findChildren()[0];
SessionIdGenerator sessionIdGenerator = context.getManager()
.getSessionIdGenerator();
assertThat(sessionIdGenerator).isInstanceOf(LazySessionIdGenerator.class);
assertThat(sessionIdGenerator.getJvmRoute()).isEqualTo("test");
}
@Override
protected Wrapper getJspServlet() {
Container context = ((TomcatEmbeddedServletContainer) this.container).getTomcat()

Loading…
Cancel
Save