How to do it...

  1. 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;
}
  1. 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(); }
  1. Start the application by running ./gradlew clean bootRun.
  2. 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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.134.78.106