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

What is the problem of distributed Session cross-domain sharing solved by Spring Session and Redis

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

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

This article to share with you is about Spring Session and Redis to solve the distributed Session cross-domain sharing problem is what, Xiaobian think quite practical, so share to everyone to learn, I hope you can read this article after some harvest, not much to say, follow Xiaobian to see it.

Using Spring Session and Redis to Solve the Problem of Cross-Domain Sharing of Distributed Session

Description of phenomenon:

In the project, the front and back code are not separated. In the case of two instances, the service runs normally. Occasionally, a prompt similar to the need to log in again will pop up, and the background will report an error message.

It's a processor anomaly. It's not obvious.

After adding the machine instance, when visiting the front-end page, the login page has been repeatedly visited, resulting in page 302. All kinds of signs indicate that it is caused by a problem with the login configuration.

Problem introduction: Session cannot be shared, resulting in polling between different machines requiring login, resulting in the final service exception

Solution: Use Spring Session and Redis to solve the distributed Session cross-domain sharing problem

Solution Configuration:

1 ) Add dependencies

org.springframework.session spring-session-data-redis 1.2.0.RELEASE org.apache.velocity velocity 1.7

2) web.xml configuration file added:

springSessionRepositoryFilter org.springframework.web.filter.DelegatingFilterProxy springSessionRepositoryFilter /*

3)Configuration of Spring.xml

Analysis:

1. DelegatingFilterProxy class in web: belongs to proxy fiter, which will transfer the management of filter to bean in spring when tomcat starts loading filter in web.xml, that is, the configuration in step 3 is introduced into RedisHttpSessionConfiguration.

2. RedisHttpSessionConfiguration inherits the SpringHttpSessionConfiguration class, which is very important. SpringHttpSessionConfiguration injects springSessionRepositoryFilter into the container by @Bean.

3. SessionRepositoryFilter This filter is the filter found by DelegatingFilterProxy. SessionRepositoryFilter is the key. How to associate it specifically?

If the init-param parameter is not specified, DelegatingFilterProxy will take filter-name as the Bean object to look for, which is also the role of DelegatingFilterProxy class. It can be seen that every request will pass through this filter, and requests that pass through this filter will also pass through the springSessionRepositoryFilter accordingly. Then we will take a look at the springSessionRepositoryFilter.

4. SessionRepositoryFilter replaces the default javax.servlet.http.HttpSession support for org.springframework.session.Session.

The main methods and properties of SessionRepositoryFilter are as follows:

5. SessionRepositoryResponseWrapper, SessionRepositoryRequestWrapper and HttpSessionWrapper are internal classes, which are also very critical. For example, SessionRepositoryRequestWrapper class

It can be seen that SessionRepositoryRequestWrapper inherits javax.servlet.http.HttpServletRequestWrapper, we know that the default implementation of HttpServletRequest interface is HttpServletRequestWrapper, as follows

6. Because SessionRepositoryRequestWrapper inherits HttpServletRequestWrapper, and HttpServletRequestWrapper implements HttpServletRequest interface, SessionRepositoryRequestWrapper rewrites some methods in HttpServletRequest interface, so there will be getSession, changeSessionId and other methods. At this point, we should roughly understand that the original request request and response have been repackaged. We also understand how the original HttpSeession was replaced by Spring Session.

We can see that there is already a SessionRepositoryRequestWrapper override method by looking at the specific implementation of request.getSession() through the shortcut key. There are two default implementations above, one is the original, one is the Spring Session implementation, which one is specifically selected as the implementation, this is the role of DelegatingFilterProxy we said above, he will filter every request, every request through DelegatingFilterProxy will also pass through springSessionRepositoryFilter filter, springSessionRepositoryFilter filter will realize the original request to SessionRepositoryRequestWrapper conversion, this is the implementation of the specific process!

request.getSession().setAttribute(name, value) implementation: trace code, you can reach the following content

You can see the Redis related actions! Now, we know how the Spring Session works! Although the process below is not introduced, it is already very clear.

The above is what Spring Session and Redis solve the distributed Session cross-domain sharing problem. Xiaobian believes that some knowledge points may be seen or used in our daily work. I hope you can learn more from this article. For more details, please 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

Database

Wechat

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

12
Report