shiro安全框架扩展教程--异常退出没有清除缓存信息处理方案

来源:互联网 发布:优化方案系列丛书答案 编辑:程序博客网 时间:2024/05/24 04:15
自从之前研究了security3一段时间,发现也不咋滴,后来转行去玩玩shiro,感觉还是挺不错的,小巧灵活;然后遇到个大家都应该遇到过的问题就是当用户退出或者异常关闭浏览器的时候不会自动清除缓存授权信息,当然shiro是有个玩意会自动扫描过期的会话,但是它只会清除会话信息不会清除cache里面的信息,看了网上的答案都是不靠谱的,最好还是自己看源码吧,下面看我的解决方案

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <!-- 默认会话管理器 -->  
  2.     <bean id="sessionManager"  
  3.         class="com.shadow.shiro.extend.session.impl.SimpleWebSessionManager">  
  4.         <property name="globalSessionTimeout" value="15000" />  
  5.         <property name="sessionValidationInterval" value="30000" />  
  6.         <property name="sessionValidationSchedulerEnabled" value="true" />  
  7.     </bean>  

全局的会话信息设置成15秒,检测扫描信息间隔30秒,第三个参数就是是否开启扫描

至于我的sessionManager实现类是自己继承,然后重写了其中一个方法

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. package com.shadow.shiro.extend.session.impl;  
  2.   
  3. import java.util.Collection;  
  4. import java.util.Iterator;  
  5.   
  6. import org.apache.log4j.Logger;  
  7. import org.apache.shiro.cache.CacheManager;  
  8. import org.apache.shiro.session.ExpiredSessionException;  
  9. import org.apache.shiro.session.InvalidSessionException;  
  10. import org.apache.shiro.session.Session;  
  11. import org.apache.shiro.session.mgt.DefaultSessionKey;  
  12. import org.apache.shiro.session.mgt.SessionKey;  
  13. import org.apache.shiro.session.mgt.SimpleSession;  
  14. import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;  
  15.   
  16. import com.shadow.shiro.extend.session.WebSessionManager;  
  17.   
  18. /** 
  19.  * 会话管理器 
  20.  *  
  21.  * @author shadow 
  22.  *  
  23.  */  
  24. public class SimpleWebSessionManager extends DefaultWebSessionManager implements  
  25.         WebSessionManager {  
  26.   
  27.     private CacheManager cacheManager;  
  28.   
  29.     private final static Logger logger = Logger  
  30.             .getLogger(SimpleWebSessionManager.class);  
  31.   
  32.     public SimpleWebSessionManager() {  
  33.         super();  
  34.     }  
  35.   
  36.     public void validateSessions() {  
  37.         if (logger.isInfoEnabled())  
  38.             logger.info("Validating all active sessions...");  
  39.         int invalidCount = 0;  
  40.         Collection<?> activeSessions = getActiveSessions();  
  41.         if (activeSessions != null && !activeSessions.isEmpty()) {  
  42.             for (Iterator<?> i$ = activeSessions.iterator(); i$.hasNext();) {  
  43.                 Session session = (Session) i$.next();  
  44.                 try {  
  45.                     SessionKey key = new DefaultSessionKey(session.getId());  
  46.                     validate(session, key);  
  47.                 } catch (InvalidSessionException e) {  
  48.                     if (cacheManager != null) {  
  49.                         SimpleSession s = (SimpleSession) session;  
  50.                         if (s.getAttribute(SESSION_USER_KEY) != null)  
  51.                             cacheManager.getCache(null).remove(  
  52.                                     s.getAttribute(SESSION_USER_KEY));  
  53.                     }  
  54.                     if (logger.isDebugEnabled()) {  
  55.                         boolean expired = e instanceof ExpiredSessionException;  
  56.                         String msg = (new StringBuilder()).append(  
  57.                                 "Invalidated session with id [").append(  
  58.                                 session.getId()).append("]").append(  
  59.                                 expired ? " (expired)" : " (stopped)")  
  60.                                 .toString();  
  61.                         logger.debug(msg);  
  62.                     }  
  63.                     invalidCount++;  
  64.                 }  
  65.             }  
  66.   
  67.         }  
  68.         if (logger.isInfoEnabled()) {  
  69.             String msg = "Finished session validation.";  
  70.             if (invalidCount > 0)  
  71.                 msg = (new StringBuilder()).append(msg).append("  [").append(  
  72.                         invalidCount).append("] sessions were stopped.")  
  73.                         .toString();  
  74.             else  
  75.                 msg = (new StringBuilder()).append(msg).append(  
  76.                         "  No sessions were stopped.").toString();  
  77.             logger.info(msg);  
  78.         }  
  79.     }  
  80.   
  81.     public void setCacheManager(CacheManager cacheManager) {  
  82.         this.cacheManager = cacheManager;  
  83.     }  
  84.   
  85. }  

其中这里的方法是校验会话的,我在方法上加入了cachemanager的接口,然后重写set方法,就能获得实例,然后在执行期间调用cache.remove()方法,就能清空缓存上的信息了;
0 0