RuoYi 5 роки тому
батько
коміт
f2b69c99e6

+ 10 - 0
ruoyi-common/src/main/java/com/ruoyi/common/constant/ShiroConstants.java

@@ -61,4 +61,14 @@ public interface ShiroConstants
      * 验证码错误
      */
     public static final String CAPTCHA_ERROR = "captchaError";
+
+    /**
+     * 登录记录缓存
+     */
+    public static final String LOGINRECORDCACHE = "loginRecordCache";
+
+    /**
+     * 系统活跃用户缓存
+     */
+    public static final String SYS_USERCACHE = "sys-userCache";
 }

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

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

+ 2 - 1
ruoyi-framework/src/main/java/com/ruoyi/framework/shiro/service/SysPasswordService.java

@@ -9,6 +9,7 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Component;
 import com.ruoyi.common.constant.Constants;
+import com.ruoyi.common.constant.ShiroConstants;
 import com.ruoyi.common.exception.user.UserPasswordNotMatchException;
 import com.ruoyi.common.exception.user.UserPasswordRetryLimitExceedException;
 import com.ruoyi.common.utils.MessageUtils;
@@ -35,7 +36,7 @@ public class SysPasswordService
     @PostConstruct
     public void init()
     {
-        loginRecordCache = cacheManager.getCache("loginRecordCache");
+        loginRecordCache = cacheManager.getCache(ShiroConstants.LOGINRECORDCACHE);
     }
 
     public void validate(SysUser user, String password)

+ 16 - 0
ruoyi-framework/src/main/java/com/ruoyi/framework/shiro/web/filter/LogoutFilter.java

@@ -1,12 +1,17 @@
 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.framework.manager.AsyncManager;
@@ -28,6 +33,8 @@ 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;
@@ -53,6 +60,8 @@ public class LogoutFilter extends org.apache.shiro.web.filter.authc.LogoutFilter
                     String loginName = user.getLoginName();
                     // 记录用户退出日志
                     AsyncManager.me().execute(AsyncFactory.recordLogininfor(loginName, Constants.LOGOUT, MessageUtils.message("user.logout.success")));
+                    // 清理缓存
+                    cache.remove(loginName);
                 }
                 // 退出登录
                 subject.logout();
@@ -83,4 +92,11 @@ 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);
+    }
 }

+ 14 - 6
ruoyi-framework/src/main/java/com/ruoyi/framework/shiro/web/filter/kickout/KickoutSessionFilter.java

@@ -17,6 +17,7 @@ import org.apache.shiro.subject.Subject;
 import org.apache.shiro.web.filter.AccessControlFilter;
 import org.apache.shiro.web.util.WebUtils;
 import com.fasterxml.jackson.databind.ObjectMapper;
+import com.ruoyi.common.constant.ShiroConstants;
 import com.ruoyi.common.core.domain.AjaxResult;
 import com.ruoyi.common.utils.ServletUtils;
 import com.ruoyi.framework.util.ShiroUtils;
@@ -108,12 +109,19 @@ public class KickoutSessionFilter extends AccessControlFilter
                 // 踢出后再更新下缓存队列
                 cache.put(loginName, deque);
 
-                // 获取被踢出的sessionId的session对象
-                Session kickoutSession = sessionManager.getSession(new DefaultSessionKey(kickoutSessionId));
-                if (kickoutSession != null)
+                try
                 {
-                    // 设置会话的kickout属性表示踢出了
-                    kickoutSession.setAttribute("kickout", true);
+                    // 获取被踢出的sessionId的session对象
+                    Session kickoutSession = sessionManager.getSession(new DefaultSessionKey(kickoutSessionId));
+                    if (null != kickoutSession)
+                    {
+                        // 设置会话的kickout属性表示踢出了
+                        kickoutSession.setAttribute("kickout", true);
+                    }
+                }
+                catch (Exception e)
+                {
+                    // 面对异常,我们选择忽略
                 }
             }
 
@@ -173,6 +181,6 @@ public class KickoutSessionFilter extends AccessControlFilter
     public void setCacheManager(CacheManager cacheManager)
     {
         // 必须和ehcache缓存配置中的缓存name一致
-        this.cache = cacheManager.getCache("sys-userCache");
+        this.cache = cacheManager.getCache(ShiroConstants.SYS_USERCACHE);
     }
 }