原博客地址: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. js跨域post请求

    function funPostBack(srvMethod){ /* var contentNR=$(document.getElementById("reportFrame") ...

  2. mybitis中对象字段与表中字段名称不匹配(复制)

    开发中,实体类中的属性名和对应的表中的字段名不一定都是完全相同的,这样可能会导致用实体类接收返回的结果时导致查询到的结果无法映射到实体类的属性中,那么该如何解决这种字段名和实体类属性名不相同的冲突呢? ...

  3. OgnlContext 源码

    // Copyright (c) 1998-2004, Drew Davidson and Luke Blanshard package ognl; import ognl.enhance.Local ...

  4. hdu 4289 网络流拆点,类似最小割(可做模板)邻接矩阵实现

    Control Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Sub ...

  5. 使用xcache加速PHP运行

    XCache 是一个开源的 opcode 缓存器/优化器, 这意味着他能够提高您服务器上的 PHP 性能. 他通过把编译 PHP 后的数据缓冲到共享内存从而避免重复的编译过程, 能够直接使用缓冲区已编 ...

  6. jQuery操作DOM基础 - 元素属性的查看与设置

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. 【Luogu】P2403所驼门王的宝藏(强连通分量)

    题目链接 想到缩点后DP这题就迷之好做 横天门就点向该行连一条边 纵门就点向该列连一条边 ziyou门直接枚举……map搞搞……话说ziyou门为啥是违规内容不让我发布? 然后缩点,DP,1A 不过写 ...

  8. mysql查看语句执行状态的常见函数

    row_count() 返回上一个insert.update.delete语句的影响行数 found_rows() 返回上一个select查到的记录条数 last_insert_id() 返回上一次插 ...

  9. BZOJ4555 [Tjoi2016&Heoi2016]求和 【第二类斯特林数 + NTT】

    题目 在2016年,佳媛姐姐刚刚学习了第二类斯特林数,非常开心. 现在他想计算这样一个函数的值: S(i, j)表示第二类斯特林数,递推公式为: S(i, j) = j ∗ S(i − 1, j) + ...

  10. 重绘(repaints)与重排(reflows)

    当页面布局和几何属性改变时就需要"重排" 避免在修改样式的过程中使用 offsetTop, scrollTop, clientTop, getComputedStyle() 这些属性, 它们都会刷新渲 ...