shiro-redis开源项目已经很好的将shiro与redis整合到一起,实现了将session存入redis,可以方便的用于session共享实现集群部署。

  git地址:https://github.com/alexxiyang/shiro-redis ,文档:http://alexxiyang.github.io/shiro-redis/

  官方的文档已经非常详细了,基本上照着文档进行修改原来的配置就可以了。

1.在原生的shiro基础上新增的jar包:

commons-pool2-2.6.1.jar
jedis-2.9.0.jar
shiro-redis-3.2.2.jar

2.修改配置:

下面是参考的配置,将对应的bean以及依赖关系修改为下面的配置即可

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd "> <!-- shiro-redis configuration [start] --> <!-- Redis Manager [start] -->
<bean id="redisManager" class="org.crazycake.shiro.RedisManager">
<property name="host" value="127.0.0.1:6379" />
</bean>
<!-- Redis Manager [end] --> <!-- Redis session DAO [start] -->
<bean id="redisSessionDAO" class="org.crazycake.shiro.RedisSessionDAO">
<property name="redisManager" ref="redisManager" />
</bean>
<bean id="sessionManager"
class="org.apache.shiro.web.session.mgt.DefaultWebSessionManager">
<property name="sessionDAO" ref="redisSessionDAO" />
</bean>
<!-- Redis session DAO [end] --> <!-- Redis cache manager [start] -->
<bean id="cacheManager" class="org.crazycake.shiro.RedisCacheManager">
<property name="redisManager" ref="redisManager" />
</bean>
<!-- Redis cache manager [end] --> <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<property name="sessionManager" ref="sessionManager" />
<property name="cacheManager" ref="cacheManager" /> <!-- other configurations -->
<property name="realm" ref="exampleRealm" />
<property name="rememberMeManager.cipherKey" value="kPH+bIxk5D2deZiIxcaaaA==" />
</bean> <!-- shiro-redis configuration [end] -->
</beans>

 需要注意:

  1.存入session的信息要实现Serializable接口(包括其依赖的成员属性等bean也要实现)

  2.对存入session的bean要有个唯一标识,且要设值到cacheManager,比如:

package cn.xm.exam.bean.system;

import java.io.Serializable;
import java.util.Date;
import java.util.List; public class User implements Serializable{
/**
*
*/
private static final long serialVersionUID = -3207880482640325843L; private String userid; private String useridcard; private String password; private String username; private String departmentname; private String departmentid; private String employeeid; private String userphoto; private String phone; private String isuse; private Date datatime; private Date logintime;
private List<Role> roles; //用户所拥有的角色 private List<String> permissions;//用户所拥有的权限code集合 public List<Role> getRoles() {
return roles;
} public void setRoles(List<Role> roles) {
this.roles = roles;
} public String getUserid() {
return userid;
} public void setUserid(String userid) {
this.userid = userid == null ? null : userid.trim();
} public String getUseridcard() {
return useridcard;
} public void setUseridcard(String useridcard) {
this.useridcard = useridcard == null ? null : useridcard.trim();
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password == null ? null : password.trim();
} public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username == null ? null : username.trim();
} public String getDepartmentname() {
return departmentname;
} public void setDepartmentname(String departmentname) {
this.departmentname = departmentname == null ? null : departmentname.trim();
} public String getEmployeeid() {
return employeeid;
} public void setEmployeeid(String employeeid) {
this.employeeid = employeeid == null ? null : employeeid.trim();
} public String getUserphoto() {
return userphoto;
} public void setUserphoto(String userphoto) {
this.userphoto = userphoto == null ? null : userphoto.trim();
} public String getIsuse() {
return isuse;
} public void setIsuse(String isuse) {
this.isuse = isuse == null ? null : isuse.trim();
} public String getDepartmentid() {
return departmentid;
} public void setDepartmentid(String departmentid) {
this.departmentid = departmentid;
} public String getPhone() {
return phone;
} public void setPhone(String phone) {
this.phone = phone;
} public List<String> getPermissions() {
return permissions;
} public void setPermissions(List<String> permissions) {
this.permissions = permissions;
} public Date getDatatime() {
return datatime;
} public void setDatatime(Date datatime) {
this.datatime = datatime;
} public Date getLogintime() {
return logintime;
} public void setLogintime(Date logintime) {
this.logintime = logintime;
} }

在配置cacheManager的时候如下:

    <!-- Redis cache manager [start] -->
<bean id="cacheManager" class="org.crazycake.shiro.RedisCacheManager">
<property name="redisManager" ref="redisManager" />
<!-- shiro-redis will call userInfo.getUserid() to get the id for storing Redis object. -->
<property name="principalIdFieldName" value="userid" />
</bean>
<!-- Redis cache manager [end] -->

最终我的完整的shiro相关配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd "> <!-- Redis Manager [start] -->
<bean id="redisManager" class="org.crazycake.shiro.RedisManager">
<property name="host" value="127.0.0.1:6379" />
</bean>
<!-- Redis Manager [end] --> <!-- Redis session DAO [start] -->
<bean id="redisSessionDAO" class="org.crazycake.shiro.RedisSessionDAO">
<property name="redisManager" ref="redisManager" />
</bean> <!-- Redis cache manager [start] -->
<bean id="cacheManager" class="org.crazycake.shiro.RedisCacheManager">
<property name="redisManager" ref="redisManager" />
<!-- shiro-redis will call userInfo.getUserid() to get the id for storing Redis object. -->
<property name="principalIdFieldName" value="userid" />
</bean>
<!-- Redis cache manager [end] --> <!-- 安全管理器 -->
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<property name="realm" ref="myRealm" />
<!-- 记住我 -->
<property name="rememberMeManager" ref="rememberMeManager" />
<!-- 注入缓存管理器 -->
<property name="cacheManager" ref="cacheManager" />
<!-- 注入session管理器 -->
<property name="sessionManager" ref="sessionManager" />
</bean> <!-- 自定义Realm -->
<bean id="myRealm" class="cn.xm.exam.utils.realm.MyRealm" /> <!-- 自定义form认证过虑器 -->
<!-- 基于Form表单的身份验证过滤器,不配置将也会注册此过虑器,表单中的用户账号、密码及loginurl将采用默认值,建议配置 -->
<bean id="formAuthenticationFilter"
class="org.apache.shiro.web.filter.authc.FormAuthenticationFilter "> <property name="usernameParam" value="username" /> <property name="passwordParam" value="password" /> <property name="rememberMeParam" value="rememberMe" />
</bean> <!-- ehcache缓存管理器 -->
<bean id="cacheManager222" class="org.apache.shiro.cache.ehcache.EhCacheManager">
<property name="cacheManagerConfigFile" value="classpath:shiro-ehcache.xml" />
</bean> <!-- session会话管理器 -->
<bean id="sessionManager"
class="org.apache.shiro.web.session.mgt.DefaultWebSessionManager">
<!-- session失效时间 单位毫秒 -->
<property name="globalSessionTimeout" value="18000000" />
<!-- 删除失效的session -->
<property name="deleteInvalidSessions" value="true" /> <property name="sessionDAO" ref="redisSessionDAO" />
</bean> <!-- rememberMeManager管理器,写cookie,取出cookie生成用户信息 -->
<bean id="rememberMeManager" class="org.apache.shiro.web.mgt.CookieRememberMeManager">
<property name="cookie" ref="rememberMeCookie" />
</bean>
<!-- 会话Cookie模板 -->
<bean id="sessionIdCookie" class="org.apache.shiro.web.servlet.SimpleCookie">
<constructor-arg value="sid" />
<property name="httpOnly" value="true" />
<property name="maxAge" value="-1" />
</bean>
<bean id="rememberMeCookie" class="org.apache.shiro.web.servlet.SimpleCookie">
<constructor-arg value="rememberMe" />
<property name="httpOnly" value="true" />
<property name="maxAge" value="2592000" /><!-- 30天 -->
</bean> <!-- 自定义form认证过虑器 -->
<bean id="permfilter" class="cn.xm.exam.utils.realm.ShiroPermsFilter"
scope="prototype">
</bean> <!-- logout -->
<bean id="logoutFilter" class="org.apache.shiro.web.filter.authc.LogoutFilter">
<!-- <property name="redirectUrl" value="/index.jsp" /> -->
<property name="redirectUrl" value="/view/index/studyMainpage2.jsp" />
</bean> <!-- shiro 过滤器 -->
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<property name="securityManager" ref="securityManager" />
<!-- loginUrl认证提交地址,如果没有认证将会请求此地址进行认证,请求此地址将由formAuthenticationFilter进行表单认证 -->
<!-- <property name="loginUrl" value="/index.jsp" /> -->
<property name="loginUrl" value="/view/index/studyMainpage2.jsp" />
<!-- 认证成功统一跳转到first.action,建议不配置,shiro认证成功自动到上一个请求路径 -->
<!-- <property name="successUrl" value="/login"/> -->
<!-- 通过unauthorizedUrl指定没有权限操作时跳转页面-->
<property name="unauthorizedUrl" value="unauthorized.jsp" /> <property name="filters">
<map>
<entry key="logout" value-ref="logoutFilter" />
</map>
</property> <!-- 过虑器链定义,从上向下顺序执行,一般将/**放在最下边 -->
<property name="filterChainDefinitions">
<value>
<!-- 对静态资源设置匿名访问 -->
/ = anon <!-- 不拦截首页的地址 -->
/index.jsp = anon
/WS/** = anon
/newsIP_**.action = anon
/train_**.action = anon
/dic_***.action = anon
/unauthorized.jsp = anon
/user_login.action = anon
/view/public/** = anon
/view/index/** = anon
/bs/** = anon
/controls/** = anon
/css/** = anon
/image/** = anon
/js/** = anon
/META-INF/** = anon <!-- 请求 logout.action地址,shiro去清除session-->
/logout.action = logout <!-- /** = authc 所有url都必须认证通过才可以访问-->
/** = authc </value>
</property>
</bean> <!-- 开启Shiro注解 -->
<!-- <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"
depends-on="lifecycleBeanPostProcessor"> <property name="proxyTargetClass"
value="true"></property> </bean> <bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
<property name="securityManager" ref="securityManager"/> </bean> --> <aop:config proxy-target-class="true"></aop:config>
<bean
class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
<property name="securityManager" ref="securityManager" />
</bean> <!-- 保证实现了Shiro内部lifecycle函数的bean执行 -->
<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor" />
</beans>

  接下来正常访问即可。session的修改以及清空都交给redis即可。我们手动清空redis之后相当于清除所有session。会将我们的信息都存入redis:

  对于集群的redis也都有相关介绍,对于哨兵模式的集群和主从复制的集群都有相关配置,需要的时候查阅文档就可以了。

我的项目的git地址:https://github.com/qiao-zhi/Exam/tree/sessionRedis

shiro-redis实现session存储到redis的更多相关文章

  1. C# redis 分布式session存储

    https://github.com/uliian/SessionExtentionStore 一个基于Redis的Session存储扩展方案,解决ASP.NET中Session的局限性和跨应用程序使 ...

  2. Django session存储到redis数据库

    把session存储到redis数据库,需要在setting中配置 django-redis 中文文档 http://django-redis-chs.readthedocs.io/zh_CN/lat ...

  3. 配置 PHP 的 Session 存储到 Redis

    PHP 的会话默认是以文件的形式存在的,可以配置到 NoSQL 中,即提高了访问速度,又能很好地实现会话共享,,,爽歪歪! 配置方式如下: 方法一:修改 php.ini 的设置 session.sav ...

  4. php Session存储到Redis的方法

    当然要写先安装php的扩展,可参考这篇文章:Redis及PHP扩展安装 修改php.ini的设置 复制代码 代码如下: session.save_handler = redis session.sav ...

  5. php session 存储到redis

    PHP 的会话默认是以文件的形式存在的,可以配置到 NoSQL 中,即提高了访问速度,又能很好地实现会话共享,,,爽歪歪! 配置方式如下: 方法一:修改 php.ini 的设置 1 2 session ...

  6. PHP如何配置session存储在redis

    当网站用户量增多的时候,正常的session存取就会出现有点慢的问题,如果提高速度呢. 我们可以使用redis去保存session的会话信息. PHP的会话默认是以文件的形式存在的,可以配置到NoSQ ...

  7. 自定义Redis作为Session存储服务提供

    之前看网上介绍可使用Redis自定义Session托管,使用第三方的Harbour.RedisSessionStateStore GitHub:https://github.com/TheCloudl ...

  8. PHP系列 | Session 存储在Redis

    默认是文件存储 修改php.ini的设置 session.save_handler = redis session.save_path = “tcp://127.0.0.1:6379″ 如果Redis ...

  9. Django框架中session存储到redis中的配置

    本文链接:https://blog.csdn.net/linqunbin/article/details/94786313————————————————版权声明:本文为CSDN博主「linqunbi ...

随机推荐

  1. Go-day06

    今日内容概要: 1.interface接口 2.反射 一.接口 接口定义 1.Interface类型可以定义一组方法,当时不需要实现,并且interface不能包含任何变量 2.接口定义 type e ...

  2. Java多线程常用面试题(含答案,精心总结整理)

    现在有T1.T2.T3三个线程,你怎样保证T2在T1执行完后执行,T3在T2执行完后执行? 目的是检测你对”join”方法是否熟悉.这个多线程问题比较简单,可以用join方法实现. 核心: threa ...

  3. MySQL常见报错汇总

    1>.ERROR 1290 (HY000): The MySQL server is running with the --secure-file-priv option so it canno ...

  4. my live health

    s 盐城的社保咨询服务热线电话:12333 射阳县医疗保险基金中心地址:射阳县合德镇解放东路24号 电话:0515-82322433 社保办事指南:http://yancheng.bendibao.c ...

  5. Java使用POI导入Excel异常Cannot get a text value from a numeric cell 解决

    异常原因:Excel数据Cell有不同的类型,当我们试图从一个数字类型的Cell读取出一个字符串并写入数据库时,就会出现Cannot get a text value from a numeric c ...

  6. hibernate关联关系映射之配置文件

    词汇解释 关系:事物之间相互作用.相互联系的状态.范围最大. 联系:在关系数据库中表示实体与实体之间的联系,1:1,1:n,m:n. 关联:表示对象之间的关系,既有数量性,又有方向性:动词:将对象之间 ...

  7. Linux记录-sysctl.conf优化方案

    Sysctl是一个允许您改变正在运行中的Linux系统的接口.它包含一些 TCP/IP 堆栈和虚拟内存系统的高级选项, 这可以让有经验的管理员提高引人注目的系统性能.用sysctl可以读取设置超过五百 ...

  8. Accord.NET Framework 介绍

    阅读目录 1.基本功能与介绍 Accord.NET Framework是在AForge.NET项目的基础上封装和进一步开发而来.因为AForge.NET更注重与一些底层和广度,而Accord.NET  ...

  9. Linux拉你入门

    前言:为了做一个更优秀的程序猿,Linux是必不可少的,因此利用闲杂的时间来增加自己对Linux的认识 (一)关于Linux命令编(至于怎样安装vmvare这一个章节就先不介绍了) 1.基础命令 1. ...

  10. jQuery中获取a标签的值

    如题,一组相同action的a标签,不同的是a标签的内容为搜索内容.点击页面显示不同的数据 刚开始试过在 a标签中添加 value值 和id 的值,结果在jQuery中获取值均失败! 后来发现,根本不 ...