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

pull/175/MERGE
RuoYi 2020-08-10 16:08:03 +08:00
parent 134e559704
commit cdbf0e2264
8 changed files with 44 additions and 22 deletions

View File

@ -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();
}

View File

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

View File

@ -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>

View File

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

View File

@ -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);
}
}

View File

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

View File

@ -57,6 +57,14 @@ public interface ISysUserOnlineService
*/
public void forceLogout(String sessionId);
/**
*
*
* @param loginName
* @param sessionId ID
*/
public void removeUserCache(String loginName, String sessionId);
/**
*
*

View File

@ -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;
@ -21,6 +26,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);
}
/**
*
*