您的位置:首页 > 数据库 > Redis

spring+redis+shiro 实现session共享

2017-12-19 18:29 525 查看
环境:

spring版本 4.3.5.RELEASE

redis版本 Redis-x64-3.2.100,关于redis-window版本的安装可以参照:Windows 安装Redis

代码:

redis属性文件:

redis_hostname=localhost
redis_port=6379
redis_pwd=密码...
spring-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:aop="http://www.springframework.org/schema/aop"
xmlns:p="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.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 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">
<bean id="redisHttpSessionConfiguration"
class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration">
<property name="maxInactiveIntervalInSeconds" value="600" />
</bean>

<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxTotal" value="100" />
<property name="maxIdle" value="10" />
</bean>

<bean id="jedisConnectionFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
destroy-method="destroy">
<property name="hostName" value="${redis_hostname}" />
<property name="port" value="${redis_port}" />
<property name="password" value="${redis_pwd}" />
<property name="timeout" value="3000" />
<property name="usePool" value="true" />
<property name="poolConfig" ref="jedisPoolConfig" />
</bean>

</beans> spring.xml,需要引入spring-redis.xml跟属性文件,如果是其它方式加载spring-redis.xml请忽略:
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<array>
<value>classpath:config/jdbc.properties</value>
<value>classpath:config/redis.properties</value>
</array>
</property>
</bean>
<import resource="spring-redis.xml"/>
web.xml,需要添加spring session共享的过滤器:

<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>两个项目上面的配置都一样,到这里基本上就结束啦,只要记得想要共享的session名称要一致就行,但是还有好几个坑在等着呢....

问题一,关于域名的坑
1,当我在门户网站用http://127.0.0.1...访问后台系统的时候,发现居然获取不到session,找啦老半天才知道原来后台跳转的时候写的是http://localhost...按道理http://localhost就是127.0.0.1嘛,但是不行,如果你用127.0.0.1,那另外一个也必须是127.0.0.1,如果是localhost的话在另外一边session就获取不到啦。两个项目域名要一致,后来查资料网上说顶级域名要一致。。。至此这个坑算是填了。。。

2,我们存储session的实体类必须要序列化,只需要实现Serializable接口就OK啦,嘿嘿嘿,到这里我以为就OK了,没想到又遇到啦一个错误,提示说我两边程序的实体类序列号不一致...还得做一件事情显示的把序列号给加上private static final long serialVersionUID = 6347820138758523572L;这样这个坑也填啦...

3,如果没有用其他工具来管理session的话,上面两点做好就行拉。但是我后台系统用啦shiro来管理用户权限,这样后台系统用的就会是shiro管理session,门户系统没有shiro的jar就会出现反序列化失败的错误....解决办法就是给门户加上shiro-web的jar包这样就OK啦。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  spring redis session共享