mirror of https://gitee.com/y_project/RuoYi.git
强退&过期清理登录帐号缓存会话
parent
134e559704
commit
cdbf0e2264
|
@ -82,6 +82,7 @@ public class SysUserOnlineController extends BaseController
|
||||||
onlineSessionDAO.update(onlineSession);
|
onlineSessionDAO.update(onlineSession);
|
||||||
online.setStatus(OnlineStatus.off_line);
|
online.setStatus(OnlineStatus.off_line);
|
||||||
userOnlineService.saveOnline(online);
|
userOnlineService.saveOnline(online);
|
||||||
|
userOnlineService.removeUserCache(online.getLoginName(), sessionId);
|
||||||
}
|
}
|
||||||
return success();
|
return success();
|
||||||
}
|
}
|
||||||
|
|
|
@ -34,6 +34,12 @@
|
||||||
<groupId>org.apache.shiro</groupId>
|
<groupId>org.apache.shiro</groupId>
|
||||||
<artifactId>shiro-core</artifactId>
|
<artifactId>shiro-core</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<!-- Shiro使用EhCache缓存框架 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.shiro</groupId>
|
||||||
|
<artifactId>shiro-ehcache</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<!-- pagehelper 分页插件 -->
|
<!-- pagehelper 分页插件 -->
|
||||||
<dependency>
|
<dependency>
|
||||||
|
|
|
@ -53,12 +53,6 @@
|
||||||
<artifactId>shiro-spring</artifactId>
|
<artifactId>shiro-spring</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<!-- Shiro使用EhCache缓存框架 -->
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.apache.shiro</groupId>
|
|
||||||
<artifactId>shiro-ehcache</artifactId>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<!-- thymeleaf模板引擎和shiro框架的整合 -->
|
<!-- thymeleaf模板引擎和shiro框架的整合 -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.github.theborakompanioni</groupId>
|
<groupId>com.github.theborakompanioni</groupId>
|
||||||
|
|
|
@ -249,7 +249,6 @@ public class ShiroConfig
|
||||||
public LogoutFilter logoutFilter()
|
public LogoutFilter logoutFilter()
|
||||||
{
|
{
|
||||||
LogoutFilter logoutFilter = new LogoutFilter();
|
LogoutFilter logoutFilter = new LogoutFilter();
|
||||||
logoutFilter.setCacheManager(getEhCacheManager());
|
|
||||||
logoutFilter.setLoginUrl(loginUrl);
|
logoutFilter.setLoginUrl(loginUrl);
|
||||||
return logoutFilter;
|
return logoutFilter;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,23 +1,20 @@
|
||||||
package com.ruoyi.framework.shiro.web.filter;
|
package com.ruoyi.framework.shiro.web.filter;
|
||||||
|
|
||||||
import java.io.Serializable;
|
|
||||||
import java.util.Deque;
|
|
||||||
import javax.servlet.ServletRequest;
|
import javax.servlet.ServletRequest;
|
||||||
import javax.servlet.ServletResponse;
|
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.session.SessionException;
|
||||||
import org.apache.shiro.subject.Subject;
|
import org.apache.shiro.subject.Subject;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import com.ruoyi.common.constant.Constants;
|
import com.ruoyi.common.constant.Constants;
|
||||||
import com.ruoyi.common.constant.ShiroConstants;
|
|
||||||
import com.ruoyi.common.utils.MessageUtils;
|
import com.ruoyi.common.utils.MessageUtils;
|
||||||
import com.ruoyi.common.utils.StringUtils;
|
import com.ruoyi.common.utils.StringUtils;
|
||||||
|
import com.ruoyi.common.utils.spring.SpringUtils;
|
||||||
import com.ruoyi.framework.manager.AsyncManager;
|
import com.ruoyi.framework.manager.AsyncManager;
|
||||||
import com.ruoyi.framework.manager.factory.AsyncFactory;
|
import com.ruoyi.framework.manager.factory.AsyncFactory;
|
||||||
import com.ruoyi.framework.util.ShiroUtils;
|
import com.ruoyi.framework.util.ShiroUtils;
|
||||||
import com.ruoyi.system.domain.SysUser;
|
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 String loginUrl;
|
||||||
|
|
||||||
private Cache<String, Deque<Serializable>> cache;
|
|
||||||
|
|
||||||
public String getLoginUrl()
|
public String getLoginUrl()
|
||||||
{
|
{
|
||||||
return loginUrl;
|
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")));
|
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();
|
subject.logout();
|
||||||
|
@ -92,11 +87,4 @@ public class LogoutFilter extends org.apache.shiro.web.filter.authc.LogoutFilter
|
||||||
}
|
}
|
||||||
return super.getRedirectUrl(request, response, subject);
|
return super.getRedirectUrl(request, response, subject);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 设置Cache的key的前缀
|
|
||||||
public void setCacheManager(CacheManager cacheManager)
|
|
||||||
{
|
|
||||||
// 必须和ehcache缓存配置中的缓存name一致
|
|
||||||
this.cache = cacheManager.getCache(ShiroConstants.SYS_USERCACHE);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -130,6 +130,7 @@ public class OnlineWebSessionManager extends DefaultWebSessionManager
|
||||||
}
|
}
|
||||||
invalidCount++;
|
invalidCount++;
|
||||||
needOfflineIdList.add(userOnline.getSessionId());
|
needOfflineIdList.add(userOnline.getSessionId());
|
||||||
|
userOnlineService.removeUserCache(userOnline.getLoginName(), userOnline.getSessionId());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -57,6 +57,14 @@ public interface ISysUserOnlineService
|
||||||
*/
|
*/
|
||||||
public void forceLogout(String sessionId);
|
public void forceLogout(String sessionId);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 清理用户缓存
|
||||||
|
*
|
||||||
|
* @param loginName 登录名称
|
||||||
|
* @param sessionId 会话ID
|
||||||
|
*/
|
||||||
|
public void removeUserCache(String loginName, String sessionId);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 查询会话集合
|
* 查询会话集合
|
||||||
*
|
*
|
||||||
|
|
|
@ -1,9 +1,14 @@
|
||||||
package com.ruoyi.system.service.impl;
|
package com.ruoyi.system.service.impl;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
import java.util.Deque;
|
||||||
import java.util.List;
|
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.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
import com.ruoyi.common.constant.ShiroConstants;
|
||||||
import com.ruoyi.common.utils.DateUtils;
|
import com.ruoyi.common.utils.DateUtils;
|
||||||
import com.ruoyi.common.utils.StringUtils;
|
import com.ruoyi.common.utils.StringUtils;
|
||||||
import com.ruoyi.system.domain.SysUserOnline;
|
import com.ruoyi.system.domain.SysUserOnline;
|
||||||
|
@ -20,6 +25,9 @@ public class SysUserOnlineServiceImpl implements ISysUserOnlineService
|
||||||
{
|
{
|
||||||
@Autowired
|
@Autowired
|
||||||
private SysUserOnlineMapper userOnlineDao;
|
private SysUserOnlineMapper userOnlineDao;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private EhCacheManager ehCacheManager;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 通过会话序号查询信息
|
* 通过会话序号查询信息
|
||||||
|
@ -101,6 +109,23 @@ public class SysUserOnlineServiceImpl implements ISysUserOnlineService
|
||||||
userOnlineDao.deleteOnlineById(sessionId);
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 查询会话集合
|
* 查询会话集合
|
||||||
*
|
*
|
||||||
|
|
Loading…
Reference in New Issue