From 088ad1abcc82786aa77db76a6d1626ceddc44928 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=96=AF=E7=8B=82=E7=9A=84=E7=8B=AE=E5=AD=90Li?= <15040126243@163.com> Date: Fri, 3 Jun 2022 00:02:49 +0800 Subject: [PATCH] =?UTF-8?q?update=20=E4=BD=BF=E7=94=A8=20SaStorage=20?= =?UTF-8?q?=E4=BC=98=E5=8C=96=20LoginHelper=20=E4=B8=80=E7=BA=A7=E7=BC=93?= =?UTF-8?q?=E5=AD=98=20=E9=81=BF=E5=85=8D=20ThreadLocal=20=E6=B8=85?= =?UTF-8?q?=E7=90=86=E4=B8=8D=E5=B9=B2=E5=87=80=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/satoken/utils/LoginHelper.java | 18 +++++------------ .../config/SecurityConfiguration.java | 20 +------------------ 2 files changed, 6 insertions(+), 32 deletions(-) diff --git a/ruoyi-common/ruoyi-common-satoken/src/main/java/com/ruoyi/common/satoken/utils/LoginHelper.java b/ruoyi-common/ruoyi-common-satoken/src/main/java/com/ruoyi/common/satoken/utils/LoginHelper.java index 31ff44e5..8d6ea663 100644 --- a/ruoyi-common/ruoyi-common-satoken/src/main/java/com/ruoyi/common/satoken/utils/LoginHelper.java +++ b/ruoyi-common/ruoyi-common-satoken/src/main/java/com/ruoyi/common/satoken/utils/LoginHelper.java @@ -1,5 +1,6 @@ package com.ruoyi.common.satoken.utils; +import cn.dev33.satoken.context.SaHolder; import cn.dev33.satoken.stp.StpUtil; import cn.hutool.core.util.ObjectUtil; import com.ruoyi.common.core.constant.UserConstants; @@ -29,15 +30,13 @@ public class LoginHelper { public static final String JOIN_CODE = ":"; public static final String LOGIN_USER_KEY = "loginUser"; - private static final ThreadLocal LOGIN_CACHE = new ThreadLocal<>(); - /** * 登录系统 * * @param loginUser 登录用户信息 */ public static void login(LoginUser loginUser) { - LOGIN_CACHE.set(loginUser); + SaHolder.getStorage().set(LOGIN_USER_KEY, loginUser); StpUtil.login(loginUser.getLoginId()); setLoginUser(loginUser); } @@ -49,7 +48,7 @@ public class LoginHelper { * @param loginUser 登录用户信息 */ 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()); setLoginUser(loginUser); } @@ -65,22 +64,15 @@ public class LoginHelper { * 获取用户(多级缓存) */ public static LoginUser getLoginUser() { - LoginUser loginUser = LOGIN_CACHE.get(); + LoginUser loginUser = (LoginUser) SaHolder.getStorage().get(LOGIN_USER_KEY); if (loginUser != null) { return loginUser; } loginUser = (LoginUser) StpUtil.getTokenSession().get(LOGIN_USER_KEY); - LOGIN_CACHE.set(loginUser); + SaHolder.getStorage().set(LOGIN_USER_KEY, loginUser); return loginUser; } - /** - * 清除一级缓存 防止内存问题 - */ - public static void clearCache() { - LOGIN_CACHE.remove(); - } - /** * 获取用户id */ diff --git a/ruoyi-common/ruoyi-common-security/src/main/java/com/ruoyi/common/security/config/SecurityConfiguration.java b/ruoyi-common/ruoyi-common-security/src/main/java/com/ruoyi/common/security/config/SecurityConfiguration.java index bc1f9f3d..66ca3538 100644 --- a/ruoyi-common/ruoyi-common-security/src/main/java/com/ruoyi/common/security/config/SecurityConfiguration.java +++ b/ruoyi-common/ruoyi-common-security/src/main/java/com/ruoyi/common/security/config/SecurityConfiguration.java @@ -3,21 +3,15 @@ package com.ruoyi.common.security.config; import cn.dev33.satoken.filter.SaServletFilter; import cn.dev33.satoken.id.SaIdUtil; import cn.dev33.satoken.interceptor.SaAnnotationInterceptor; -import cn.dev33.satoken.interceptor.SaRouteInterceptor; -import cn.dev33.satoken.router.SaRouter; import cn.dev33.satoken.util.SaResult; import com.ruoyi.common.core.constant.HttpStatus; -import com.ruoyi.common.satoken.utils.LoginHelper; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - /** - * Sa-Token 整合 jwt (Style模式) + * 权限安全配置 * * @author Lion Li */ @@ -29,18 +23,6 @@ public class SecurityConfiguration implements WebMvcConfigurer { */ @Override public void addInterceptors(InterceptorRegistry registry) { - // 注册路由拦截器,自定义验证规则 - // 这里只处理登录缓存清理 具体拦截再网关处理 - registry.addInterceptor(new SaRouteInterceptor((request, response, handler) -> { - // 获取所有的 - SaRouter.match("/**"); - }) { - @SuppressWarnings("all") - @Override - public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { - LoginHelper.clearCache(); - } - }).addPathPatterns("/**"); // 注解拦截器 registry.addInterceptor(new SaAnnotationInterceptor()).addPathPatterns("/**"); }