Apache Shiro Session Management学习笔记
来源:互联网 发布:软件人才外包 编辑:程序博客网 时间:2024/05/29 13:31
Shiro的session并不依赖于web或者EJB的服务架构。
特点:
1.良好的兼容性,支持IOC,兼容json、yaml、spring xml配置。组件拥有良好的拓展性。
2.Shiro的session基于POJO,可以很方便的存储于文件、缓存、关系型数据库、配置文件。
3.session的取值过程完全独立,并不依赖于容器。
4.session的存在独立,不依赖于web或者EJB机构。
5.事件监听机制。
6.session会记录开始ip。
7.session回收机制,可以通过touch()方法来案场session的生命周期。
8.Shiro的session和http的session是兼容的。
9.支持SSO(单点登录)。
创建session
Subject currentUser = SecurityUtils.getSubject();Session session = currentUser.getSession();session.setAttribute( "someKey", someValue);
Shiro的getSession方法和HttpServletRequest的getSession方法相似:
1.如果存在相应session,Boolean型标记忽略,立即返回;
2.如果不存在相应session,Boolean型标记为true,则创建新的session并返回;
3.如果不存在相应session,Boolean型标记为false,则不创建session,返回null。
sessionManagement同样可以在shiro.ini中配置
[main]...sessionManager = com.foo.my.SessionManagerImplementationsecurityManager.sessionManager = $sessionManager
session设置超时
[main]...# 3,600,000 milliseconds = 1 hoursecurityManager.sessionManager.globalSessionTimeout = 3600000
设置session事件监听两种方式(shiro.ini):
1.实现SessionListener接口。
2.继承SessionListenerAdapter类。
[main]...aSessionListener = com.foo.my.SessionListeneranotherSessionListener = com.foo.my.OtherSessionListenersecurityManager.sessionManager.sessionListeners = $aSessionListener, $anotherSessionListener, etc.
关于session的持久化的操作(shiro.ini):
[main]...sessionDAO = com.foo.my.SessionDAOsecurityManager.sessionManager.sessionDAO = $sessionDAO
上述方法只限定于本地session管理,如果是基于web的应用,则需要首先配置本地sessionManager
[main]...sessionManager = org.apache.shiro.web.session.mgt.DefaultWebSessionManagersecurityManager.sessionManager = $sessionManager# Configure a SessionDAO and then set it:securityManager.sessionManager.sessionDAO = $sessionDAO
session的缓存处理机制(需要导入shiro-ehcache-<version>.jar文件):
配置方法一:
#This implementation would use your preferred distributed caching product's APIs:activeSessionsCache = my.org.apache.shiro.cache.CacheImplementationsessionDAO = org.apache.shiro.session.mgt.eis.EnterpriseCacheSessionDAOsessionDAO.activeSessionsCache = $activeSessionsCachesecurityManager.sessionManager.sessionDAO = $sessionDAO
配置方法二(一般使用):
[main]sessionDAO = org.apache.shiro.session.mgt.eis.EnterpriseCacheSessionDAOsecurityManager.sessionManager.sessionDAO = $sessionDAOcacheManager = org.apache.shiro.cache.ehcache.EhCacheManagersecurityManager.cacheManager = $cacheManager
ehcache.xml中的配置
<cache name="shiro-activeSessionCache" maxElementsInMemory="10000" overflowToDisk="true" //内存耗尽时,将session序列化到磁盘,必须属性 eternal="true" //缓存永不过期,而且不会自动删除,必须属性 timeToLiveSeconds="0" timeToIdleSeconds="0" diskPersistent="true" diskExpiryThreadIntervalSeconds="600"/>
关于配置中的name,shiro-activeSessionCache的更改,需要在EnterpriseCacheSessionDAO中进行配置(shiro.ini)
[main]...sessionDAO = org.apache.shiro.session.mgt.eis.EnterpriseCacheSessionDAOsessionDAO.activeSessionsCacheName = myname...
Shiro的默认session的id是由JavaUuidSessionIdGenerator所产生,可以根据需要在配置中改变(shiro.ini):
[main]...sessionIdGenerator = com.my.session.SessionIdGeneratorsecurityManager.sessionManager.sessionDAO.sessionIdGenerator = $sessionIdGenerator
session的回收机制,默认的回收机制不符合需求的话,可以自行设置(shiro.ini):
[main]...sessionValidationScheduler = org.apache.shiro.session.mgt.ExecutorServiceSessionValidationScheduler# Default is 3,600,000 millis = 1 hour:sessionValidationScheduler.interval = 3600000securityManager.sessionManager.sessionValidationScheduler = $sessionValidationScheduler
如果想要自己实现回收,也可以设置为:
[main]...sessionValidationScheduler = com.foo.my.SessionValidationSchedulersecurityManager.sessionManager.sessionValidationScheduler = $sessionValidationScheduler
如果不需要回收机制的情况下,可以设置(关闭回收机制,需要新的周期性处理无效session的机制,否则会内存溢出。shiro.ini):
[main]...securityManager.sessionManager.sessionValidationSchedulerEnabled = false
如果不需要session的删除机制,也可以设置关闭(需自行删除无效session。shiro.ini):
[main]...securityManager.sessionManager.deleteInvalidSessions = false
关闭回收机制和关闭删除机制的不同之处在于,回收机制关闭必须有其他形式的周期性会话验证机制来代替。
Ehcache + Terracotta集成案例(ehcache.xml):
<ehcache> <terracottaConfig url="localhost:9510"/> <diskStore path="java.io.tmpdir/shiro-ehcache"/> <defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="false" //必须为false,此设置不支持集群配置 diskPersistent="false" //必须为false,此设置不支持集群配置 diskExpiryThreadIntervalSeconds="120"> <terracotta/> </defaultCache> <cache name="shiro-activeSessionCache" maxElementsInMemory="10000" eternal="true" timeToLiveSeconds="0" timeToIdleSeconds="0" diskPersistent="false" overflowToDisk="false" diskExpiryThreadIntervalSeconds="600"> <terracotta/> </cache> <!-- Add more cache entries as desired, for example, Realm authc/authz caching: --></ehcache>
此处shiro.ini配置
sessionDAO = org.apache.shiro.session.mgt.eis.EnterpriseCacheSessionDAO# This name matches a cache name in ehcache.xml:sessionDAO.activeSessionsCacheName = shiro-activeSessionsCachesecurityManager.sessionManager.sessionDAO = $sessionDAO# Configure The EhCacheManager:cacheManager = org.apache.shiro.cache.ehcache.EhCacheManagercacheManager.cacheManagerConfigFile = classpath:ehcache.xml# Configure the above CacheManager on Shiro's SecurityManager# to use it for all of Shiro's caching needs:securityManager.cacheManager = $cacheManager //配置在最后,使cacheManager可以传播至前面的配置
禁用Shiro内部的session存储机制,但是并不影响session的产生(shiro.ini):
[main]...securityManager.subjectDAO.sessionStorageEvaluator.sessionStorageEnabled = false...
对于部分需要产生session,部分不需要的情况,可以通过实现org.apache.shiro.mgt.SessionStorageEvaluator接口来对subject进行定制:
... public boolean isSessionStorageEnabled(Subject subject) { boolean enabled = false; if (WebUtils.isWeb(Subject)) { HttpServletRequest request = WebUtils.getHttpRequest(subject); //set 'enabled' based on the current request. } else { //not a web request - maybe a RMI or daemon invocation? //set 'enabled' another way... } return enabled; }
shiro.ini中需要配置
[main]...sessionStorageEvaluator = com.mycompany.shiro.subject.mgt.MySessionStorageEvaluatorsecurityManager.subjectDAO.sessionStorageEvaluator = $sessionStorageEvaluator...
web项目中Shiro的noSession过滤器的配置:
[urls].../rest/** = noSessionCreation, authcBasic, ...
过滤器将阻止新的session的产生,但是对于已经存在的任然可以继续使用:
1.httpServletRequest.getSession();
2.httpServletRequest.getSession(true);
3.subject.getSession();
4.subject.getSession(true)。
以上四种方法在己经有相应session存在时,可以正常使用;否则会抛出DisabledSessionException异常。
1. httpServletRequest.getSession(false)
2.subject.getSession(false)
以上两种方法则可以在任意情况下使用。
- Apache Shiro Session Management学习笔记
- apache-shiro 学习笔记
- apache-shiro 学习笔记
- Apache Shiro学习笔记
- shiro 之 Session Management
- Apache Shiro Authentication学习笔记
- Apache Shiro Authorization学习笔记
- Apache Shiro Permission学习笔记
- Apache Shiro Relam学习笔记
- Apache Shiro Caching学习笔记
- Apache Shiro Subject学习笔记
- Apache Shiro 学习笔记1
- Apache Shiro学习笔记(1)
- Apache Shiro Web Support学习笔记
- Apache Shiro's Test学习笔记
- Apache Shiro with Spring学习笔记
- apache Shiro学习笔记(一)
- apache Shiro学习笔记(二) Authentication
- ExecutorService实现java多线程
- 郑州外贸公司网站建设做网站哪家好
- 《大话操作系统——做坚实的工程实践派》(1)
- AndroidStudio使用心得-引入第三方library
- Log4j.properties配置详解
- Apache Shiro Session Management学习笔记
- intellij idea14 注册码
- ActionBar 详解(上)
- C++测试程序执行时间代码
- 计算题(三)成本管理之挣值管理
- Scala 基础语法代码<转>
- 内存映射mmap函数
- MySQL启动报错“Bind on TCP/IP port: Address already in use”
- 【cocos2dx中Node类getParent和getChildByTag()】学习体会