update 使用 SaStorage 优化 LoginHelper 一级缓存 避免 ThreadLocal 清理不干净问题

feature/model
疯狂的狮子Li 2 years ago
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…
Cancel
Save