From cdbf0e226453b6c213a6c560b70c7ff6774eddfc Mon Sep 17 00:00:00 2001 From: RuoYi Date: Mon, 10 Aug 2020 16:08:03 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BC=BA=E9=80=80&=E8=BF=87=E6=9C=9F=E6=B8=85?= =?UTF-8?q?=E7=90=86=E7=99=BB=E5=BD=95=E5=B8=90=E5=8F=B7=E7=BC=93=E5=AD=98?= =?UTF-8?q?=E4=BC=9A=E8=AF=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../monitor/SysUserOnlineController.java | 1 + ruoyi-common/pom.xml | 6 +++++ ruoyi-framework/pom.xml | 6 ----- .../ruoyi/framework/config/ShiroConfig.java | 1 - .../shiro/web/filter/LogoutFilter.java | 18 +++---------- .../web/session/OnlineWebSessionManager.java | 1 + .../system/service/ISysUserOnlineService.java | 8 ++++++ .../impl/SysUserOnlineServiceImpl.java | 25 +++++++++++++++++++ 8 files changed, 44 insertions(+), 22 deletions(-) diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/monitor/SysUserOnlineController.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/monitor/SysUserOnlineController.java index 1c36b111b..4d76ef4fb 100644 --- a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/monitor/SysUserOnlineController.java +++ b/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(); } diff --git a/ruoyi-common/pom.xml b/ruoyi-common/pom.xml index 77221b0d8..491865027 100644 --- a/ruoyi-common/pom.xml +++ b/ruoyi-common/pom.xml @@ -34,6 +34,12 @@ org.apache.shiro shiro-core + + + + org.apache.shiro + shiro-ehcache + diff --git a/ruoyi-framework/pom.xml b/ruoyi-framework/pom.xml index 85a7ceaed..e4af296bd 100644 --- a/ruoyi-framework/pom.xml +++ b/ruoyi-framework/pom.xml @@ -53,12 +53,6 @@ shiro-spring - - - org.apache.shiro - shiro-ehcache - - com.github.theborakompanioni diff --git a/ruoyi-framework/src/main/java/com/ruoyi/framework/config/ShiroConfig.java b/ruoyi-framework/src/main/java/com/ruoyi/framework/config/ShiroConfig.java index 06e75ec96..1173b589c 100644 --- a/ruoyi-framework/src/main/java/com/ruoyi/framework/config/ShiroConfig.java +++ b/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; } diff --git a/ruoyi-framework/src/main/java/com/ruoyi/framework/shiro/web/filter/LogoutFilter.java b/ruoyi-framework/src/main/java/com/ruoyi/framework/shiro/web/filter/LogoutFilter.java index f5aa96647..6afec8870 100644 --- a/ruoyi-framework/src/main/java/com/ruoyi/framework/shiro/web/filter/LogoutFilter.java +++ b/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> 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); - } } diff --git a/ruoyi-framework/src/main/java/com/ruoyi/framework/shiro/web/session/OnlineWebSessionManager.java b/ruoyi-framework/src/main/java/com/ruoyi/framework/shiro/web/session/OnlineWebSessionManager.java index 089806733..7556afa10 100644 --- a/ruoyi-framework/src/main/java/com/ruoyi/framework/shiro/web/session/OnlineWebSessionManager.java +++ b/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()); } } diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/ISysUserOnlineService.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/ISysUserOnlineService.java index 1ed908064..9c62537f2 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/service/ISysUserOnlineService.java +++ b/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); + /** * 查询会话集合 * diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysUserOnlineServiceImpl.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysUserOnlineServiceImpl.java index b9d6f1d24..1dde6c286 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysUserOnlineServiceImpl.java +++ b/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> cache = ehCacheManager.getCache(ShiroConstants.SYS_USERCACHE); + Deque deque = cache.get(loginName); + if (StringUtils.isNull(cache) || StringUtils.isEmpty(deque)) + { + return; + } + deque.remove(sessionId); + } + /** * 查询会话集合 *