In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
Today, I will talk to you about how to achieve session sharing based on Spring-Session, which may not be well understood by many people. in order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.
Some time ago, when adjusting the permission framework of the project (SpringSecurity changed to Shiro), the SpringSession widget was integrated.
Due to some features of this project, distributed deployment can not be carried out for the time being, so there is no in-depth testing of SpringSession and deployment of applications in production. Here we just record the process of integration and some experiences.
Some students on the Internet also said that there are some holes in SpringSession, such as unable to achieve cross-domain Session sharing, and then can be solved by modifying its Cookie production strategy, so full testing is needed before production and use.
Learn the @ Configuration and @ Bean Spring annotations:
To reduce configuration in xml, @ Configuration and @ Bean annotation classes are introduced in Spring. The class annotated by @ Configuration is equivalent to configuring beans in XML, indicating that the class is the source defined by beans; the method annotated by @ Bean is equivalent to the bean configured in XML, and the instance created by this method is managed by the Ioc container.
The configuration class of the SpringSession core is SpringHttpSessionConfiguration (the framework also provides Redis, Mongo, Jdbc and other implementations by default, and RedisHttpSessionConfiguration is used in this project). The two most important operations in the RedisHttpSessionConfiguration class are the creation of springSessionRepositoryFilter and sessionRedisTemplate. When creating sessionRedisTemplate, the framework relies on parsing RedisConnectionFactory, so in the Spring configuration, we need to define that RedisConnectionFactory;springSessionRepositoryFilter is the core of SpringSession and the Filter proxy in web.xml, in which the acquisition and submission of the entire Session are implemented.
The following is the whole process of configuration:
1. ApplicationContext-session.xml is configured as follows:
2. The property file config.properties is configured as follows:
# # redis pool configredis.pool.maxTotal=100redis.pool.maxIdle=20redis.pool.minIdle=10redis.pool.blockWhenExhausted=trueredis.pool.maxWaitMillis=3000redis.pool.testOnBorrow=falseredis.pool.testOnReturn=falseredis.pool.testWhileIdle=trueredis.pool.minEvictableIdleTimeMillis=60000redis.pool.timeBetweenEvictionRunsMillis=30000redis.pool.numTestsPerEvictionRun=-1# redis configredis.host=192.168.0.123redis.port=6379redis.password=admin-123456redis.database=0redis.timeout=3600
3. Web.xml is configured as follows:
Editor contextConfigLocation classpath:spring/applicationContext.xml Classpath:spring/applicationContext-security.xml org.jasig.cas.client.session.SingleSignOutHttpSessionListener org.springframework.web.context.ContextLoaderListener org.springframework.web.util.IntrospectorCleanupListener org.springframework.web.context.request.RequestContextListener CAS SingleSignOutFilter com.wuzb.pw.common.shiro.CustomSingleSignOutFilter CAS SingleSignOutFilter / * CharacterEncodingFilter org.springframework.web.filter.CharacterEncodingFilter Encoding utf-8 forceEncoding true CharacterEncodingFilter / * shiroFilter Org.springframework.web.filter.DelegatingFilterProxy true targetFilterLifecycle true shiroFilter / * springSessionRepositoryFilter org.springframework.web .filter.DelegatingFilterProxy springSessionRepositoryFilter / * dispatcherServlet org.springframework.web.servlet.DispatcherServlet contextConfigLocation classpath:spring/applicationContext-mvc.xml DispatcherServlet /
The Spring version of 4.1.6.RELEASE used in the project. The Jar required to integrate Spring Session is as follows:
Spring-data-commons-1.12.3.RELEASE.jar
Spring-data-keyvalue-1.1.3.RELEASE.jar
Spring-data-redis-1.7.1.RELEASE.jar
Spring-session-1.2.2.RELEASE.jar
Spring-session-data-redis-1.2.2.RELEASE.jar
Jedis-2.8.1.jar
Points to pay attention to:
Spring-session-data-redis-1.2.2.RELEASE.jar depends on spring-data-redis-1.7.1.RELEASE.jar by default
If you use spring-data-redis-1.7.3.RELEASE or later, the following exception will occur:
Caused by: java.lang.NoSuchMethodError: org.springframework.core.serializer.support.DeserializingConverter. (Ljava/lang/ClassLoader ) V at org.springframework.data.redis.serializer.JdkSerializationRedisSerializer. (JdkSerializationRedisSerializer.java:53) at org.springframework.data.redis.core.RedisTemplate.afterPropertiesSet (RedisTemplate.java:119) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods (AbstractAutowireCapableBeanFactory.java:1633) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean (AbstractAutowireCapableBeanFactory.java:1570)... 49 more
In the case of dealing with distributed Session, there are many solutions. For example, based on Tomcat middleware (TomcatRedisSessionManager) based on the server to do session sharing (limited); based on the SpringSession plug-in to achieve Session sharing; if you use the Shiro rights framework can also achieve their own SessionDao to achieve Session sharing. These will continue to be explored in the follow-up time.
After reading the above, do you have any further understanding of how to achieve conversation based on Spring-Session? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.