Skip to content

Commit

Permalink
feat: 退出登录清理缓存
Browse files Browse the repository at this point in the history
  • Loading branch information
lerry903 committed Jun 7, 2019
1 parent 64fdf3b commit ac5f846
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ public SecurityManager securityManager(UserRealm userRealm) {
*/
private LogoutFilter logoutFilter() {
LogoutFilter logoutFilter = new LogoutFilter();
logoutFilter.setCacheManager(getEhCacheManager());
logoutFilter.setLoginUrl(loginUrl);
return logoutFilter;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.concurrent.atomic.AtomicInteger;
import javax.annotation.PostConstruct;

import com.ruoyi.common.constant.ShiroConstants;
import org.apache.shiro.cache.Cache;
import org.apache.shiro.cache.CacheManager;
import org.apache.shiro.crypto.hash.Md5Hash;
Expand Down Expand Up @@ -39,7 +40,7 @@ public SysPasswordService(CacheManager cacheManager) {

@PostConstruct
public void init() {
loginRecordCache = cacheManager.getCache("loginRecordCache");
loginRecordCache = cacheManager.getCache(ShiroConstants.LOGINRECORDCACHE);
}

public void validate(SysUser user, String password) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,22 @@

import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.constant.ShiroConstants;
import com.ruoyi.common.utils.MessageUtils;
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 lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.shiro.cache.Cache;
import org.apache.shiro.cache.CacheManager;
import org.apache.shiro.subject.Subject;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.io.Serializable;
import java.util.Deque;

/**
* 退出过滤器
Expand All @@ -27,6 +32,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;
}
Expand All @@ -45,6 +52,8 @@ protected boolean preHandle(ServletRequest request, ServletResponse response){
String loginName = user.getLoginName();
// 记录用户退出日志
AsyncManager.me().execute(AsyncFactory.recordLogininfor(loginName, Constants.LOGOUT, MessageUtils.message("user.logout.success")));
// 清理缓存
cache.remove(loginName);
}
// 退出登录
subject.logout();
Expand All @@ -66,4 +75,13 @@ protected String getRedirectUrl(ServletRequest request, ServletResponse response
}
return super.getRedirectUrl(request, response, subject);
}

/**
* 设置Cache的key的前缀
* @param cacheManager 缓存管理器
*/
public void setCacheManager(CacheManager cacheManager) {
// 必须和ehcache缓存配置中的缓存name一致
this.cache = cacheManager.getCache(ShiroConstants.SYS_USERCACHE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ protected boolean onAccessDenied(ServletRequest request, ServletResponse respons
try {
// 获取被踢出的sessionId的session对象
Session kickoutSession = sessionManager.getSession(new DefaultSessionKey(kickoutSessionId));
if (null != kickoutSession) {
if (ObjectUtil.isNotNull(kickoutSession)) {
// 设置会话的kickout属性表示踢出了
kickoutSession.setAttribute("kickout", true);
}
Expand Down

0 comments on commit ac5f846

Please sign in to comment.