shiro session管理

SessionManager是在应用程序中为所有Subject提供Session的管理,包括创建,删除,失效及验证等。同其的核心组件一样,SessionManager 也是一个由SecurityManager 维护的顶级组件

 

Shiro中默认提供了一个SessionManager的实现DefaultSessionManagerDefaultSessionManager 提供一个应用程序所需的所有企业级会话管理。可以在任何应用程序中使用

 

org.apache.shiro.web.mgt.DefaultWebSecurityManager 的默认SessionManager为ServletContainerSessionManager,如果使用ServletContainerSessionManager进行会话管理,Session的超时依赖于底层Servlet容器的超时时间,可以在web.xml中配置其会话的超时时间(分钟为单位)。

 

<!-- 企业sessiondao-->
<bean id ="sessionDao" class="org.apache.shiro.session.mgt.eis.EnterpriseCacheSessionDAO"></bean>
   
    <bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
	   <property name="cacheManagerConfigFile" value="classpath:ehcache-shiro.xml" />
	</bean>
	 <bean id="shiroDbRealm" class="com.wanwei.irdm4k.common.shiro.ShiroDbRealm" >
     <property name="cacheManager" ref="cacheManager"/>
    </bean>

	<bean id="sessionManager" class="org.apache.shiro.web.session.mgt.DefaultWebSessionManager">
        <property name="sessionDAO" ref="sessionDao"/> 
         <property name="globalSessionTimeout" value="7200000"/> 
    </bean> 
   
    <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
		<property name="realm" ref="shiroDbRealm" />
		<property name="cacheManager" ref="cacheManager"/>
		
		<property name="sessionManager" ref="sessionManager" />
	</bean>

 

Session Listeners

Shiro 支持SessionListener 概念来允许你对发生的重要会话作出反应。你可以实现SessionListener 接口(或扩展易用的SessionListenerAdapter)来与相应的会话操作作出反应。

由于默认的SessionManager sessionListeners 属性是一个集合,你可以对SessionManager 配置一个或多个listener 实现

Shiro提供了三个默认实现:

DefaultSessionManager:DefaultSecurityManager使用的默认实现,用于JavaSE环境;

ServletContainerSessionManager:DefaultWebSecurityManager使用的默认实现,用于Web环境,其直接使用Servlet容器的会话;

 

DefaultWebSessionManager:用于Web环境的实现,可以替代ServletContainerSessionManager,自己维护着会话,直接废弃了Servlet容器的会话管理。

 

另外如果使用ServletContainerSessionManager进行会话管理,Session的超时依赖于底层Servlet容器的超时时间,可以在web.xml中配置其会话的超时时间(分钟为单位): 

 

Java代码 复制代码  收藏代码
  1. <session-config>   
  2.   <session-timeout>30</session-timeout>   
  3. </session-config>  

猜你喜欢

转载自feiteyizu.iteye.com/blog/2281990