原博客地址: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. [oldboy-django][2深入django]后台生成form标签并设置标签的属性

    # Form生成html标签 a. 通过Form生成Input输入框,Form标签,以及submit标签还是要在前端写的, 但是Form标签内的Input标签可以在后台实现:只需要按以下步骤 - vi ...

  2. 打开任意位置的webConfig

    请阅读原文:打开任意的配置文件 翻翻ConfigurationManager的签名,有一个方法吸引了我的注意:OpenExeConfiguration(string exePath).看上去我可以把B ...

  3. 【bzoj1367】[Baltic2004]sequence 可并堆

    题目描述 输入 输出 一个整数R 样例输入 7 9 4 8 20 14 15 18 样例输出 13 题解 可并堆,黄源河<左偏树的特点及其应用>Page 13例题原题 #include & ...

  4. Educational Codeforces Round 22 E. Army Creation(分块好题)

    E. Army Creation time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  5. BZOJ1487 [HNOI2009]无归岛 【仙人掌dp】

    题目链接 BZOJ1487 题解 就是一个简单的仙人掌最大权独立集 还是不会圆方树 老老实实地树形Dp + 环处理 #include<iostream> #include<cstdi ...

  6. Codeforces Round #363 (Div. 2) C dp或贪心 两种方法

    Description Vasya has n days of vacations! So he decided to improve his IT skills and do sport. Vasy ...

  7. Twitter如何在数千台服务器上快速部署代码?

    答案是:用BT,也就是你我应该都很熟悉的BitTorrent. 对于网站经营者.创业者来说,扩展性的问题是在网站流量成长过程中势必会面对的问题,如何建立一个具有扩展性的架构(scalable arch ...

  8. What and How in an Application

    Basically, two concerntrations: 1. What: business docs and prototype design 2. How: technical docs a ...

  9. sooket数据成传输压缩

    样压缩不以文件为基础的数据 Q: 回答了两个使用Java进行数据压缩的问题. 第一个问题是: 我怎样才能压缩那些不在文件中的数据. 第二个问题是: 我以极大的热情阅读了Todd Sundsted的&q ...

  10. 《Linux命令行与shell脚本编程大全 第3版》Linux命令行---41

    以下为阅读<Linux命令行与shell脚本编程大全 第3版>的读书笔记,为了方便记录,特地与书的内容保持同步,特意做成一节一次随笔,特记录如下: