原博客地址:http://jinnianshilongnian.iteye.com/blog/2018398

根据下载的pdf学习。

第十章 会话管理(一)

10.1 会话

shiro提供的会话可以用于JavaSE/JavaEE环境,不依赖于任何底层容器,可以独立使用,是完整的会话模块。即直接使用shiro的会话管理替换如web容器的会话管理。

获取会话及其相关信息的一些代码:

 login("classpath:shiro.ini", "zhang", "123");
Subject subject = SecurityUtils.getSubject(); //获取session对象,如果当前没有session对象,则创建一个
Session session = subject.getSession();
//同上
Session session = subject.getSession(true);
//获取session对象,如果当前没有session对象,返回null
Session session = subject.getSession(false); //获取当前会话的唯一id
session.getId(); //获取当前Subject的主机地址
session.getHost(); //获取/设置当前session的过期时间,默认为会话管理器的全局过期时间
session.getTimeout();
session.setTimeout(毫秒); //获取会话启动时间
session.getStartTimestamp();
//获取会话最后访问时间
session.getLastAccessTime();
//更新会话最后访问时间
session.touch();
//销毁会话
session.stop(); //设置/获取/删除会话属性,在整个会话范围都可以对这些属性操作
session.setAttribute("key", "123");
Assert.assertEquals("123", session.getAttribute("key"));
session.removeAttribute("key");

10.2 会话管理器 sessionManager

shiro提供了三个默认实现:

(1)DefaultSessionManager:用于JavaSE环境

(2)ServletContainerSessionManager:用于Web环境,直接使用servlet容器的会话。

(3)DefaultWebSessionManager:用于web环境,自己维护会话。

我的项目中用的(3)。globalSessionTimeout是会话的全局过期时间,单位ms。可以单独设置每个session的timeout属性,来为每个session设置超时时间。

 <bean id="sessionManager" class="org.apache.shiro.web.session.mgt.DefaultWebSessionManager">
<property name="globalSessionTimeout" value="86400000"/>
<property name="deleteInvalidSessions" value="true"/>
<property name="sessionValidationSchedulerEnabled" value="true"/>
<property name="sessionValidationScheduler" ref="sessionValidationScheduler"/>
<property name="sessionDAO" ref="sessionDAO"/>
<property name="sessionIdCookieEnabled" value="true"/>
<property name="sessionIdCookie" ref="sessionIdCookie"/>
</bean>

10.3 会话监听器

10.4 会话存储/持久化

shiro提供SessionDAO用于对话的CRUD。

我的项目中:

     <!--会话DAO-->
<bean id="sessionDAO" class="org.apache.shiro.session.mgt.eis.EnterpriseCacheSessionDAO">
<property name="activeSessionsCacheName" value="shiro-activeSessionCache"/>
<property name="sessionIdGenerator" ref="sessionIdGenerator"/>
</bean>

shiro提供了使用ehcache进行会话存储。ehcache可以配合TerraCotta实现容器无关的分布式集群。

     <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<property name="authorizer" ref="aasModularRealmAuthorizer"/>
<property name="realm" ref="authServiceRealm"/>
<property name="cacheManager" ref="springCacheManager"/>
</bean> <!-- 缓存管理器 -->
<bean id="springCacheManager" class="com.baosight.aas.auth.cache.SpringCacheManagerWrapper">
<property name="cacheManager" ref="cacheManager"/>
</bean>
     <!-- cache manager -->
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
<property name="cacheManager" ref="ehcacheManager"/>
</bean> <!-- 采用EhCache-->
<bean id="ehcacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation" value="classpath:ehcache/ehcache.xml"/>
<property name="shared" value="true"/>
</bean>

2017.2.9 开涛shiro教程-第十章-会话管理(一)的更多相关文章

  1. 2017.2.16 开涛shiro教程-第十七章-OAuth2集成(一)服务器端

    原博客地址:http://jinnianshilongnian.iteye.com/blog/2018398 根据下载的pdf学习. 开涛shiro教程-第十七章-OAuth2集成 1.OAuth2介 ...

  2. 2017.2.15 开涛shiro教程-第二十一章-授予身份与切换身份(二) controller

    原博客地址:http://jinnianshilongnian.iteye.com/blog/2018398 根据下载的pdf学习. 开涛shiro教程-第二十一章-授予身份与切换身份(二) 1.回顾 ...

  3. 2017.2.15 开涛shiro教程-第二十一章-授予身份与切换身份(一) table、entity、service、dao

    原博客地址:http://jinnianshilongnian.iteye.com/blog/2018398 根据下载的pdf学习. 第二十一章 授予身份与切换身份(一) 1.使用场景 某个领导因为某 ...

  4. 2017.2.12 开涛shiro教程-第七章-与Web集成

    2017.2.9 开涛shiro教程-第七章-与Web集成(一) 原博客地址:http://jinnianshilongnian.iteye.com/blog/2018398 根据下载的pdf学习. ...

  5. 2017.4.12 开涛shiro教程-第十八章-并发登录人数控制

    原博客地址:http://jinnianshilongnian.iteye.com/blog/2018398 根据下载的pdf学习. 开涛shiro教程-第十八章-并发登录人数控制 shiro中没有提 ...

  6. 2017.2.16 开涛shiro教程-第十七章-OAuth2集成(二)客户端

    原博客地址:http://jinnianshilongnian.iteye.com/blog/2018398 根据下载的pdf学习. 开涛shiro教程-第十七章-OAuth2集成 3.客户端 客户端 ...

  7. 2017.2.13 开涛shiro教程-第十二章-与Spring集成(一)配置文件详解

    原博客地址:http://jinnianshilongnian.iteye.com/blog/2018398 根据下载的pdf学习. 第十二章-与Spring集成(一)配置文件详解 1.pom.xml ...

  8. 2017.2.13 开涛shiro教程-第十二章-与Spring集成(二)shiro权限注解

    原博客地址:http://jinnianshilongnian.iteye.com/blog/2018398 根据下载的pdf学习. 第十二章-与Spring集成(二)shiro权限注解 shiro注 ...

  9. 2017.2.12 开涛shiro教程-第八章-拦截器机制

    原博客地址:http://jinnianshilongnian.iteye.com/blog/2018398 根据下载的pdf学习. 1.拦截器介绍 下图是shiro拦截器的基础类图: 1.Namea ...

随机推荐

  1. Vue在tradingView遇到的问题

    K线图刷新或重新加载时闪白 首先需要了解的是,闪白是 iframe的机制 所以只要解决掉iframe就可以了 首先找到 charting_library.min.js 搜索 找到配置项 style=& ...

  2. 初学Linux 命令

    查看ip:ifconfig 切换用户:us root(root为用户名) 显示当前目录:pwd 列出当前目录下所有文件:ls 进入某个目录 :cd 创建一个文件夹:mkdir 创建多个目录(当没有该父 ...

  3. s debug

    value stack contents   ognl 值栈 stack context           action上下文 action上下文是一个map对象,通过#key获得对象内容,在#re ...

  4. PHP排序的几种方法

    // 冒泡排序 function BubbleSort($arr) { // 获得数组总长度 $num = count($arr); // 正向遍历数组 for ($i = 1; $i < $n ...

  5. POJ 1815 Friendship(字典序最小的最小割)

    Friendship Time Limit: 2000MS   Memory Limit: 20000K Total Submissions: 10744   Accepted: 2984 Descr ...

  6. [国家集训队][bzoj2038] 小Z的袜子 [莫队]

    题面: 传送门 思路: 又是一道标准的莫队处理题目,但是这道题需要一点小改动:求个数变成了求概率 我们思考:每次某种颜色从i个增加到i+1个,符合要求的情况多了多少? 原来的总情况数是i*(i-1)/ ...

  7. BZOJ3531 [Sdoi2014]旅行 【树剖 + 线段树】

    题目 S国有N个城市,编号从1到N.城市间用N-1条双向道路连接,满足 从一个城市出发可以到达其它所有城市.每个城市信仰不同的宗教,如飞天面条神教.隐形独角兽教.绝地教都是常见的信仰.为了方便,我们用 ...

  8. vue的roter使用

    1在src下建立router文件夹,再建立router.js import Vue from 'vue' import Router from 'vue-router' import home fro ...

  9. jsp导出table数据excel表

    <html> <head> <meta http-equiv="content-Type" content="text/html;chars ...

  10. POJ1671 Rhyme Schemes

    Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 1776   Accepted: 984   Special Judge De ...