update [重磅更新] 登录流程升级 支持缓存

This commit is contained in:
疯狂的狮子li
2021-12-03 18:46:49 +08:00
parent 5a271b8fde
commit e763381186
19 changed files with 179 additions and 105 deletions

View File

@ -1,8 +1,8 @@
package com.ruoyi.common.core.controller;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.LoginUtils;
import com.ruoyi.common.utils.StringUtils;
/**
@ -70,28 +70,28 @@ public class BaseController {
/**
* 获取用户缓存信息
*/
public SysUser getUser() {
return SecurityUtils.getUser();
public LoginUser getUser() {
return LoginUtils.getLoginUser();
}
/**
* 获取登录用户id
*/
public Long getUserId() {
return SecurityUtils.getUserId();
return LoginUtils.getUserId();
}
/**
* 获取登录部门id
*/
public Long getDeptId() {
return SecurityUtils.getDeptId();
return LoginUtils.getDeptId();
}
/**
* 获取登录用户名
*/
public String getUsername() {
return SecurityUtils.getUsername();
return LoginUtils.getUsername();
}
}

View File

@ -0,0 +1,83 @@
package com.ruoyi.common.core.domain.model;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import java.io.Serializable;
import java.util.Set;
/**
* 登录用户身份权限
*
* @author Lion Li
*/
@Data
@NoArgsConstructor
@Accessors(chain = true)
public class LoginUser implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 用户ID
*/
private Long userId;
/**
* 部门ID
*/
private Long deptId;
/**
* 用户唯一标识
*/
private String token;
/**
* 登录时间
*/
private Long loginTime;
/**
* 过期时间
*/
private Long expireTime;
/**
* 登录IP地址
*/
private String ipaddr;
/**
* 登录地点
*/
private String loginLocation;
/**
* 浏览器类型
*/
private String browser;
/**
* 操作系统
*/
private String os;
/**
* 菜单权限
*/
private Set<String> menuPermission;
/**
* 角色权限
*/
private Set<String> rolePermission;
/**
* 用户名
*/
private String username;
}

View File

@ -10,7 +10,7 @@ import com.ruoyi.common.exception.base.BaseException;
public class UserException extends BaseException {
private static final long serialVersionUID = 1L;
public UserException(String code, Object[] args) {
public UserException(String code, Object... args) {
super("user", code, args, null);
}
}

View File

@ -1,6 +1,8 @@
package com.ruoyi.common.utils;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.enums.DeviceType;
import com.ruoyi.common.enums.UserType;
import com.ruoyi.common.exception.UtilException;
@ -13,39 +15,68 @@ import com.ruoyi.common.exception.UtilException;
*/
public class LoginUtils {
private final static String LOGIN_USER_KEY = "loginUser";
/**
* 登录系统
* 针对两套用户体系
* @param userId 用户id
* @param loginUser 登录用户信息
*/
public static void login(Long userId, UserType userType) {
StpUtil.login(userType.getUserType() + userId);
public static void login(LoginUser loginUser, UserType userType) {
StpUtil.login(userType.getUserType() + loginUser.getUserId());
StpUtil.getTokenSession().set(LOGIN_USER_KEY, loginUser);
}
/**
* 登录系统 基于 设备类型
* 针对一套用户体系
* @param userId 用户id
* @param loginUser 登录用户信息
*/
public static void loginByDevice(Long userId, UserType userType, DeviceType deviceType) {
StpUtil.login(userType.getUserType() + userId, deviceType.getDevice());
public static void loginByDevice(LoginUser loginUser, UserType userType, DeviceType deviceType) {
StpUtil.login(userType.getUserType() + loginUser.getUserId(), deviceType.getDevice());
StpUtil.getTokenSession().set(LOGIN_USER_KEY, loginUser);
}
/**
* 获取用户
**/
public static LoginUser getLoginUser() {
return (LoginUser) StpUtil.getTokenSession().get(LOGIN_USER_KEY);
}
/**
* 获取用户id
*/
public static Long getUserId() {
String loginId = StpUtil.getLoginIdAsString();
String userId;
String replace = "";
if (StringUtils.contains(loginId, UserType.SYS_USER.getUserType())) {
userId = StringUtils.replace(loginId, UserType.SYS_USER.getUserType(), replace);
} else if (StringUtils.contains(loginId, UserType.APP_USER.getUserType())){
userId = StringUtils.replace(loginId, UserType.APP_USER.getUserType(), replace);
} else {
throw new UtilException("登录用户: LoginId异常 => " + loginId);
LoginUser loginUser = getLoginUser();
if (ObjectUtil.isNull(loginUser)) {
String loginId = StpUtil.getLoginIdAsString();
String userId;
String replace = "";
if (StringUtils.contains(loginId, UserType.SYS_USER.getUserType())) {
userId = StringUtils.replace(loginId, UserType.SYS_USER.getUserType(), replace);
} else if (StringUtils.contains(loginId, UserType.APP_USER.getUserType())){
userId = StringUtils.replace(loginId, UserType.APP_USER.getUserType(), replace);
} else {
throw new UtilException("登录用户: LoginId异常 => " + loginId);
}
return Long.parseLong(userId);
}
return Long.parseLong(userId);
return loginUser.getUserId();
}
/**
* 获取部门ID
**/
public static Long getDeptId() {
return getLoginUser().getDeptId();
}
/**
* 获取用户账户
**/
public static String getUsername() {
return getLoginUser().getUsername();
}
/**

View File

@ -13,41 +13,13 @@ import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
* @author Long Li
*/
public class SecurityUtils {
/**
* 用户ID
**/
public static Long getUserId() {
return LoginUtils.getUserId();
}
/**
* 获取部门ID
**/
public static Long getDeptId() {
try {
return getUser().getDeptId();
} catch (Exception e) {
throw new ServiceException("获取部门ID异常", HttpStatus.HTTP_UNAUTHORIZED);
}
}
/**
* 获取用户账户
**/
public static String getUsername() {
try {
return getUser().getUserName();
} catch (Exception e) {
throw new ServiceException("获取用户账户异常", HttpStatus.HTTP_UNAUTHORIZED);
}
}
/**
* 获取用户
**/
public static SysUser getUser() {
try {
return SpringUtils.getBean(UserService.class).selectUserById(getUserId());
return SpringUtils.getBean(UserService.class).selectUserById(LoginUtils.getUserId());
} catch (Exception e) {
throw new ServiceException("获取用户信息异常", HttpStatus.HTTP_UNAUTHORIZED);
}