update 使用 SaStorage 优化 LoginHelper 一级缓存 避免 ThreadLocal 清理不干净问题
This commit is contained in:
parent
da0c86dd54
commit
5479b4db2b
@ -1,5 +1,6 @@
|
|||||||
package com.ruoyi.common.helper;
|
package com.ruoyi.common.helper;
|
||||||
|
|
||||||
|
import cn.dev33.satoken.context.SaHolder;
|
||||||
import cn.dev33.satoken.stp.StpUtil;
|
import cn.dev33.satoken.stp.StpUtil;
|
||||||
import cn.hutool.core.util.ObjectUtil;
|
import cn.hutool.core.util.ObjectUtil;
|
||||||
import com.ruoyi.common.constant.UserConstants;
|
import com.ruoyi.common.constant.UserConstants;
|
||||||
@ -29,15 +30,13 @@ public class LoginHelper {
|
|||||||
public static final String JOIN_CODE = ":";
|
public static final String JOIN_CODE = ":";
|
||||||
public static final String LOGIN_USER_KEY = "loginUser";
|
public static final String LOGIN_USER_KEY = "loginUser";
|
||||||
|
|
||||||
private static final ThreadLocal<LoginUser> LOGIN_CACHE = new ThreadLocal<>();
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 登录系统
|
* 登录系统
|
||||||
*
|
*
|
||||||
* @param loginUser 登录用户信息
|
* @param loginUser 登录用户信息
|
||||||
*/
|
*/
|
||||||
public static void login(LoginUser loginUser) {
|
public static void login(LoginUser loginUser) {
|
||||||
LOGIN_CACHE.set(loginUser);
|
SaHolder.getStorage().set(LOGIN_USER_KEY, loginUser);
|
||||||
StpUtil.login(loginUser.getLoginId());
|
StpUtil.login(loginUser.getLoginId());
|
||||||
setLoginUser(loginUser);
|
setLoginUser(loginUser);
|
||||||
}
|
}
|
||||||
@ -49,7 +48,7 @@ public class LoginHelper {
|
|||||||
* @param loginUser 登录用户信息
|
* @param loginUser 登录用户信息
|
||||||
*/
|
*/
|
||||||
public static void loginByDevice(LoginUser loginUser, DeviceType deviceType) {
|
public static void loginByDevice(LoginUser loginUser, DeviceType deviceType) {
|
||||||
LOGIN_CACHE.set(loginUser);
|
SaHolder.getStorage().set(LOGIN_USER_KEY, loginUser);
|
||||||
StpUtil.login(loginUser.getLoginId(), deviceType.getDevice());
|
StpUtil.login(loginUser.getLoginId(), deviceType.getDevice());
|
||||||
setLoginUser(loginUser);
|
setLoginUser(loginUser);
|
||||||
}
|
}
|
||||||
@ -65,22 +64,15 @@ public class LoginHelper {
|
|||||||
* 获取用户(多级缓存)
|
* 获取用户(多级缓存)
|
||||||
*/
|
*/
|
||||||
public static LoginUser getLoginUser() {
|
public static LoginUser getLoginUser() {
|
||||||
LoginUser loginUser = LOGIN_CACHE.get();
|
LoginUser loginUser = (LoginUser) SaHolder.getStorage().get(LOGIN_USER_KEY);
|
||||||
if (loginUser != null) {
|
if (loginUser != null) {
|
||||||
return loginUser;
|
return loginUser;
|
||||||
}
|
}
|
||||||
loginUser = (LoginUser) StpUtil.getTokenSession().get(LOGIN_USER_KEY);
|
loginUser = (LoginUser) StpUtil.getTokenSession().get(LOGIN_USER_KEY);
|
||||||
LOGIN_CACHE.set(loginUser);
|
SaHolder.getStorage().set(LOGIN_USER_KEY, loginUser);
|
||||||
return loginUser;
|
return loginUser;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 清除一级缓存 防止内存问题
|
|
||||||
*/
|
|
||||||
public static void clearCache() {
|
|
||||||
LOGIN_CACHE.remove();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取用户id
|
* 获取用户id
|
||||||
*/
|
*/
|
||||||
|
@ -6,7 +6,6 @@ import cn.dev33.satoken.jwt.StpLogicJwtForSimple;
|
|||||||
import cn.dev33.satoken.router.SaRouter;
|
import cn.dev33.satoken.router.SaRouter;
|
||||||
import cn.dev33.satoken.stp.StpLogic;
|
import cn.dev33.satoken.stp.StpLogic;
|
||||||
import cn.dev33.satoken.stp.StpUtil;
|
import cn.dev33.satoken.stp.StpUtil;
|
||||||
import com.ruoyi.common.helper.LoginHelper;
|
|
||||||
import com.ruoyi.common.utils.spring.SpringUtils;
|
import com.ruoyi.common.utils.spring.SpringUtils;
|
||||||
import com.ruoyi.framework.config.properties.ExcludeUrlProperties;
|
import com.ruoyi.framework.config.properties.ExcludeUrlProperties;
|
||||||
import com.ruoyi.framework.config.properties.SecurityProperties;
|
import com.ruoyi.framework.config.properties.SecurityProperties;
|
||||||
@ -17,9 +16,6 @@ import org.springframework.context.annotation.Configuration;
|
|||||||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
||||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||||
|
|
||||||
import javax.servlet.http.HttpServletRequest;
|
|
||||||
import javax.servlet.http.HttpServletResponse;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* sa-token 配置
|
* sa-token 配置
|
||||||
*
|
*
|
||||||
@ -59,13 +55,7 @@ public class SaTokenConfig implements WebMvcConfigurer {
|
|||||||
// }
|
// }
|
||||||
|
|
||||||
});
|
});
|
||||||
}) {
|
})).addPathPatterns("/**");
|
||||||
@SuppressWarnings("all")
|
|
||||||
@Override
|
|
||||||
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
|
|
||||||
LoginHelper.clearCache();
|
|
||||||
}
|
|
||||||
}).addPathPatterns("/**");
|
|
||||||
registry.addInterceptor(new SaAnnotationInterceptor()).addPathPatterns("/**");
|
registry.addInterceptor(new SaAnnotationInterceptor()).addPathPatterns("/**");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user