第十章 会话管理(五) 会话验证

Shiro 提供了会话验证调度器,用于定期的验证会话是否已过期,如果过期将停止会话;出于性能考虑,一般情况下都是获取会话时来验证会话是否过期并停止会话的;但是如在web环境中,如果用户不主动退出是不知道会话是否过期的,因此需要定期的检测会话是否过期,Shiro 提供了会话验证调度器SessionValidationScheduler来做这件事情。


可以通过如下ini配置开启会话验证:

sessionValidationScheduler=org.apache.shiro.session.mgt.ExecutorServiceSessionValidationScheduler
sessionValidationScheduler.interval = 3600000
sessionValidationScheduler.sessionManager=$sessionManager
sessionManager.globalSessionTimeout=1800000
sessionManager.sessionValidationSchedulerEnabled=true
sessionManager.sessionValidationScheduler=$sessionValidationScheduler

sessionValidationScheduler : 会话验证调度器, sessionManager 默认就是使用ExecutorServiceSessionValidationScheduler,其使用JDK的ScheduledExecutorService进行定期调度并验证会话是否过期;

sessionValidationScheduler.interval:设置调度时间间隔,单位毫秒,默认就是1 小时;

sessionValidationScheduler.sessionManager:设置会话验证调度器进行会话验证时的会话管理器;

sessionManager.globalSessionTimeout:设置全局会话超时时间,默认30 分钟,即如果30分钟内没有访问会话将过期;

sessionManager.sessionValidationSchedulerEnabled:是否开启会话验证器,默认是开启的;

sessionManager.sessionValidationScheduler: 设置会话验证调度器,默认就是使用ExecutorServiceSessionValidationScheduler。


Shiro 也提供了使用Quartz会话验证调度器:

sessionValidationScheduler=org.apache.shiro.session.mgt.quartz.QuartzSessionValidationScheduler
sessionValidationScheduler.sessionValidationInterval = 3600000
sessionValidationScheduler.sessionManager=$sessionManager

使用时需要导入shiro-quartz依赖:

<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-quartz</artifactId>
<version>1.2.2</version>
</dependency>


如 上会话验证调度器实现都是直接调用AbstractValidatingSessionManager 的validateSessions方法进行验证,其直接调用SessionDAO 的getActiveSessions方法获取所有会话进行验证,如果会话比较多,会影响性能;可以考虑如分页获取会话并进行验证,如com.github.zhangkaitao.shiro.chapter10.session.scheduler.MySessionValidationScheduler:

//分页获取会话并验证
String sql = "select session from sessions limit ?,?";
int start = 0; //起始记录
int size = 20; //每页大小
List<String> sessionList = jdbcTemplate.queryForList(sql, String.class, start, size);
while(sessionList.size() > 0) {
for(String sessionStr : sessionList) {
try {
Session session = SerializableUtils.deserialize(sessionStr);
Method validateMethod =
ReflectionUtils.findMethod(AbstractValidatingSessionManager.class,
"validate", Session.class, SessionKey.class);
validateMethod.setAccessible(true);
ReflectionUtils.invokeMethod(validateMethod,
sessionManager, session, new DefaultSessionKey(session.getId()));
} catch (Exception e) {
//ignore
}
}
start = start + size;
sessionList = jdbcTemplate.queryForList(sql, String.class, start, size);
}

其直接改造自ExecutorServiceSessionValidationScheduler,如上代码是验证的核心代码,可以根据自己的需求改造此验证调度器器;ini的配置和之前的类似。


如果在会话过期时不想删除过期的会话,可以通过如下ini配置进行设置:

sessionManager.deleteInvalidSessions=false

默认是开启的,在会话过期后会调用SessionDAO的delete 方法删除会话:如会话时持久化存储的,可以调用此方法进行删除。


如果是在获取会话时验证了会话已过期,将抛出InvalidSessionException;因此需要捕获这个异常并跳转到相应的页面告诉用户会话已过期,让其重新登录,如可以在web.xml 配置相应的错误页面:

<error-page>
<exception-type>org.apache.shiro.session.InvalidSessionException</exception-type>
<location>/invalidSession.jsp</location>
</error-page>

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean"> 02 <property name="securityManager" ref="securityManager"/> 03 <!-- override these for application-specific URLs if you like:--> 04 <!-- <property name="loginUrl" value="/welcome" />--> 05 <!-- <property name="successUrl" value="/blog/drug/list"/>--> 06 <property name="unauthorizedUrl" value="/unauthorized.jsp"/> 07 <!-- The 'filters' property is not necessary since any declared javax.servlet.Filter bean --> 08 <!-- defined will be automatically acquired and available via its beanName in chain --> 09 <!-- definitions, but you can perform instance overrides or name aliases here if you like: --> 10 <!-- <property name="filters"> 11 <util:map> 12 <entry key="anAlias" value-ref="someFilter"/> 13 </util:map> 14 </property> --> 15 <property name="filterChainDefinitions"> 16 <value> 17 /login* = anon 18 /welcome = anon 19 /slave4j/** = anon 20 /list = anon 21 /** = authc 22 </value> 23 </property> 24 </bean> 25 26 <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager"> 27 28 <!-- 基于ehCache来缓存用户认证信息和授权信息的实现 --> 29 <property name="cacheManager" ref="shiroCacheManager"/> 30 31 <!-- Single realm app. If you have multiple realms, use the 'realms' property instead. --> 32 <property name="realm" ref="jdbcRealm"/> 33 34 <!-- sessionMode参数设置为native时,那么shrio就将用户的基本认证信息保存到缺省名称为shiro-activeSessionCache 的Cache中 --> 35 <property name="sessionMode" value="native" /> 36 37 <!-- 缺省使用的是DefaultWebSessionManager来管理Session,该管理类缺省使用的是使用MemorySessionDAO基于内存来保存和操作用户基本认证信息。如果系统内的用户数特别多,我们需要使用CacheSessionDao来基于Cache进行操作 --> 38 <property name="sessionManager" ref="shiroSessionManager"/> 39 </bean> 40 41 <!-- jdbcRealm --> 42 <bean id="jdbcRealm" class="org.slave4j.shiro.MyRealm"> 43 <!-- shiro.authorizationCache是用来存放授权信息的Cache --> 44 <property name="authorizationCacheName" value="shiro.authorizationCache"/> 45 </bean> 46 47 <!-- shiroSessionManager --> 48 <bean id="shiroSessionManager" class="org.apache.shiro.web.session.mgt.DefaultWebSessionManager"> 49 <property name="sessionDAO" ref="sessionDAO"/> 50 <property name="sessionListeners"> 51 <set><bean class="org.slave4j.shiro.SessionHandler"/> </set> 52 </property> 53 54 <property name="sessionValidationScheduler" ref="sessionValidationScheduler" /> 55 <property name="globalSessionTimeout" value="180000"/> 56 <property name="sessionValidationSchedulerEnabled" value="true"/> 57 <property name="deleteInvalidSessions" value="true"/> 58 </bean> 59 <bean id="sessionDAO" class="org.apache.shiro.session.mgt.eis.EnterpriseCacheSessionDAO"> 60 <property name="activeSessionsCacheName" value="shiro-activeSessionCache"/> 61 </bean> 62 <bean id="sessionValidationScheduler" class="org.apache.shiro.session.mgt.ExecutorServiceSessionValidationScheduler"> 63 <property name="interval" value="5000"/> 64 </bean> 65 66 <!-- shiroCacheManager --> 67 <!-- Let's use some enterprise caching support for better performance. You can replace this with any enterprise 68 caching framework implementation that you like (Terracotta+Ehcache, Coherence, GigaSpaces, etc --> 69 <bean id="shiroCacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager"> 70 <!-- Set a net.sf.ehcache.CacheManager instance here if you already have one. If not, a new one 71 will be created with a default config: --> 72 <property name="cacheManager" ref="ehCacheManager"/> 73 <!-- If you don't have a pre-built net.sf.ehcache.CacheManager instance to inject, but you want 74 a specific Ehcache configuration to be used, specify that here. If you don't, a default 75 will be used.:--> 76 <property name="cacheManagerConfigFile" value="classpath:shiro_ehcache.xml"/> 77 </bean> 78 <bean id="ehCacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"/> 79 80 <!-- Enable Shiro Annotations for Spring-configured beans. Only run after the lifecycleBeanProcessor has run: --> 81 <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/> 82 <bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor"> 83 <property name="securityManager" ref="securityManager"/> 84 </bean>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值