单点登录(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> <

【原】通过Spring-Session实现不同系统之间的单点登录的更多相关文章

  1. 第04项目:淘淘商城(SpringMVC+Spring+Mybatis)【第十天】(单点登录系统实现)

    https://pan.baidu.com/s/1bptYGAb#list/path=%2F&parentPath=%2Fsharelink389619878-229862621083040 ...

  2. C/S架构程序多种类服务器之间实现单点登录(转)

    (一) 在项目开发的过程中,经常会出现这样的情况:我们的产品包括很多,以QQ举例,如登陆.好友下载.群下载.网络硬盘.QQ游戏.QQ音乐等,总不能要求用户每次输入用户名.密码吧,为解决这个问题,高手提 ...

  3. C/S架构程序多种类服务器之间实现单点登录

    (一) 在项目开发的过程中,经常会出现这样的情况:我们的产品包括很多,以QQ举例,如登陆.好友下载.群下载.网络硬盘.QQ游戏.QQ音乐等,总不能要求用户每次输入用户名.密码吧,为解决这个问题,高手提 ...

  4. 你真的知道什么是【共享Session】,什么是【单点登录】吗?

    一直有人问,为什么我实现的共享session不能单点登录,今天我也抽时间准备好好说一下. 我要喷(别喷我) 首先,网上水货文章很多,CSDN居多.CSDN转载率很高,也就是说同相同文章有很多,换汤不换 ...

  5. 170810、spring+springmvc+Interceptor+jwt+redis实现sso单点登录

    在分布式环境中,如何支持PC.APP(ios.android)等多端的会话共享,这也是所有公司都需要的解决方案,用传统的session方式来解决,我想已经out了,我们是否可以找一个通用的方案,比如用 ...

  6. Spring boot security权限管理集成cas单点登录

    挣扎了两周,Spring security的cas终于搞出来了,废话不多说,开篇! Spring boot集成Spring security本篇是使用spring security集成cas,因此,先 ...

  7. .NET Core2.0+MVC 用session,cookie实现的sso单点登录

    博主刚接触.NET Core2.0,想做一个单点登录的demo,所以参考了一些资料,这里给上链接: 1.http://www.cnblogs.com/baibaomen/p/sso-sequence- ...

  8. Spring Cloud OAuth2 实现用户认证及单点登录

    文章较长,建议推荐,建议转发,建议收藏,建议关注公众号哈. OAuth 2 有四种授权模式,分别是授权码模式(authorization code).简化模式(implicit).密码模式(resou ...

  9. 阶段5 3.微服务项目【学成在线】_day16 Spring Security Oauth2_02-用户认证技术方案-单点登录

    2 用户认证技术方案 2.1 单点登录技术方案 分布式系统要实现单点登录,通常将认证系统独立抽取出来,并且将用户身份信息存储在单独的存储介质,比如: MySQL.Redis,考虑性能要求,通常存储在R ...

随机推荐

  1. Docker之存储管理

    本文由作者邹珍珍授权网易云社区发布. 本文主要介绍Docker的存储管理.Docker拥有镜像分层,写时复制机制以及内容寻址存储等特征,为了支持这些特征,Docker设计了一套镜像元数据管理机制来管理 ...

  2. brew - 更换国内源

    brew如果不换成国内源,安装软件时候可能会出问题,不是安装不了就是速度很慢,所以使用它,更换国内游是比较好的选择! 我更换的是清华大学开源软件镜像站,打开shell窗口,依次执行下面命令: cd & ...

  3. 1002. Find Common Characters

    Given an array A of strings made only from lowercase letters, return a list of all characters that s ...

  4. Educational Codeforces Round 34 (Rated for Div. 2) D - Almost Difference(高精度)

    D. Almost Difference Let's denote a function You are given an array a consisting of n integers. You ...

  5. Sublime Text 格式化JSON-pretty json

    1.安装install package 按control + `,打开命令输入框 输入一下命令: import urllib2,os; pf='Package Control.sublime-pack ...

  6. VmWare扩展硬盘分区

    扩展硬盘 对硬盘进行分区 () 查看现有的硬盘分区情况 [mysql@china ~]$ df -h Filesystem Size Used Avail Use% Mounted on /dev/m ...

  7. leetcode-166-分数到小数(用余数判断有没有出现小数的循环体)

    题目描述:   给定两个整数,分别表示分数的分子 numerator 和分母 denominator,以字符串形式返回小数. 如果小数部分为循环小数,则将循环的部分括在括号内. 示例 1: 输入: n ...

  8. Winform—C#读写config配置文件

    现在FrameWork2.0以上使用的是:ConfigurationManager或WebConfigurationManager.并且AppSettings属性是只读的,并不支持修改属性值. 一.如 ...

  9. Linux grep命令详解[备份]

    linux grep命令 1.作用Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹 配的行打印出来.grep全称是Global Regular Expressi ...

  10. JVM-Java8的MetaSpace

    Java 8 彻底将永久代 (PermGen) 移除出了 HotSpot JVM,将其原有的数据迁移至 Java Heap 或 Metaspace 为什么取消了永久代而用MetaSpace代替了永久代 ...