Reputation: 598
In Tomcat there is a well known configuration option in conf/context.xml
to disable session persistence:
<!-- Uncomment this to disable session persistence across Tomcat restarts -->
<Manager pathname="" />
When uncommented as shown here, the applied implementation of org.apache.catalina.Manager
(e.g. org.apache.catalina.session.StandardManager
) does not have a pathname
to tell it where to store sessions to the disk, and thus it does not write session files to disk (e.g. on shutdown), which is what we want.
In other words, this disables the standard Tomcat feature to sustain sessions through server restart.
How can the same be achieved in Spring Boot with embedded Tomcat?
Perhaps the Manager object can somehow be obtained to set the property pathname to null?
Upvotes: 25
Views: 13343
Reputation: 1440
Or like this, in case you are using application.yml
:
# Whether to persist session data between restarts.
server:
servlet:
session:
persistent: false
Upvotes: 2
Reputation: 2261
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.
Upvotes: 12
Reputation: 448
... and this for Spring Boot 2.0.x
@Bean
public TomcatServletWebServerFactory servletContainer() {
TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
tomcat.addContextCustomizers(new TomcatContextCustomizer() {
@Override
public void customize(Context context) {
if (context.getManager() instanceof StandardManager) {
((StandardManager) context.getManager()).setPathname("");
}
}
});
return tomcat;
}
Upvotes: 2
Reputation: 116051
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;
}
Upvotes: 18