ApacheShiro的会话管理提供了一个非常令人兴奋的功能,你可通过Shiro本身实现会话集群而不需要担心容器的环境。你使用Shiro本身的会话和会话集群可以部署到Tomcat,Jetty,JBOSS,Geronimo等任何的环境下,而不用担心容器或环境对集群设置所需要的特定配置。Shiro的会话集群只需配置一次就可运行在任何环境下。
这是怎么做到的呢?
一切是基于Shiro的POJO体系结构,使得创建会话集群非常简单,只要在会话持久层实现集群机制。也就是说,如果你配置了一个具有集群功能的SessionDAO,这个DAO就可以与集群机制交互而SessionManager并不需要知道集群的细节。
分布式缓存
现在很多分布式缓存解决方案都已经解决了分布式数据保存在持久层的问题,像Ehcache+TerraCotta,GigaSpacesOracleCoherence和Memcached等等。因此在Shiro中创建会话缓存非常简单,只需配置好某个分布式缓存机制。
在实现一个分布式或企业级高速缓存时,对于集群会话存贮需要注意两点:
1、有足够的内存保留所有的当前会话
2、如果没有足够的内存来保留所有活动的会话,它必须支持磁盘溢出,确保会话不会丢失。
EnterpriseCacheSessionDAO
Shiro本身已经提供了一个SessionDAO的实现能保存企业级/分布式的缓存数据。这个实现就是EnterpriseCacheSessionDAO,可以在CacheManager中配置它来实现缓存机制。配置如下:
#Thisimplementationwoulduseyourpreferreddistributedcachingproduct'sAPIs:
activeSessionCache=my.org.apache.shiro.cache.CacheImplementation
sessionDAO=org.apache.shiro.session.mgt.eis.EnterpriseCacheSessionDAO
sessionDAO.activeSessionCache=$activeSessionCache
securityManager.sessionManager.sessionDAO=$sessionDAO
|
虽然象上面介绍的方法你可要直接往SessionDAO里注入一个Cache实例,但还可以用一种更常见的方式可以为Shiro中所有需要用到缓存的地方配置一个通用的CacheManager。在这种方式中你可以告诉EnterpriseCacheSessionDAO一个Cache的名字,而这个Cache是用来存储活动的会话。
例如:
#Thisimplementationwoulduseyourcachingproduct'sAPIs:
cacheManager=my.org.apache.shiro.cache.CacheManagerImplementation
#NowconfiguretheEnterpriseCacheSessionDAOandtellitwhat
#cacheintheCacheManagershouldbeusedtostoreactivesessions:
sessionDAO=org.apache.shiro.session.mgt.eis.EnterpriseCacheSessionDAO
#Thisisthedefaultvalue.ChangeitifyourCacheManagerconfiguredadifferentname:
sessionDAO.activeSessionsCacheName=shiro-activeSessionsCache
#NowhavethenativeSessionManagerusethatDAO:
securityManager.sessionManager.sessionDAO=$sessionDAO
#ConfiguretheaboveCacheManageronShiro'sSecurityManager
#touseitforallofShiro'scachingneeds:
securityManager.cacheManager=$cacheManager
|
这种配置方法的特点是不论配置在什么地方,实际上是告诉SessionDAO实例都要使用Cache或CacheManager。那么SessionDAO是怎么使用分布式缓存呢?
当Shiro初始化SecurityManager时,它会检查SessionDAO是否实现了CacheManagerAware的接口。如果是的话,它会自动支持任何可用的全局配置的CacheManager。
当Shiro读到securityManager.cacheManager=$cacheManager这一行时,它会发现EnterpriseCacheSessionDAO实现了CacheManagerAware的接口并且调用setCacheManager方法,将你配置的CacheManager作为方法参数。
然后在运行时,当EnterpriseCacheSessionDAO需要activeSessionsCache的时候将要求CacheManager实例化并返回它,使用activeSessionsCacheName做为查询的关键字来得到缓存实例。该高速缓存实例将被用于存储和检索会话所有的SessionDAOCRUD操作。
使用Ehcache+Terracotta
在Shiro中使用Enchache+Terracotta提供分布式缓存解决方案已相当成熟。在这个解决方案中象上面描述的Enchache会话缓存配置将不能正常工作,需要做一些Terracotta配置。配置信息要保存到一个enchance.xml文件,例如:
<ehcache>
<terracottaConfigurl="localhost:9510"/>
<diskStorepath="java.io.tmpdir/shiro-ehcache"/>
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="false"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120">
<terracotta/>
</defaultCache>
<cachename="shiro-activeSessionCache"
maxElementsInMemory="10000"
eternal="true"
timeToLiveSeconds="0"
timeToIdleSeconds="0"
diskPersistent="false"
overflowToDisk="false"
diskExpiryThreadIntervalSeconds="600">
<terracotta/>
</cache>
<!--Addmorecacheentriesasdesired,forexample,
Realmauthc/authzcaching:-->
</ehcache>
|
你需要修改<terracottaConfigurl="localhost:9510"/>的内容,写入相应的Terracotta服务器阵列的主机/端口。另请注意,activeSessionCache元素的diskPersistent或overflowToDisk属性都应该是false的,在群集配置中不支持true。
除此之外还需要在Shiro中进行配置:
sessionDAO=org.apache.shiro.session.mgt.eis.EnterpriseCacheSessionDAO
#Thisnamematchesacachenameinehcache.xml:
sessionDAO.activeSessionsCacheName=shiro-activeSessionsCache
securityManager.sessionManager.sessionDAO=$sessionDAO
#ConfigureTheEhCacheManager:
cacheManager=org.apache.shiro.cache.ehcache.EhCacheManager
cacheManager.cacheManagerConfigFile=classpath:ehcache.xml
#ConfiguretheaboveCacheManageronShiro'sSecurityManager
#touseitforallofShiro'scachingneeds:
securityManager.cacheManager=$cacheManager
|
请记住,顺序很重要。通过在SecurityManager最后配置CacheManager,我们能确保CacheManager可以传播到所有先前配置的CacheManagerAware组件(如EnterpriseCachingSessionDAO)中。
分享到:
相关推荐
Apache Shiro 是一个框架,可用于身份验证和授权。Apache Shiro的教程(PDF),相关jar包,源码等。
Apache Shiro。 官网 Apache Shiro API。 Apache Shiro开发文档。
Apache Shiro 是一个强大而灵活的开源安全框架,它干净利落地处理身份认证,授权,企业会话管理和加密。 Apache Shiro 的首要目标是易于使用和理解。安全有时候是很复杂的,甚至是痛苦的,但它没有必要这样。框架...
Apache Shiro中文版使用手册!
apache shiro 实例 apache shiro 实例
采用SpringBoot + Apache Shiro + Mybatis Plus + Thymeleaf 实现的内容管理系统(附带权限管理),是搭建博客、网站的不二之选。 技术栈:Spring Boot、Apache Shiro、MyBatis-Plus、Alibaba Druid、Redis、MySQL、...
apache shiro 管理用户权限与数据库交互
Apache_Shiro_使用手册(一)Shiro架构介绍
Apache shiro 1.13.0源码 https://shiro.apache.org/
Shiro 是一个 Apache Incubator 项目,旨在简化身份验证和授权。在本文中,了解 Apache Shiro 并通过示例来在一个 Groovy web 应用程序中尝试使用 Shiro 进行身份验证和授权
Apache shiro1.2.4反序列化漏洞介绍 Apache Shiro是一个Java安全框架,执行身份验证、授权、密码和会话管理。Apache Shiro框架提供了记住我(RememberMe)的功能,关闭了浏览器下次再打开时还是能记住你是谁,下次...
本系统(基于SpringBoot+MyBatis+Apache Shiro+Bootstrap+Thymeleaf) 可用于开发所有企业级WEB应用系统(如:各种后台管理系统、CRM、ERP、CMS、OA、博客、论坛等...)。响应式布局,支持大部分浏览器(如:IE9+...
Apache Shiro是一个强大易用的Java安全框架,提供了认证、授权、加密和会话管理等功能。 使用Shiro的易于理解的API,您可以快速、轻松地获得任何应用程序,从最小的移动应用程序到最大的网络和企业应用程序。
Apache Shiro(发音为“shee-roh”,日语“堡垒(Castle)”的意思)是一个强大易用的Java安全框架,提供了认证、授权、加密和会话管理功能,可为任何应用提供安全保障 - 从命令行应用、移动应用到大型网络及企业...
4、优点:快速上手、全面支持验证、授权、加密和会话、灵活自定义设计、支持web环境、可以无缝集成spring等优点。可以用来用户验证、用户授权、用户session管理、安全加密等 5、基于RBAC五张表:用户表 tb_user、...
Apache Shiro是一个强大且易用的Java安全框架,执行身份验证、授权、密码和会话管理。使用Shiro的易于理解的API,您可以快速、轻松地获得任何应用程序,从最小的移动应用程序到最大的网络和企业应用程序。
spring mvc、apache shiro、mysql 框架搭建,基于maven构建
积分最低,Spring MVC+Mybatis+Ehcache+Apache Shiro+Bootstrap整合开发java仓库管理系统源码 开发环境:Eclipse ,JDK 1.8 ,Tomcat7 技术选型 后端技术 SpringMVC MVC框架 Spring Framework 容器 Apache ...
Apache shiro 标签授权,介绍Apache shiro 标签授权的多种形式。
Apache Shiro 是一个强大而灵活的开源安全框架,它干净利落地处理身份认证,授权,企业会话管理和加密。 Apache Shiro 的首要目标是易于使用和理解。安全有时候是很复杂的,甚至是痛苦的,但它没有必要这样。框架应 ...