Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Example Analysis of Springboot2.X session sharing

Shulou Source: shulou.com Published: 2022-06-02 04:30:21 09月21日 Update

Springboot2.X session sharing example analysis, in view of this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible way.

General outline of SpringBoot2.X 's method of mind

Summary: session sharing is when different projects share a resource, such as single sign-on.

1. Pom depends on org.springframework.boot spring-boot-starter-data-redis org.springframework.session spring-session-data-redis2 and application.properties

Redis library 15 is selected by default.

Server.port=8082spring.redis.database=15spring.redis.host=localhostspring.redis.port=6379spring.redis.password=spring.redis.timeout=10000ms3, enable session caching

Add the following annotation EnableRedisHttpSession to the startup class.

@ SpringBootApplication@EnableRedisHttpSessionpublic class DockerApplication {public static void main (String [] args) {SpringApplication.run (DockerApplication.class, args);}} 4, Controller@Controllerpublic class FirstController {@ GetMapping (value = "/ session") @ ResponseBody public void session (HttpServletRequest request) {request.getSession () .setAttribute ("name", request.getRequestURL ()) } @ GetMapping (value = "/ gainSession") @ ResponseBody public void gainSession (HttpServletRequest request) {String name = request.getSession () .getAttribute ("name") .toString (); System.out.println (name);}} 5, Test

Start the project and run localhost:8082/session, and you will find that there will be relevant session records in redis library 15. The expiration time is 1800 (by default, you can set it in the code).

So you call http://localhost:8082/gainSession and the result is as follows:

6. Change the port number 8083server.port=8083

Changing the port number for the project means becoming another project so that when you call http://localhost:8083/gainSession again, see if you get session.

The result is the same, so it shows that we can achieve single sign-on in this way, but only if all projects use a common redis library, after all, session is stored in redis.

This is the answer to the sample analysis question about Springboot2.X session sharing. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.

Tags: Project analysis results questions examples slogans time more single point help login answer different easy same simple code content premise can be obtained through the Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple Shulou Technology macOS Redmi Huawei