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

Example Analysis of Springboot2.X session sharing

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

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.

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

Internet Technology

Wechat

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

12
Report