SpringBootSecurity learning (09) Web version Login Configuration Session Sharing

Scenes

When the project back by the deployment of a change in the deployment after more than one address the session sharing problem most commonly used way is stored in the session redis such as cache. About session cookie and concepts will not repeat them here, in springboot-security environment, the session is stored in redis it is very, very simple to share, in addition to a few more configuration, almost no change any code. Sharing session to achieve the effect that, after the user logs in successfully on a single server above to access another one, the user is logged in. Create the following two different programs to configure session sharing.

Increased reliance

The session shared storage configuration in redis, you need to add two dependencies, one redis, the second is spring session:

file

Modify the configuration file

In the configuration file, redis need to configure some attributes of the data source and the session:

file

About session configuration, you can look according to ide tips:

file

You can see not only the session is stored in redis, it is also possible in a database or in MongoDB, but at the moment is to choose the most redis one way.

Add comment

Finally, add a comment in the class start @EnableRedisHttpSession:

file

test

First open the login page of a project, the login is successful,

file

Then open a browser, direct access to the home page of the second project, this time without logging direct access to:

file

Look redis stored in the session:

file

This realization of the session to share! In this case the one exit, the other will automatically exit, session data in redis will be automatically deleted.

Code Address: https://gitee.com/blueses/spring-boot-security 091 092

Guess you like

Origin www.cnblogs.com/guos/p/11614047.html