日期:2014-05-17  浏览次数:20771 次

安全认证框架-Apache Shiro研究心得(转)

最近因为项目需要,研究了一下Apache Shiro安全认证框架,把心得记录下来。

(原创by:西风吹雨)

???? Apache Shrio是一个安全认证框架,和Spring Security相比,在于他使用了和比较简洁易懂的认证和授权方式。其提供的native-session(即把用户认证后的授权信息保存在其自身提供Session中)机制,这样就可以和HttpSession、EJB Session Bean的基于容器的Session脱耦,到到和客户端应用、Flex应用、远程方法调用等都可以使用它来配置权限认证。?

?1、sessionMode

??? 在普通的WEB项目中,我们可以选择使用native session或者是HttpSession,通过设置securityManager的sessionMode参数为http或native即可。

?2、realm

??? 我们可以基于jdbc,ldap,text,activeDirectory,jndi等多种方式来获取用户基本信息,角色信息,权限信息等。只需要在 securityManager中指定使用相应的realm实现即可,其在这各方面都提供了对应的缺省实现,比如我们常用的基于数据库表的形式来配置用户 权限信息,就可以使用其缺省实现的jdbcRealm(org.apache.shiro.realm.jdbc.JdbcRealm)。当然,如果认证 信息来自于多方面,多个不同的来源(比如来自两个库中,或者一个数据库,一个是ldap,再配上一个缺省的基于文本的测试用等等),我们可以为 securityManager指定realms参数,即把这一组安全配置都配置上。各个具体的realm实现提供了方法来获取用户基本信息、角色、权限 等。

??? realm的授权信息可以存放在Cache中,Cache的名称可以通过设置其authorizationCacheName参数指定。

3、缓存 ???

?? 目前Shrio缺省提供了基于ehCache来缓存用户认证信息和授权信息的实现。只需要配置

org.apache.shiro.web.mgt.DefaultWebSecurityManager 这个 cacheManager并设置给SecurityManager即可。如果项目中已经存在使用的ehCacheManager配置(org.springframework.cache.ehcache.EhCacheManagerFactoryBean),DefaultWebSecurityManager则可以指定使用现有的ehCacheManager,如果不指定,它将自行使用缺省配置创建一个。同时,也可以设置cacheManagerConfigFile参数来指定ehCache的配置文件。

?

下例中的shiro.authorizationCache是用来存放授权信息的Cache,我们在配置realm(如myRealm或 jdbcReaml)时,把authorizationCacheName属性设置shiro.authorizationCache来对应。

?

ehcache.xml

<ehcache>

??? <diskStore path="java.io.tmpdir/tuan-oauth"/>

?


??? <defaultCache
??????????? maxElementsInMemory="10000"
??????????? eternal="false"
??????????? timeToIdleSeconds="120"
??????????? timeToLiveSeconds="120"
??????????? overflowToDisk="false"
??????????? diskPersistent="false"
??????????? diskExpiryThreadIntervalSeconds="120"
??????????? />

??? <!-- We want eternal="true" (with no timeToIdle or timeToLive settings) because Shiro manages session
expirations explicitly.? If we set it to false and then set corresponding timeToIdle and timeToLive properties,
ehcache would evict sessions without Shiro's knowledge, which would cause many problems
(e.g. "My Shiro session timeout is 30 minutes - why isn't a session available after 2 minutes?"
Answer - ehcache expired it due to the timeToIdle property set to 120 seconds.)

diskPersistent=true since we want an enterprise session management feature - ability to use sessions after
even after a JVM restart.? -->
??? <cache name="shiro-activeSessionCache"
?????????? maxElementsInMemory="10000"
?????????? eternal="true"
?????????? overflowToDisk="true"
?????????? diskPersistent="true"
?????????? diskExpiryThreadIntervalSeconds="600"/>

??? <cache name="shiro.authorizationCache"
?????????? maxElementsInMemory="100"
?????????? eternal="false"
?????????? timeToLiveSeconds="600"
?????????? overflowToDisk="false"/>

</ehcache>

?

当我们把securityManager的sessionMode参数设置为native时,那么shrio就将用户的基本认证信息保存到缺省名称为shiro-activeSessionCache 的Cache中

org.apache.shiro.web.mgt.DefaultWebSecurityManager 在sessionMode参数设置为native时,缺省使用的是DefaultWebSessionManager来管理Session,该 管理类缺省使用的是使用MemorySessionDAO基于内存来保存和操作用户基本认证信息。如果系统内的用户数特别多,我们需要使用 CacheSessionDao来基于Cache进行操作,因此,这里需要显示配置一个 sessionManager(org.apache.shiro.web.session.mgt.DefaultWebSessionManager), 并配置该sessionManager的sessionDao为 CacheSessionDao(org.apache.shiro.session.mgt.eis.CachingSessionDAO,需用其实现 类org.apache.shiro.session.mgt.eis.EnterpriseCacheSessionDAO)。配置CacheSessionDao时,我们可以指定属性activeSessionsCacheName的名称来替换掉缺省名 shiro-activeSessionCache。

我们再把该sessionManager配置给DefaultWebSecurityManager就可以了。

?

??? <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
???????