Skip to content

Commit

Permalink
refactor: 重构获取登录用户信息方式(线程级存储)
Browse files Browse the repository at this point in the history
  • Loading branch information
Charles7c committed Oct 10, 2024
1 parent 8466105 commit 79ea39d
Show file tree
Hide file tree
Showing 27 changed files with 522 additions and 362 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
package top.continew.admin.common.config.mybatis;

import cn.hutool.core.convert.Convert;
import top.continew.admin.common.model.dto.LoginUser;
import top.continew.admin.common.util.helper.LoginHelper;
import top.continew.admin.common.context.UserContextHolder;
import top.continew.starter.extension.datapermission.enums.DataScope;
import top.continew.starter.extension.datapermission.filter.DataPermissionUserContextProvider;
import top.continew.starter.extension.datapermission.model.RoleContext;
Expand All @@ -36,17 +35,16 @@ public class DefaultDataPermissionUserContextProvider implements DataPermissionU

@Override
public boolean isFilter() {
LoginUser loginUser = LoginHelper.getLoginUser();
return !loginUser.isAdmin();
return !UserContextHolder.isAdmin();
}

@Override
public UserContext getUserContext() {
LoginUser loginUser = LoginHelper.getLoginUser();
top.continew.admin.common.context.UserContext context = UserContextHolder.getContext();
UserContext userContext = new UserContext();
userContext.setUserId(Convert.toStr(loginUser.getId()));
userContext.setDeptId(Convert.toStr(loginUser.getDeptId()));
userContext.setRoles(loginUser.getRoles()
userContext.setUserId(Convert.toStr(context.getId()));
userContext.setDeptId(Convert.toStr(context.getDeptId()));
userContext.setRoles(context.getRoles()
.stream()
.map(r -> new RoleContext(Convert.toStr(r.getId()), DataScope.valueOf(r.getDataScope().name())))
.collect(Collectors.toSet()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import top.continew.admin.common.util.helper.LoginHelper;
import top.continew.admin.common.context.UserContextHolder;
import top.continew.starter.core.exception.BusinessException;
import top.continew.starter.core.util.ExceptionUtils;
import top.continew.starter.extension.crud.model.entity.BaseDO;

import java.time.LocalDateTime;
Expand Down Expand Up @@ -62,7 +61,7 @@ public void insertFill(MetaObject metaObject) {
if (null == metaObject) {
return;
}
Long createUser = ExceptionUtils.exToNull(LoginHelper::getUserId);
Long createUser = UserContextHolder.getUserId();
LocalDateTime createTime = LocalDateTime.now();
if (metaObject.getOriginalObject() instanceof BaseDO baseDO) {
// 继承了 BaseDO 的类,填充创建信息字段
Expand All @@ -89,8 +88,7 @@ public void updateFill(MetaObject metaObject) {
if (null == metaObject) {
return;
}

Long updateUser = LoginHelper.getUserId();
Long updateUser = UserContextHolder.getUserId();
LocalDateTime updateTime = LocalDateTime.now();
if (metaObject.getOriginalObject() instanceof BaseDO baseDO) {
// 继承了 BaseDO 的类,填充修改信息
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@

package top.continew.admin.common.config.websocket;

import cn.dev33.satoken.stp.StpUtil;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.http.server.ServletServerHttpRequest;
import org.springframework.stereotype.Component;
import top.continew.admin.common.model.dto.LoginUser;
import top.continew.admin.common.util.helper.LoginHelper;
import top.continew.starter.core.exception.BusinessException;
import top.continew.starter.messaging.websocket.core.WebSocketClientService;

/**
Expand All @@ -36,7 +36,9 @@ public class WebSocketClientServiceImpl implements WebSocketClientService {
public String getClientId(ServletServerHttpRequest request) {
HttpServletRequest servletRequest = request.getServletRequest();
String token = servletRequest.getParameter("token");
LoginUser loginUser = LoginHelper.getLoginUser(token);
return loginUser.getToken();
if (null == StpUtil.getLoginIdByToken(token)) {
throw new BusinessException("登录已过期,请重新登录");
}
return token;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,6 @@ public class CacheConstants {
*/
public static final String DELIMITER = StringConstants.COLON;

/**
* 登录用户键
*/
public static final String LOGIN_USER_KEY = "LOGIN_USER";

/**
* 验证码键前缀
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package top.continew.admin.common.model.dto;
package top.continew.admin.common.context;

import lombok.Data;
import top.continew.admin.common.enums.DataScopeEnum;
Expand All @@ -23,13 +23,13 @@
import java.io.Serializable;

/**
* 角色信息
* 角色上下文
*
* @author Charles7c
* @since 2023/3/7 22:08
*/
@Data
public class RoleDTO implements Serializable {
public class RoleContext implements Serializable {

@Serial
private static final long serialVersionUID = 1L;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package top.continew.admin.common.model.dto;
package top.continew.admin.common.context;

import cn.hutool.core.collection.CollUtil;
import lombok.Data;
Expand All @@ -28,14 +28,14 @@
import java.util.stream.Collectors;

/**
* 登录用户信息
* 用户上下文
*
* @author Charles7c
* @since 2022/12/24 13:01
* @since 2024/10/9 20:29
*/
@Data
@NoArgsConstructor
public class LoginUser implements Serializable {
public class UserContext implements Serializable {

@Serial
private static final long serialVersionUID = 1L;
Expand All @@ -56,69 +56,39 @@ public class LoginUser implements Serializable {
private Long deptId;

/**
* 权限码集合
*/
private Set<String> permissions;

/**
* 角色编码集合
*/
private Set<String> roleCodes;

/**
* 角色集合
*/
private Set<RoleDTO> roles;

/**
* 令牌
*/
private String token;

/**
* IP
*/
private String ip;

/**
* IP 归属地
*/
private String address;

/**
* 浏览器
* 最后一次修改密码时间
*/
private String browser;
private LocalDateTime pwdResetTime;

/**
* 操作系统
* 登录时系统设置的密码过期天数
*/
private String os;
private Integer passwordExpirationDays;

/**
* 登录时间
* 权限码集合
*/
private LocalDateTime loginTime;
private Set<String> permissions;

/**
* 最后一次修改密码时间
* 角色编码集合
*/
private LocalDateTime pwdResetTime;
private Set<String> roleCodes;

/**
* 登录时系统设置的密码过期天数
* 角色集合
*/
private Integer passwordExpirationDays;
private Set<RoleContext> roles;

public LoginUser(Set<String> permissions, Set<RoleDTO> roles, Integer passwordExpirationDays) {
public UserContext(Set<String> permissions, Set<RoleContext> roles, Integer passwordExpirationDays) {
this.permissions = permissions;
this.setRoles(roles);
this.passwordExpirationDays = passwordExpirationDays;
}

public void setRoles(Set<RoleDTO> roles) {
public void setRoles(Set<RoleContext> roles) {
this.roles = roles;
this.roleCodes = roles.stream().map(RoleDTO::getCode).collect(Collectors.toSet());
this.roleCodes = roles.stream().map(RoleContext::getCode).collect(Collectors.toSet());
}

/**
Expand Down
Loading

0 comments on commit 79ea39d

Please sign in to comment.