退出登录清理缓存

dep_wangsr
RuoYi 6 years ago committed by Limy
parent f63d9e6a92
commit 391ffd7e21

@ -61,4 +61,14 @@ public interface ShiroConstants
* *
*/ */
public static final String CAPTCHA_ERROR = "captchaError"; public static final String CAPTCHA_ERROR = "captchaError";
/**
*
*/
public static final String LOGINRECORDCACHE = "loginRecordCache";
/**
*
*/
public static final String SYS_USERCACHE = "sys-userCache";
} }

@ -219,6 +219,7 @@ 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;
} }

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

@ -1,12 +1,17 @@
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.framework.manager.AsyncManager; 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 String loginUrl;
private Cache<String, Deque<Serializable>> cache;
public String getLoginUrl() public String getLoginUrl()
{ {
return loginUrl; return loginUrl;
@ -53,6 +60,8 @@ public class LogoutFilter extends org.apache.shiro.web.filter.authc.LogoutFilter
String loginName = user.getLoginName(); String loginName = user.getLoginName();
// 记录用户退出日志 // 记录用户退出日志
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);
} }
// 退出登录 // 退出登录
subject.logout(); subject.logout();
@ -83,4 +92,11 @@ 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);
}
} }

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

Loading…
Cancel
Save