From 380cb2db29b6918f7c37394de09c88ed6910b9e8 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, 17 Feb 2023 22:45:59 +0800 Subject: [PATCH] =?UTF-8?q?fix=20=E4=BF=AE=E5=A4=8D=20=E5=9B=A0=E8=8F=9C?= =?UTF-8?q?=E5=8D=95=E6=9D=83=E9=99=90=E8=BF=87=E5=A4=9A=20=E5=AF=BC?= =?UTF-8?q?=E8=87=B4=20token=20=E8=87=83=E8=82=BF=E8=BF=87=E9=95=BF?= =?UTF-8?q?=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 | 50 ++++++++++--------- 1 file changed, 26 insertions(+), 24 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 1bc41976..b8f35b22 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 @@ -10,8 +10,6 @@ import com.ruoyi.common.core.constant.UserConstants; import com.ruoyi.common.core.domain.model.LoginUser; import com.ruoyi.common.core.enums.DeviceType; import com.ruoyi.common.core.enums.UserType; -import com.ruoyi.common.core.exception.UtilException; -import com.ruoyi.common.core.utils.StringUtils; import lombok.AccessLevel; import lombok.NoArgsConstructor; @@ -32,8 +30,8 @@ import java.util.Set; @NoArgsConstructor(access = AccessLevel.PRIVATE) public class LoginHelper { - public static final String JOIN_CODE = ":"; public static final String LOGIN_USER_KEY = "loginUser"; + public static final String MENU_PERMISSION = "menuPermission"; /** * 登录系统 @@ -41,8 +39,7 @@ public class LoginHelper { * @param loginUser 登录用户信息 */ public static void login(LoginUser loginUser) { - SaHolder.getStorage().set(LOGIN_USER_KEY, loginUser); - StpUtil.login(loginUser.getLoginId(), new SaLoginModel().setExtra(LOGIN_USER_KEY, loginUser)); + loginByDevice(loginUser, null); } /** @@ -53,20 +50,30 @@ public class LoginHelper { */ public static void loginByDevice(LoginUser loginUser, DeviceType deviceType) { SaHolder.getStorage().set(LOGIN_USER_KEY, loginUser); - StpUtil.login(loginUser.getLoginId(), new SaLoginModel() - .setDevice(deviceType.getDevice()) - .setExtra(LOGIN_USER_KEY, loginUser)); + Set menuPermission = loginUser.getMenuPermission(); + loginUser.setMenuPermission(null); + SaLoginModel model = new SaLoginModel(); + if (ObjectUtil.isNotNull(deviceType)) { + model.setDevice(deviceType.getDevice()); + } + StpUtil.login(loginUser.getLoginId(), model.setExtra(LOGIN_USER_KEY, loginUser)); + // 解决菜单权限过度 token 臃肿过长问题 + StpUtil.getTokenSession().set(MENU_PERMISSION, menuPermission); } /** * 获取用户(多级缓存) */ + @SuppressWarnings("unchecked cast") public static LoginUser getLoginUser() { LoginUser loginUser = (LoginUser) SaHolder.getStorage().get(LOGIN_USER_KEY); if (loginUser != null) { return loginUser; } loginUser = ((JSONObject) StpUtil.getExtra(LOGIN_USER_KEY)).toBean(LoginUser.class); + // 解决菜单权限过度 token 臃肿过长问题 + Set menuPermission = (Set) StpUtil.getTokenSession().get(MENU_PERMISSION); + loginUser.setMenuPermission(menuPermission); SaHolder.getStorage().set(LOGIN_USER_KEY, loginUser); return loginUser; } @@ -74,29 +81,24 @@ public class LoginHelper { /** * 获取用户基于token */ + @SuppressWarnings("unchecked cast") public static LoginUser getLoginUser(String token) { - return ((JSONObject) StpUtil.getExtra(token, LOGIN_USER_KEY)).toBean(LoginUser.class); + LoginUser loginUser = ((JSONObject) StpUtil.getExtra(token, LOGIN_USER_KEY)).toBean(LoginUser.class); + // 解决菜单权限过度 token 臃肿过长问题 + Set menuPermission = (Set) StpUtil.getTokenSessionByToken(token).get(MENU_PERMISSION); + loginUser.setMenuPermission(menuPermission); + return loginUser; } /** * 获取用户id */ public static Long getUserId() { - LoginUser loginUser = getLoginUser(); - if (ObjectUtil.isNull(loginUser)) { - String loginId = StpUtil.getLoginIdAsString(); - String userId = null; - for (UserType value : UserType.values()) { - if (StringUtils.contains(loginId, value.getUserType())) { - String[] strs = StringUtils.split(loginId, JOIN_CODE); - // 用户id在总是在最后 - userId = strs[strs.length - 1]; - } - } - if (StringUtils.isBlank(userId)) { - throw new UtilException("登录用户: LoginId异常 => " + loginId); - } - return Long.parseLong(userId); + LoginUser loginUser; + try { + loginUser = getLoginUser(); + } catch (Exception e) { + return null; } return loginUser.getUserId(); }