How to disable Tomcat session persistence in Spring Boot via Manager pathname?

This behavior can be customized via application.properties:

server.servlet.session.persistent=false # Whether to persist session data between restarts.

Session persistence is disabled by default in Spring Boot 2.x.


You can use a TomcatContextCustomizer to access the manager and apply the necessary configuration:

@Bean
public EmbeddedServletContainerFactory servletContainer() {
    TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory();
    tomcat.addContextCustomizers(new TomcatContextCustomizer() {

        @Override
        public void customize(Context context) {
            if (context.getManager() instanceof StandardManager) {
                ((StandardManager) context.getManager()).setPathname("");
            }
        }
    });
    return tomcat;
}