How Spring Session works
This article was first published on the official account https://mp.weixin.qq.com/s/KCOFv0nRuymkX79-RZi9eg of Wechat, the Internet technology of vivo.
Author: Zhang Zhenglin
The HTTP protocol itself is stateless. In order to save the session information, the browser Cookie identifies the session request through SessionID, and the server stores the session information with SessionID as key. In single-instance applications, the storage of the application process itself can be considered. With the growth of the application volume, it is necessary to expand horizontally, and the problem of multi-instance session sharing follows.
Spring Session is to solve the problem of multi-process session sharing. This article will introduce how to use Spring Session and how Spring Session works.
1. Introduce the background
When the application is deployed in Tomcat, the session is maintained by Tomcat memory. If the application deploys multiple instances, the session cannot be shared. Spring Session is to solve the problem of session sharing in distributed scenarios.
2. Usage
Spring Session supports storage in Hazelcast, Redis, MongoDB, and relational databases. This article mainly discusses the storage of session in Redis.
Web.xml configuration:
SpringSessionRepositoryFilter org.springframework.web.filter.DelegatingFilterProxy springSessionRepositoryFilter / *
The main configuration of Spring:
PRV poolConfigRfex = "jedisPoolConfig" 3, workflow
Tomcat web.xml parsing steps:
ContextInitialized (ServletContextEvent arg0); / / Listenerinit (FilterConfig filterConfig); / / Filterinit (ServletConfig config); / / Servlet
Initialization order: Listener > Filter > Servlet.
1) load SessionRepositoryFilter into the Spring container through Tomcat's listener.
RedisHttpSessionConfiguration was declared in the Spring configuration file in the previous section, and SessionRepositoryFilter was generated in its parent class SpringHttpSessionConfiguration:
@ Beanpublic SessionRepositoryFilter