基于Spring

49次阅读
没有评论

共计 6424 个字符,预计需要花费 17 分钟才能阅读完成。

今天就跟大家聊聊有关基于 Spring-Session 如何实现会话共享,可能很多人都不太了解,为了让大家更加了解,丸趣 TV 小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

前段时间,在对项目权限框架调整的时候(SpringSecurity 改成 Shiro),集成了 SpringSession 小插件。

  由于现今本项目某些特性,暂时无法进行分布式部署,所以对 SpringSession 并没有进行深入测试以及生产上部署应用,这里只是记录整合的过程以及一些心得。

  网上也看到一些同学说 SpringSession 存在一些坑,比如无法实现跨域 Session 共享,然后通过修改其 Cookie 生产策略可以进行解决等,因此在生产使用之前,还需进行充分的测试。

学习 @Configuration、@Bean 两个 Spring 注解:

Spring 中为了减少 xml 中配置,引入了 @Configuration 和 @Bean 注解类。@Configuration 注解的类,等价于 XML 中配置 beans,表明这个类是 beans 定义的源;@Bean 注解的方法等价于 XML 中配置的 bean,该方法创建出的实例由 Ioc 容器管理。

 SpringSession 核心的配置类为 SpringHttpSessionConfiguration(框架也默认提供了 Redis、Mongo、Jdbc 等实现,本项目使用 RedisHttpSessionConfiguration)。RedisHttpSessionConfiguration 类中最重要的两个操作就是创建 springSessionRepositoryFilter 以及 sessionRedisTemplate。在创建 sessionRedisTemplate 时框架会依赖解析 RedisConnectionFactory,所以在 Spring 配置中我们需要定义 RedisConnectionFactory;springSessionRepositoryFilter 是 SpringSession 的核心,也是 web.xml 中 Filter 代理,整个 Session 的获取以及提交都在其里实现。

以下是整个配置的过程:

1、applicationContext-session.xml 配置如下:

?xml version= 1.0  encoding= UTF-8 ? 
 beans xmlns= http://www.springframework.org/schema/beans 
  xmlns:xsi= http://www.w3.org/2001/XMLSchema-instance 
  xmlns:mvc= http://www.springframework.org/schema/mvc 
  xmlns:context= http://www.springframework.org/schema/context 
  xmlns:util= http://www.springframework.org/schema/util 
  xmlns:aop= http://www.springframework.org/schema/aop 
  xmlns:tx= http://www.springframework.org/schema/tx 
  xsi:schemaLocation= http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd 
 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd 
 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd 
 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd 
 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd  default-lazy-init= true 
 bean id= jedisPoolConfig   >

2、属性文件 config.properties 配置如下:

## redis pool config
redis.pool.maxTotal=100
redis.pool.maxIdle=20
redis.pool.minIdle=10
redis.pool.blockWhenExhausted=true
redis.pool.maxWaitMillis=3000
redis.pool.testOnBorrow=false
redis.pool.testOnReturn=false
redis.pool.testWhileIdle=true
redis.pool.minEvictableIdleTimeMillis=60000
redis.pool.timeBetweenEvictionRunsMillis=30000
redis.pool.numTestsPerEvictionRun=-1
# redis config
redis.host=192.168.0.123
redis.port=6379
redis.password=admin-123456
redis.database=0
redis.timeout=3600

3、web.xml 配置如下:

?xml version= 1.0  encoding= UTF-8 ? 
 web-app xmlns:xsi= http://www.w3.org/2001/XMLSchema-instance 
 xmlns= http://java.sun.com/xml/ns/javaee 
 xsi:schemaLocation= http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd 
 id= WebApp_ID  version= 2.5 
 display-name editor /display-name 
 context-param 
 param-name contextConfigLocation /param-name 
 param-value classpath:spring/applicationContext.xml,classpath:spring/applicationContext-security.xml /param-value 
 /context-param 
 listener  
 listener-class org.jasig.cas.client.session.SingleSignOutHttpSessionListener /listener-class  
 /listener 
 listener 
 listener-class org.springframework.web.context.ContextLoaderListener /listener-class 
 /listener 
 listener 
 listener-class org.springframework.web.util.IntrospectorCleanupListener /listener-class 
 /listener 
 listener 
 listener-class org.springframework.web.context.request.RequestContextListener /listener-class 
 /listener 
 !-- CAS  单点退出  -- 
 filter 
 filter-name CAS Single Sign Out Filter /filter-name 
 filter-class com.wuzb.pw.common.shiro.CustomSingleSignOutFilter /filter-class 
 /filter 
 filter-mapping 
 filter-name CAS Single Sign Out Filter /filter-name 
 url-pattern /* /url-pattern 
 /filter-mapping 
 !-- POST 乱码处理  -- 
 filter 
 filter-name CharacterEncodingFilter /filter-name 
 filter-class org.springframework.web.filter.CharacterEncodingFilter /filter-class 
 init-param 
 param-name encoding /param-name 
 param-value utf-8 /param-value 
 /init-param 
 init-param 
 param-name forceEncoding /param-name 
 param-value true /param-value 
 /init-param 
 /filter 
 filter-mapping 
 filter-name CharacterEncodingFilter /filter-name 
 url-pattern /* /url-pattern 
 /filter-mapping 
 filter 
 filter-name shiroFilter /filter-name 
 filter-class org.springframework.web.filter.DelegatingFilterProxy /filter-class 
 async-supported true /async-supported 
 init-param 
 param-name targetFilterLifecycle /param-name 
 param-value true /param-value 
 /init-param 
 /filter 
 filter-mapping 
 filter-name shiroFilter /filter-name 
 url-pattern /* /url-pattern 
 /filter-mapping 
 filter 
 filter-name springSessionRepositoryFilter /filter-name 
 filter-class org.springframework.web.filter.DelegatingFilterProxy /filter-class 
 /filter 
 filter-mapping 
 filter-name springSessionRepositoryFilter /filter-name 
 url-pattern /* /url-pattern 
 /filter-mapping  
 servlet 
 servlet-name dispatcherServlet /servlet-name 
 servlet-class org.springframework.web.servlet.DispatcherServlet /servlet-class 
 init-param 
 param-name contextConfigLocation /param-name 
 param-value classpath:spring/applicationContext-mvc.xml /param-value 
 /init-param 
 /servlet 
 servlet-mapping 
 servlet-name dispatcherServlet /servlet-name 
 url-pattern / /url-pattern 
 /servlet-mapping 
 welcome-file-list 
 welcome-file /welcome-file 
 /welcome-file-list 
 /web-app

项目中使用的 Spring 版本 4.1.6.RELEASE。其中集成 Spring Session 所需的 Jar 如下:

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

需要注意的点:

spring-session-data-redis-1.2.2.RELEASE.jar 默认依赖 spring-data-redis-1.7.1.RELEASE.jar,

若使用 spring-data-redis-1.7.3.RELEASE 或者更高版本的会出现如下异常:

Caused by: java.lang.NoSuchMethodError: org.springframework.core.serializer.support.DeserializingConverter. init (Ljava/lang/ClassLoader;)V
 at org.springframework.data.redis.serializer.JdkSerializationRedisSerializer. init (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

  在处理分布式 Session 的案例中,有很多种解决方案。例如基于 Tomcat 中间件(TomcatRedisSessionManager)基于服务器基本做会话共享(局限大);基于 SpringSession 插件实现 Session 共享;若使用 Shiro 权限框架还可以自己实现 SessionDao 来实现 Session 共享。这些在后续时间里,再继续探索。

看完上述内容,你们对基于 Spring-Session 如何实现会话共享有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注丸趣 TV 行业资讯频道,感谢大家的支持。

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2023-08-17发表,共计6424字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)