Home  >  Article  >  Java  >  How to set session timeout in SpringBoot Session

How to set session timeout in SpringBoot Session

WBOY
WBOYforward
2023-05-15 14:37:062208browse

Problem discovery

The springboot project produces session-out timeout problem. Describe the problem:

Configure session-out in the test environment by changing application.yaml, and verify the session-out by setting different times. The out configuration took effect, so the expiration time was directly set to 8 hours and released to the production environment. However, I received customer feedback at noon that the project expiration time was set to be short. If no operation is performed for half an hour, the session will expire and require repeated login.

Solution

Development environment: The springboot project has built-in Tomcat, so the session-out configured in application.yaml in the project is effective.

Production environment: The production environment is released through cloud services (Docker K8s) and Docker to build images. However, the session-out in the web.xml of the basic image tomacat is set to 30 minutes.

Solution: Finally, when Docker builds the image, put the modified web.xml into the DockerFile, overwrite the original web.xml of the base image, and build the image again to successfully replace the web.xml of the original base image. Problem It was eventually resolved.

Seesion invalidation: Create a session from the time the user logs in. When the user stops operating for longer than the session-out setting time, the session expires.

1. Set in the project's web.xml

[html] view plain copy<!-- 时间单位为分钟   -->  <session-config>
      <session-timeout>15</session-timeout></session-config>

2. Set in the web container (here, tomcat is used as an example)

[html] view plain copy  <!-- ==================== Default Session Configuration ================= -->  
  <!-- You can set the default session timeout (in minutes) for all newly   -->  
  <!-- created sessions by modifying the value below.    -->  
	<session-config>  
        <session-timeout>30</session-timeout>  
	</session-config>

3. Set through Java code

session.setMaxInactiveInterval(30*60);//以秒为单位

4.springboot project application.yaml settings

server:
   port: 8089
   session:
      timeout: 1800  #以秒为单位

5.Copy web.xml to DockerFile

COPY ./web.xml /opt/tpapp/tomcat/conf

The above is the detailed content of How to set session timeout in SpringBoot Session. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete