fix 修复 LoginHelper 获取 LoginUser 缺失子类扩展数据问题

2.X
疯狂的狮子li 2 years ago
parent 331238a057
commit 826c3f7ce5

@ -3,8 +3,8 @@ package com.ruoyi.common.satoken.utils;
import cn.dev33.satoken.context.SaHolder; import cn.dev33.satoken.context.SaHolder;
import cn.dev33.satoken.stp.SaLoginModel; import cn.dev33.satoken.stp.SaLoginModel;
import cn.dev33.satoken.stp.StpUtil; import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONObject;
import com.ruoyi.common.core.constant.UserConstants; import com.ruoyi.common.core.constant.UserConstants;
import com.ruoyi.common.core.enums.DeviceType; import com.ruoyi.common.core.enums.DeviceType;
import com.ruoyi.common.core.enums.UserType; import com.ruoyi.common.core.enums.UserType;
@ -12,15 +12,13 @@ import com.ruoyi.system.api.model.LoginUser;
import lombok.AccessLevel; import lombok.AccessLevel;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
import java.util.Set;
/** /**
* *
* * <p>
* user_type pc,app * user_type pc,app
* deivce web,ios * deivce web,ios
* *
* * <p>
* *
* *
* *
@ -30,7 +28,7 @@ import java.util.Set;
public class LoginHelper { public class LoginHelper {
public static final String LOGIN_USER_KEY = "loginUser"; public static final String LOGIN_USER_KEY = "loginUser";
public static final String MENU_PERMISSION = "menuPermission"; public static final String USER_KEY = "userId";
/** /**
* *
@ -49,30 +47,23 @@ public class LoginHelper {
*/ */
public static void loginByDevice(LoginUser loginUser, DeviceType deviceType) { public static void loginByDevice(LoginUser loginUser, DeviceType deviceType) {
SaHolder.getStorage().set(LOGIN_USER_KEY, loginUser); SaHolder.getStorage().set(LOGIN_USER_KEY, loginUser);
Set<String> menuPermission = loginUser.getMenuPermission();
loginUser.setMenuPermission(null);
SaLoginModel model = new SaLoginModel(); SaLoginModel model = new SaLoginModel();
if (ObjectUtil.isNotNull(deviceType)) { if (ObjectUtil.isNotNull(deviceType)) {
model.setDevice(deviceType.getDevice()); model.setDevice(deviceType.getDevice());
} }
StpUtil.login(loginUser.getLoginId(), model.setExtra(LOGIN_USER_KEY, loginUser)); StpUtil.login(loginUser.getLoginId(), model.setExtra(USER_KEY, loginUser.getUserId()));
// 解决菜单权限过多 token 臃肿过长问题 StpUtil.getTokenSession().set(LOGIN_USER_KEY, loginUser);
StpUtil.getTokenSession().set(MENU_PERMISSION, menuPermission);
} }
/** /**
* () * ()
*/ */
@SuppressWarnings("unchecked cast")
public static LoginUser getLoginUser() { public static LoginUser getLoginUser() {
LoginUser loginUser = (LoginUser) SaHolder.getStorage().get(LOGIN_USER_KEY); LoginUser loginUser = (LoginUser) SaHolder.getStorage().get(LOGIN_USER_KEY);
if (loginUser != null) { if (loginUser != null) {
return loginUser; return loginUser;
} }
loginUser = ((JSONObject) StpUtil.getExtra(LOGIN_USER_KEY)).toBean(LoginUser.class); loginUser = (LoginUser) StpUtil.getTokenSession().get(LOGIN_USER_KEY);
// 解决菜单权限过多 token 臃肿过长问题
Set<String> menuPermission = (Set<String>) StpUtil.getTokenSession().get(MENU_PERMISSION);
loginUser.setMenuPermission(menuPermission);
SaHolder.getStorage().set(LOGIN_USER_KEY, loginUser); SaHolder.getStorage().set(LOGIN_USER_KEY, loginUser);
return loginUser; return loginUser;
} }
@ -80,26 +71,21 @@ public class LoginHelper {
/** /**
* token * token
*/ */
@SuppressWarnings("unchecked cast")
public static LoginUser getLoginUser(String token) { public static LoginUser getLoginUser(String token) {
LoginUser loginUser = ((JSONObject) StpUtil.getExtra(token, LOGIN_USER_KEY)).toBean(LoginUser.class); return (LoginUser) StpUtil.getTokenSessionByToken(token).get(LOGIN_USER_KEY);
// 解决菜单权限过多 token 臃肿过长问题
Set<String> menuPermission = (Set<String>) StpUtil.getTokenSessionByToken(token).get(MENU_PERMISSION);
loginUser.setMenuPermission(menuPermission);
return loginUser;
} }
/** /**
* id * id
*/ */
public static Long getUserId() { public static Long getUserId() {
LoginUser loginUser; Long userId;
try { try {
loginUser = getLoginUser(); userId = Convert.toLong(StpUtil.getExtra(USER_KEY));
} catch (Exception e) { } catch (Exception e) {
return null; return null;
} }
return loginUser.getUserId(); return userId;
} }
/** /**

Loading…
Cancel
Save