2017.2.9 开涛shiro教程-第十章-会话管理(一)
原博客地址: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教程-第十章-会话管理(一)的更多相关文章
- 2017.2.16 开涛shiro教程-第十七章-OAuth2集成(一)服务器端
原博客地址:http://jinnianshilongnian.iteye.com/blog/2018398 根据下载的pdf学习. 开涛shiro教程-第十七章-OAuth2集成 1.OAuth2介 ...
- 2017.2.15 开涛shiro教程-第二十一章-授予身份与切换身份(二) controller
原博客地址:http://jinnianshilongnian.iteye.com/blog/2018398 根据下载的pdf学习. 开涛shiro教程-第二十一章-授予身份与切换身份(二) 1.回顾 ...
- 2017.2.15 开涛shiro教程-第二十一章-授予身份与切换身份(一) table、entity、service、dao
原博客地址:http://jinnianshilongnian.iteye.com/blog/2018398 根据下载的pdf学习. 第二十一章 授予身份与切换身份(一) 1.使用场景 某个领导因为某 ...
- 2017.2.12 开涛shiro教程-第七章-与Web集成
2017.2.9 开涛shiro教程-第七章-与Web集成(一) 原博客地址:http://jinnianshilongnian.iteye.com/blog/2018398 根据下载的pdf学习. ...
- 2017.4.12 开涛shiro教程-第十八章-并发登录人数控制
原博客地址:http://jinnianshilongnian.iteye.com/blog/2018398 根据下载的pdf学习. 开涛shiro教程-第十八章-并发登录人数控制 shiro中没有提 ...
- 2017.2.16 开涛shiro教程-第十七章-OAuth2集成(二)客户端
原博客地址:http://jinnianshilongnian.iteye.com/blog/2018398 根据下载的pdf学习. 开涛shiro教程-第十七章-OAuth2集成 3.客户端 客户端 ...
- 2017.2.13 开涛shiro教程-第十二章-与Spring集成(一)配置文件详解
原博客地址:http://jinnianshilongnian.iteye.com/blog/2018398 根据下载的pdf学习. 第十二章-与Spring集成(一)配置文件详解 1.pom.xml ...
- 2017.2.13 开涛shiro教程-第十二章-与Spring集成(二)shiro权限注解
原博客地址:http://jinnianshilongnian.iteye.com/blog/2018398 根据下载的pdf学习. 第十二章-与Spring集成(二)shiro权限注解 shiro注 ...
- 2017.2.12 开涛shiro教程-第八章-拦截器机制
原博客地址:http://jinnianshilongnian.iteye.com/blog/2018398 根据下载的pdf学习. 1.拦截器介绍 下图是shiro拦截器的基础类图: 1.Namea ...
随机推荐
- python re模块详解
re模块 re模块使用python拥有全部的正则表达式功能 1 2 3 4 re.I(re.IGNORECASE): 忽略大小写(括号内是完整写法) re.M(MULTILINE):(多行模式,改变 ...
- python re 模块小结
前言: 本人环境windows 7 64位,python2.7 re是什么: regular expression缩写,意为正则表达式,是python的众多模块之一 re用途: 从文本中有选择的批量抽 ...
- webRTC前世今生
WebRTC 的前世今生 本文由 rwebrtc 翻译 WebRTC 技术是激烈的开放的 Web 战争中一大突破.-Brendan Eich, inventor of JavaScript 无插件实时 ...
- 面试:ios 批量上传图片
这几天面试,被问到关于GCD的用法,想了想,之前项目好像确实用的比较少,只是知道怎么用,有思路,但是却从来没有试过,回来之后,就尝试写了下: 封装图片上传的方法 /**批量上传图片*/ + (NSUR ...
- pat 甲级 1053. Path of Equal Weight (30)
1053. Path of Equal Weight (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...
- 使用非对称算法RSA实现加解密和使用签名算法SHA1WithRSA、MD5withRSA生成签名以及验签
不啰嗦,直接上源码 package com.hudai.platform.manager.util; import java.io.ByteArrayOutputStream; import java ...
- js,add script async? loaded ok.
function loadScript(url, callback){ var script = document.createElement_x("script") script ...
- css3动画、边框、投影知识
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- linux的进程管理
进程就是正在运行的程序. 一个程序可以对应多个进程,但是一个进程只对应一个程序. 1.进程和进程控制的概念 查看进程状态: w+用户名 查看个别用户的进程 ps -aux 显示系统进程 a:代表 ...
- Swift Perfect 基础项目
brew install mysql@5.7 && brew link mysql@5.7 --force Package.swift import PackageDescriptio ...