Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the problem that the Springboot2 session setting timeout is invalid

2025-01-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article Xiaobian for you to introduce in detail "Springboot2 session setting timeout invalid how to solve", the content is detailed, the steps are clear, the details are handled properly, I hope this "Springboot2 session setting timeout invalid how to solve" article can help you solve doubts, the following follow the editor's ideas slowly in-depth, together to learn new knowledge.

Question:

There is a problem of setting timeout in today's project. The application.properties change by SpringBoot2 has not taken effect.

Solution:

The server.* property is used to control the embedded container used by Spring Boot. Spring Boot will create an instance of the servlet container using one of the ServletWebServerFactory instances. These classes use the server.* property to configure a controlled servlet container (tomcat,jetty, etc.).

The server.* property does not apply when the application is deployed to an Tomcat instance as a war file. They are not applicable because you can use a preconfigured servlet container (because it is a remotely running service). Therefore, deploying to a remote Tomcat makes the server.* property useless.

1. Change the configuration file according to the post given online (if the Jar startup takes effect), as follows: server: servlet: session: timeout: PT1H # 1 hour expiration cookie: max-age: PT1H # 1 hour expiration

Note: PT1H means to set the session failure time to 1 hour.

Extension: Duration

Discover the setTimeouot method by looking at the springboot source code. Here, an instance of Duration is required.

Public void setTimeout (Duration timeout) {this.timeout = timeout;}

Duration is new in Java8 and is mainly used to calculate the date difference. Duration is declared by final and is thread-safe.

If you convert a string, it is similar to the date formatted by SimpleDateFormat

Duration strings are similar to positive and negative numbers: the default is positive, the negative begins with'-', followed by the time letter under 'PT',:

-'D'.-God.

'H'-hour

'M'-minutes

'S'-seconds

Each unit must start with a number, and the order of minutes and seconds must not be out of order, for example, PT2H3M2S equals-PT-2H-3M-2S.

two。 Set the session timeout for tomcat

1) under the conf directory of tomcat, change servler.xml:

2) change web.xml in the project:

twenty

3) change in the program

Session.setMaxInactiveInterval (30,60)

When you encounter the same problem, please look at the scarlet letter above and check it in order.

Test the code:

@ RestController@RequestMapping ("/ valid-time") public class TestController {@ GetMapping ("/ test") public String validTime (HttpServletRequest request, HttpServletResponse response) {HttpSession session = request.getSession (); int sessionTime= session.getMaxInactiveInterval (); return new StringBuilder ("sessionTime=") .append (sessionTime). ToString ();} time out of sync leads to Spring session failure.

A huge pit in which the time of the Linux server is out of sync and causes Spring session failure.

Due to the needs of the business, the original stand-alone environment is transformed into a cluster environment, and spring session + redis is chosen as the session sharing scheme in order not to modify the task.

After confirming the technical solution, I searched the online Barabara for a pile of information about spring session, looked through it and found no previous pits, and then started to work on it.

The process of installing redis is ignored.

According to the data step by step to add spring session to the project, the single node situation project ran successfully, no error was reported, and session was successfully written into redis.

Then, to be on the safe side, I installed nginx on my computer and deployed three tomcat, and everything looked so perfect that session sharing was completed between multiple nodes.

So far, all the preliminary preparations have been completed, but the last step is needed.

The nightmare begins.

Deploy all the nodes online, and then open the browser to access the application smoothly, of course, we can't just stop at the end of the page, we have to log in anyway, so...

And then.

Countless times to enter the user's password, indicating that the login was successful, but the final result was rejected, o (╥ password ╥) o

Then there are countless trips to fill the hole.

Look at the log.

Look at all kinds of requests.

Suspect that spring session has BUG.

Even turn on remote DEBUG mode debugging, and finally see in the omnipotent DEBUG mode. When you get the getSession of spring session, if you get the session, you will first judge whether the session has expired. The way of comparison is very simple, that is, you can get the current system time to compare with the expiration time of session. If the current time is less than the expiration time, then identify that the session has not expired. Seeing this, there was an instant sense of enlightenment, and the small universe finally broke out here.

The session obtained by Nima-> is all out of date, and then. And then. Of course, I ran to see the server time, so. Crying o (╥ crying ╥) o, it turned out that Nima was you who tricked me.

In order to commemorate this trip to lie in the pit, this article is specially posted.

By the way, record the time synchronization of the Linux server.

Date command:

Date: check the current time, and the result is as follows: Tue Mar 4 01:36:45 CST 2017

Date-s 09:38:40: set the current time, the result is as follows: Tue Mar 4 09:38:40 CST 2017

Ntpdate command:

Ntpdate-u ntp.api.bz: network time synchronization command

Ntp commonly used servers:

China National time Service Center: 210.72.145.44

NTP Server (Shanghai): ntp.api.bz

Read here, this article "Springboot2 session setting timeout invalid how to solve" article has been introduced, want to master the knowledge of this article still need to practice and use to understand, if you want to know more about the article, welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report