【原】通过Spring-Session实现不同系统之间的单点登录

导读:本篇文章讲解 【原】通过Spring-Session实现不同系统之间的单点登录,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

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

   过程:

        很早期的公司,一家公司可能只有一个Server,慢慢的Server开始变多了。每个Server都要进行注册登录,退出的时候又要一个个退出。用户体验很不好!你可以想象一下,上豆瓣 要登录豆瓣FM、豆瓣读书、豆瓣电影、豆瓣日记……真的会让人崩溃的。我们想要另一种登录体验:一家企业下的服务只要一次注册,登录的时候只要一次登录,退出的时候只要一次退出。怎么做?

     笔者之前所在的某公司项目是一个单体垂直架构,当初为了节省时间也没做拆分处理,只是集成了了Spring-Session和Redis。有一天突然发现在本地调试的时候关掉Tomcat再启动发现竟然用户的缓存还在。因为之前刚接触Servlet的时候做过一些小demo,知道用户登录后会为该用户分配一个session对象 当服务被关掉的时候会释放内存。

 项目拆分:

    过了一段时间,由于公司内部发展需要,要额外开发一个新的平台;于是讨论后打算要做成真正的单点登录,因为用户可能在原先平台登录后要去到另外一个新的平台,其中新的平台域名是:www.xxx.xx.com,旧平台域名是 www.xx.com,按照传统的开发,用户去到新的平台后拿不到session,会造成用户重新登录的状况。

改造过程:

  #首先思考下,为什么单体架构中Servlet 的Session在服务重启后会失效,原因在于重启后资源被释放,依赖的载体也不复存在,看过大型网站的演变过程那本书都知道为了统一管理缓存一般都会存储在第三方,例如Redis,MembeCache。

 #使用cookie作为媒介,存放用户凭证。把保存用户登陆信息的cookie的域设置成一样,这样由于一级二级域名都是共享的,所以他们的cookie也是共享的。然后在web.xml配置好DelegatingFilterProxy过滤器之后,Spring-session会对HttpServletRequest进行包装,核心就是每次取session信息都拿到浏览器的jsessionid的value再到redis去取。


 #Spring Session提供了一套创建和管理Servlet HttpSession的方案。Spring Session提供了集群Session(Clustered Sessions)功能,默认采用外置的Redis来存储Session数据,以此来解决Session共享的问题。

 

配置流程:

 #web.xml,核心过滤器。

    <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>*.do</url-pattern>
    </filter-mapping>

 

#spring-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:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">


    <bean id="redisHttpSessionConfiguration" class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration">
        <property name="maxInactiveIntervalInSeconds" value="1800" />
    </bean>

    <bean id="defaultCookieSerializer" class="org.springframework.session.web.http.DefaultCookieSerializer">
        <property name="domainName" value=".xxx.com" />
        <property name="useHttpOnlyCookie" value="true" />
        <property name="cookiePath" value="/" />
        <property name="cookieMaxAge" value="31536000" />
    </bean>

    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <property name="maxTotal" value="20"/>
    </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="poolConfig" ref="jedisPoolConfig" />
        <property name="password" value="redis"></property>
    </bean>

</beans>

 

 

property naehtpSessionStrategy" ref="cookieHttpSessionStrategy"/>
    </bean>
          设置cookieName和path
      <bean id="defaultCookieSerializer"  
        class="org.springframework.session.web.http.DefaultCookieSerializer">  
        <property name="cookieName" value="DTL_SESSION_ID" />  
        <property name="cookiePath" value="/" />  
    </bean>  
           
     <bean id="cookieHttpSessionStrategy"  
        class="org.springframework.session.web.http.CookieHttpSessionStrategy">  
        <property name="cookieSerializer" ref="defaultCookieSerializer" />  
    </bean>  

<

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/9794.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!