Parcourir la source

强退&过期清理登录帐号缓存会话

RuoYi il y a 4 ans
Parent
commit
cdbf0e2264

+ 1 - 0
ruoyi-admin/src/main/java/com/ruoyi/web/controller/monitor/SysUserOnlineController.java

@@ -82,6 +82,7 @@ public class SysUserOnlineController extends BaseController
             onlineSessionDAO.update(onlineSession);
             online.setStatus(OnlineStatus.off_line);
             userOnlineService.saveOnline(online);
+            userOnlineService.removeUserCache(online.getLoginName(), sessionId);
         }
         return success();
     }

+ 6 - 0
ruoyi-common/pom.xml

@@ -34,6 +34,12 @@
             <groupId>org.apache.shiro</groupId>
             <artifactId>shiro-core</artifactId>
         </dependency>
+        
+        <!-- Shiro使用EhCache缓存框架 -->
+        <dependency>
+            <groupId>org.apache.shiro</groupId>
+            <artifactId>shiro-ehcache</artifactId>
+        </dependency>
 
         <!-- pagehelper 分页插件 -->
         <dependency>

+ 0 - 6
ruoyi-framework/pom.xml

@@ -53,12 +53,6 @@
             <artifactId>shiro-spring</artifactId>
         </dependency>
 
-        <!-- Shiro使用EhCache缓存框架 -->
-        <dependency>
-            <groupId>org.apache.shiro</groupId>
-            <artifactId>shiro-ehcache</artifactId>
-        </dependency>
-
         <!-- thymeleaf模板引擎和shiro框架的整合 -->
         <dependency>
             <groupId>com.github.theborakompanioni</groupId>

+ 0 - 1
ruoyi-framework/src/main/java/com/ruoyi/framework/config/ShiroConfig.java

@@ -249,7 +249,6 @@ public class ShiroConfig
     public LogoutFilter logoutFilter()
     {
         LogoutFilter logoutFilter = new LogoutFilter();
-        logoutFilter.setCacheManager(getEhCacheManager());
         logoutFilter.setLoginUrl(loginUrl);
         return logoutFilter;
     }

+ 3 - 15
ruoyi-framework/src/main/java/com/ruoyi/framework/shiro/web/filter/LogoutFilter.java

@@ -1,23 +1,20 @@
 package com.ruoyi.framework.shiro.web.filter;
 
-import java.io.Serializable;
-import java.util.Deque;
 import javax.servlet.ServletRequest;
 import javax.servlet.ServletResponse;
-import org.apache.shiro.cache.Cache;
-import org.apache.shiro.cache.CacheManager;
 import org.apache.shiro.session.SessionException;
 import org.apache.shiro.subject.Subject;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import com.ruoyi.common.constant.Constants;
-import com.ruoyi.common.constant.ShiroConstants;
 import com.ruoyi.common.utils.MessageUtils;
 import com.ruoyi.common.utils.StringUtils;
+import com.ruoyi.common.utils.spring.SpringUtils;
 import com.ruoyi.framework.manager.AsyncManager;
 import com.ruoyi.framework.manager.factory.AsyncFactory;
 import com.ruoyi.framework.util.ShiroUtils;
 import com.ruoyi.system.domain.SysUser;
+import com.ruoyi.system.service.ISysUserOnlineService;
 
 /**
  * 退出过滤器
@@ -33,8 +30,6 @@ public class LogoutFilter extends org.apache.shiro.web.filter.authc.LogoutFilter
      */
     private String loginUrl;
 
-    private Cache<String, Deque<Serializable>> cache;
-
     public String getLoginUrl()
     {
         return loginUrl;
@@ -61,7 +56,7 @@ public class LogoutFilter extends org.apache.shiro.web.filter.authc.LogoutFilter
                     // 记录用户退出日志
                     AsyncManager.me().execute(AsyncFactory.recordLogininfor(loginName, Constants.LOGOUT, MessageUtils.message("user.logout.success")));
                     // 清理缓存
-                    cache.remove(loginName);
+                    SpringUtils.getBean(ISysUserOnlineService.class).removeUserCache(loginName, ShiroUtils.getSessionId());
                 }
                 // 退出登录
                 subject.logout();
@@ -92,11 +87,4 @@ public class LogoutFilter extends org.apache.shiro.web.filter.authc.LogoutFilter
         }
         return super.getRedirectUrl(request, response, subject);
     }
-
-    // 设置Cache的key的前缀
-    public void setCacheManager(CacheManager cacheManager)
-    {
-        // 必须和ehcache缓存配置中的缓存name一致
-        this.cache = cacheManager.getCache(ShiroConstants.SYS_USERCACHE);
-    }
 }

+ 1 - 0
ruoyi-framework/src/main/java/com/ruoyi/framework/shiro/web/session/OnlineWebSessionManager.java

@@ -130,6 +130,7 @@ public class OnlineWebSessionManager extends DefaultWebSessionManager
                 }
                 invalidCount++;
                 needOfflineIdList.add(userOnline.getSessionId());
+                userOnlineService.removeUserCache(userOnline.getLoginName(), userOnline.getSessionId());
             }
 
         }

+ 8 - 0
ruoyi-system/src/main/java/com/ruoyi/system/service/ISysUserOnlineService.java

@@ -57,6 +57,14 @@ public interface ISysUserOnlineService
      */
     public void forceLogout(String sessionId);
 
+    /**
+     * 清理用户缓存
+     * 
+     * @param loginName 登录名称
+     * @param sessionId 会话ID
+     */
+    public void removeUserCache(String loginName, String sessionId);
+
     /**
      * 查询会话集合
      * 

+ 25 - 0
ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysUserOnlineServiceImpl.java

@@ -1,9 +1,14 @@
 package com.ruoyi.system.service.impl;
 
+import java.io.Serializable;
 import java.util.Date;
+import java.util.Deque;
 import java.util.List;
+import org.apache.shiro.cache.Cache;
+import org.apache.shiro.cache.ehcache.EhCacheManager;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
+import com.ruoyi.common.constant.ShiroConstants;
 import com.ruoyi.common.utils.DateUtils;
 import com.ruoyi.common.utils.StringUtils;
 import com.ruoyi.system.domain.SysUserOnline;
@@ -20,6 +25,9 @@ public class SysUserOnlineServiceImpl implements ISysUserOnlineService
 {
     @Autowired
     private SysUserOnlineMapper userOnlineDao;
+    
+    @Autowired
+    private EhCacheManager ehCacheManager;
 
     /**
      * 通过会话序号查询信息
@@ -101,6 +109,23 @@ public class SysUserOnlineServiceImpl implements ISysUserOnlineService
         userOnlineDao.deleteOnlineById(sessionId);
     }
 
+    /**
+     * 清理用户缓存
+     * 
+     * @param loginName 登录名称
+     * @param sessionId 会话ID
+     */
+    public void removeUserCache(String loginName, String sessionId)
+    {
+        Cache<String, Deque<Serializable>> cache = ehCacheManager.getCache(ShiroConstants.SYS_USERCACHE);
+        Deque<Serializable> deque = cache.get(loginName);
+        if (StringUtils.isNull(cache) || StringUtils.isEmpty(deque))
+        {
+            return;
+        }
+        deque.remove(sessionId);
+    }
+
     /**
      * 查询会话集合
      *