- Let's say that we want our session to be for one minute. To make this happen, we will ad a ServletWebServerFactory bean to our WebConfiguration class with the following content:
@Bean
public ServletWebServerFactory servletContainer() {
TomcatServletWebServerFactory tomcat =
new TomcatServletWebServerFactory();
tomcat.getSession().setTimeout(Duration.ofMinutes(1));
return tomcat;
}
- Just for the purpose of demonstration, we will get the session from the request to force its creation. To do this, we will add a new request mapping to our BookController class with the following content:
@RequestMapping(value = "/session", method =
RequestMethod.GET) public String getSessionId(HttpServletRequest request) { return request.getSession().getId(); }
- Start the application by running ./gradlew clean bootRun.
- Let's open http://localhost:8080/books/session in the browser to see the following results:
If we wait for more than a minute and then reload this page, the session ID will change to a different one.