使用spring-session、redeis实现跨二级域名单点登录
2017-01-06 16:38
671 查看
提示:目前了解到的,此方式只能实现父级域名一致的子域名间单点登录,如用户登录了a.xx.com,再访问b.xx.com/xxx.do,则不需要再登录。
单点登录(个人理解,如有不足,请补充):由多个服务组成的一组服务,登录其中一个服务后,访问其他服务的受限资源,不再需要登录
实现方式(个人理解,如有不足,请补充):
1、共享session:本文基于此,只有让不同的tomcat共用一个session对象,才真正实现了session共享,从而实现单点
2、放弃session:引入认证中心,每次访问受限资源,携带token
3、只是单纯共享登录状态:重写ValveBase
,见http://blog.csdn.net/luka2008/article/details/38385703/
准备工作:
1、新建web工程(或者已经使用session的既有工程),基于maven
2、准备redis实例
3、修改pom,加入如下dependency
4、新建spring-session.xml,写入redis配置
注意:
注意,此处的父级域名xx.com要和spring-seesion.xml中,defaultCookieSerializer的domainName属性值配成一个,访问时,url也要用配置的对应项访问
情况1:domainName配成.xx.com,则验证时,url输入a.xx.com
情况2:domainName配成127.0.0.1,则验证时,url输入127.0.0.1
情况3:domainName配成localhost,则验证时,url输入localhost
这一点好多文章都没有写,这和session的创建和匹配机制有关,只有严格匹配了域名、path、访问路径才不会重复创建session,详细了解参考:跨域(二级域)session共享
7、验证
访问a.xx.com并登录,然后访问b.xx.com/xxx.do,则可正常访问。
总结:
优点:对原有系统代码零侵入,只需修改配置,便可实现单点登录
不足:不能实现跨顶级域名(设计之初,就没有往这个方向考虑大约)
刚开始研究没多久,如有不妥之处,请多多拍砖。
能够配置成功,参考了如下几篇文章,多谢多谢。
Spring-session & redis 子域名共享session:http://blog.csdn.net/beflyabot/article/details/51449315
SPRING SESSION实现单点登录 :http://blog.csdn.net/moxies8090/article/details/53355244
跨域(二级域)session共享:http://blog.csdn.net/luka2008/article/details/38385703/
单点登录(个人理解,如有不足,请补充):由多个服务组成的一组服务,登录其中一个服务后,访问其他服务的受限资源,不再需要登录
实现方式(个人理解,如有不足,请补充):
1、共享session:本文基于此,只有让不同的tomcat共用一个session对象,才真正实现了session共享,从而实现单点
2、放弃session:引入认证中心,每次访问受限资源,携带token
3、只是单纯共享登录状态:重写ValveBase
,见http://blog.csdn.net/luka2008/article/details/38385703/
准备工作:
1、新建web工程(或者已经使用session的既有工程),基于maven
2、准备redis实例
3、修改pom,加入如下dependency
<dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.5.2</version> </dependency> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>1.7.1.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.session</groupId> <artifactId>spring-session-data-redis</artifactId> <version>1.2.0.RELEASE</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-pool2</artifactId> <version>2.2</version> </dependency>
4、新建spring-session.xml,写入redis配置
<?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:p="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd" default-autowire="byName" default-lazy-init="true"> <!-- redis --> <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"> </bean> <!-- 设置Cookie domain 和 名称 --> <bean id="defaultCookieSerializer" class="org.springframework.session.web.http.DefaultCookieSerializer"> <property name="domainName" value=".xx.com"/> <property name="cookieName" value="JSESSIONID"/> <property name="cookiePath" value="/"></property> <!-- <property name="domainNamePattern" value="^.+?\.(\w+\.[a-z]+)$"></property>--> </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="usePool" value="true" /> </bean> <bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate"> <property name="connectionFactory" ref="jedisConnectionFactory" /> <property name="keySerializer"> <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" /> </property> <property name="valueSerializer"> <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" /> </property> <property name="hashKeySerializer"> <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" /> </property> <property name="hashValueSerializer"> <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" /> </property> </bean> <!-- 将session放入redis --> <bean id="redisHttpSessionConfiguration" class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration"> <property name="maxInactiveIntervalInSeconds" value="1800" /> <property name="cookieSerializer" ref="defaultCookieSerializer"/> </bean> </beans>5、修改web.xml,加入如下片段(我的工程是基于spring mvc、再集成spring-seesion)
注意:
springSessionRepositoryFilter必须放在所有filter的最前面
<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring.xml,classpath:spring-session.xml</param-value> </context-param> <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> <dispatcher>REQUEST</dispatcher> <dispatcher>ERROR</dispatcher> </filter-mapping>6、修改本地hosts文件,将127.0.0.1分别绑定a.xx.com、b.xx.com
注意,此处的父级域名xx.com要和spring-seesion.xml中,defaultCookieSerializer的domainName属性值配成一个,访问时,url也要用配置的对应项访问
情况1:domainName配成.xx.com,则验证时,url输入a.xx.com
情况2:domainName配成127.0.0.1,则验证时,url输入127.0.0.1
情况3:domainName配成localhost,则验证时,url输入localhost
这一点好多文章都没有写,这和session的创建和匹配机制有关,只有严格匹配了域名、path、访问路径才不会重复创建session,详细了解参考:跨域(二级域)session共享
7、验证
访问a.xx.com并登录,然后访问b.xx.com/xxx.do,则可正常访问。
总结:
优点:对原有系统代码零侵入,只需修改配置,便可实现单点登录
不足:不能实现跨顶级域名(设计之初,就没有往这个方向考虑大约)
刚开始研究没多久,如有不妥之处,请多多拍砖。
能够配置成功,参考了如下几篇文章,多谢多谢。
Spring-session & redis 子域名共享session:http://blog.csdn.net/beflyabot/article/details/51449315
SPRING SESSION实现单点登录 :http://blog.csdn.net/moxies8090/article/details/53355244
跨域(二级域)session共享:http://blog.csdn.net/luka2008/article/details/38385703/
相关文章推荐
- tomcat 使用 memcached管理session ,并且实现统一登录
- 使用cookie,session,filter等技术实现用户自动登录
- 引用 使用Session和Cookie实现网站自动登录
- 分布式Session之使用Spring-Session & Redis实现
- java中使用session监听实现同帐号登录限制、登录人数限制
- 使用memcache实现单点登录(session共享)
- 使用SQL Server保存Session状态,实现单点登录
- SpringMVC使用session实现简单登录
- tomcat使用redis共享session并实现单点登录
- 【SSM进阶之路】使用Spring SqlSessionTemplate API实现查询数据
- SpringMVC+Spring+mybatis 实现登录过程(使用后台验证框架)
- PHP中如何使用session实现保存用户登录信息
- 使用SQL Server保存Session状态,实现单点登录
- 使用SpringMVC+Angularjs实现登录功能
- ThreadLocal的实现原理,及使用实例,解决spring,hibernate非web项目下的懒加载 no session or session was closed(1)!
- 使用Spring3 实现用户登录以及权限认证
- java web使用session实现使用注册码码登录
- 菜鸟使用SSM框架搭建web服务器实现登录功能(Spring+SpringMVC+Mybatis)
- SPRING SESSION实现单点登录
- 使用Memcache实现Session共享(单点登录)的原理