Spring Session教程
Spring Session常见的应用场景
Spring Session的执行流程
Session会话管理

Spring Session单点登录

不同根域名下的项目实现Session共享,

比如阿里巴巴这样的公司,有多个业务线,多个网站,用户在一个网站登录,那么其他网站也会是登录了的状态,比如:登录了淘宝网,则天猫网也是登录的;

www.taobao.com

www.tmall.com

比如:www.web.com www.p2p.com www.dai.com

对于不同根域名的场景,要实现一处登录,处处登录,Spring Session不支持;

单点登录(Single Sign On),简称为 SSO,是目前比较流行的企业业务整合的解决方案之一。SSO的定义是在多个应用系统中,用户只需要登录一次就可以访问所有相互信任的应用系统。目前市面上有很多实现单点登录的方案,例如CAS,Token颁发校验,Cookie+域名+路径配置,在这里主要是想介绍一下第三种方案的实现方式。

概述
1. SpringSession提供了
• 一套管理ServletHttpSession的解决方案;
• 一台Cluster Session的解决方案,默认使用外置的Redis存储Session的方式解决Session共享的问题;

2. 相关网站:
• 官方网站:https://projects.spring.io/spring-session
• 文档地址:https://docs.spring.io/spring-session/docs/current/reference/html5
• 源码地址:https://github.com/spring-projects/spring-session

集成步骤

1. 导入maven(注意Spring的版本需要大于等于4.0.3,否在后面的过滤器会报异常):

<dependency>
    <groupId>org.springframework.session</groupId>
    <artifactId>spring-session-data-redis</artifactId>
    <version>1.2.0.RELEASE</version>
</dependency>

2. 添加过滤器SessionExpireFilter

<filter>
    <filter-name>sessionExpireFilter</filter-name>
    <filter-class>com.mmall.controller.common.SessionExpireFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>sessionExpireFilter</filter-name>
    <url-pattern>*.do</url-pattern>
</filter-mapping>

3. 在Spring的配置文件中添加配置:

<bean id="redisHttpSessionConfiguration" class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration">
    <property name="maxInactiveIntervalInSeconds" value="1800" />
</bean>
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
    <property name="maxTotal" value="20"/>
    <property name="maxIdle" value="10"/>
    <property name="minIdle" value="2"/>
    <property name="testOnBorrow" value="true"/>
    <property name="testOnReturn" value="false"/>
</bean>
<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
    <property name="hostName" value="127.0.0.1" />
    <property name="port" value="6379" />
    <property name="database" value="0"/>
    <property name="poolConfig" ref="jedisPoolConfig" />
</bean>

4. 添加单点登录Session的各种配置:

<bean id="defaultCookieSerializer" class="org.springframework.session.web.http.DefaultCookieSerializer">
    <property name="cookieName" value="USER_KEY"/>
    <property name="domainName" value=".shreker.org"/>
    <property name="useHttpOnlyCookie" value="true"/> <!-- Only In Servlet3 -->
    <property name="cookiePath" value="/"/>
    <property name="cookieMaxAge" value="1800"/>
</bean>

5. 正常编写session代码(注意:这里的response.getData()返回的对象必须实现接口java.io.Serializable),如:

session.setAttribute(Consts.USER_IN_SESSION,response.getData());
session.getAttribute(Const.USER_IN_SESSION);
session.removeAttribute(Const.USER_IN_SESSION);

6. 完成配置,需要注意:

• Spring-Session通过代理拦截我们设置的session信息,并使用Jedis存储到Redis上;

• Spring-Session在Redis上存储的不止用户的信息,还有两个关联的key;

• 当过期时间到时,Spring-Session会先去删除关联的key信息,最后过一段时间删除真正存储的用户信息;

• Redis中,当关联的key被删除之后,真正的信息删除之前的这一段时间里,我们也是无法获取真正存储在Redis中的信息的;

7. 使用到的相关的类:

• org.springframework.data.redis.connection.jedis.JedisConnectionFactory

• org.springframework.web.filter.DelegatingFilterProxy

•org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration

• org.springframework.session.web.http.DefaultCookieSerializer

• redis.clients.jedis.JedisPoolConfig

• org.springframework.session.web.http.SessionRepositoryFilter

• org.springframework.session.data.redis.RedisOperationsSessionRepository

• org.springframework.session.web.context.AbstractHttpSessionApplicationInitializer

• org.springframework.session.web.http.SessionRepositoryFilter.SessionRepositoryRequestWrapper

• org.springframework.session.web.http.SessionRepositoryFilter.SessionRepositoryResponseWrapper

• org.springframework.session.web.http.CookieHttpSessionStrategy

全部教程