多租户层级查询优化
This commit is contained in:
+6
-1
@@ -97,6 +97,11 @@ public class LoginUserContextHolder {
|
||||
return Arrays.asList(currentTenantId);
|
||||
}
|
||||
|
||||
// 优先使用登录时已初始化的可访问租户列表(包含自身与子孙)
|
||||
if (user != null && user.getAccessibleTenantIds() != null && !user.getAccessibleTenantIds().isEmpty()) {
|
||||
return user.getAccessibleTenantIds();
|
||||
}
|
||||
|
||||
// 管理员用户可以查询当前租户及其所有子租户
|
||||
try {
|
||||
ITenantHierarchyService tenantService = getBean(ITenantHierarchyService.class);
|
||||
@@ -233,4 +238,4 @@ public class LoginUserContextHolder {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,6 +27,10 @@ public interface UserService {
|
||||
@GetMapping(value = "/users/name/{username}")
|
||||
SysUser selectByUsername(@PathVariable("username") String username);
|
||||
|
||||
|
||||
@GetMapping("/users/{id}")
|
||||
SysUser findUserById(@PathVariable("id") Long id);
|
||||
|
||||
/**
|
||||
* feign rpc访问远程/users-anon/login接口
|
||||
*
|
||||
@@ -81,4 +85,10 @@ public interface UserService {
|
||||
|
||||
@GetMapping("/users-anon/miniApp")
|
||||
MiniAppConfig getByAppid(@RequestParam("appCode") String appCode);
|
||||
|
||||
/**
|
||||
* 获取自己以及子孙租户
|
||||
*/
|
||||
@GetMapping(value = "/tenant/hierarchy/son/tenantId", params = "tenantId")
|
||||
List<String> getSonTenantIds(@RequestParam("tenantId") String tenantId);
|
||||
}
|
||||
|
||||
+11
@@ -27,6 +27,11 @@ public class UserServiceFallbackFactory implements FallbackFactory<UserService>
|
||||
return new SysUser();
|
||||
}
|
||||
|
||||
@Override
|
||||
public SysUser findUserById(Long id) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public SysUser findByUsername(String username) {
|
||||
log.error("通过用户名查询用户异常:{}", username, throwable);
|
||||
@@ -78,6 +83,12 @@ public class UserServiceFallbackFactory implements FallbackFactory<UserService>
|
||||
public MiniAppConfig getByAppid(String appid) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getSonTenantIds(String tenantId) {
|
||||
log.error("获取自己以及子孙租户列表异常:{}", tenantId, throwable);
|
||||
return Collections.emptyList();
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
+9
-37
@@ -1,7 +1,6 @@
|
||||
package com.central.common.service;
|
||||
|
||||
import com.central.common.model.LoginAppUser;
|
||||
import com.central.common.service.ITenantHierarchyService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
@@ -13,9 +12,6 @@ import java.util.List;
|
||||
/**
|
||||
* 用户租户信息初始化器
|
||||
* 在用户登录时初始化租户相关信息
|
||||
*
|
||||
* @author zlt
|
||||
* @date 2024-12-19
|
||||
*/
|
||||
@Component
|
||||
@Slf4j
|
||||
@@ -25,10 +21,7 @@ public class UserTenantInitializer {
|
||||
private ITenantHierarchyService tenantHierarchyService;
|
||||
|
||||
/**
|
||||
* 初始化用户租户信息
|
||||
* 在用户登录成功后调用此方法
|
||||
*
|
||||
* @param user 登录用户
|
||||
* 初始化用户租户信息(登录成功后调用)
|
||||
*/
|
||||
public void initializeUserTenantInfo(LoginAppUser user) {
|
||||
if (user == null) {
|
||||
@@ -38,7 +31,7 @@ public class UserTenantInitializer {
|
||||
|
||||
String userTenantId = getUserTenantId(user);
|
||||
if (!StringUtils.hasText(userTenantId)) {
|
||||
log.warn("用户 {} 未分配租户", user.getUsername());
|
||||
log.warn("用户 {} 未分配租户ID,将使用默认租户配置", user.getUsername());
|
||||
setDefaultTenantInfo(user);
|
||||
return;
|
||||
}
|
||||
@@ -71,45 +64,32 @@ public class UserTenantInitializer {
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取用户租户ID
|
||||
* 优先从用户属性中获取,如果没有则从公司字段获取
|
||||
* 获取用户租户ID:优先 attributes.company,其次 user.company
|
||||
*/
|
||||
private String getUserTenantId(LoginAppUser user) {
|
||||
// 优先从属性中获取
|
||||
Object companyFromAttributes = user.getAttributes().get("company");
|
||||
if (companyFromAttributes != null && StringUtils.hasText(companyFromAttributes.toString())) {
|
||||
return companyFromAttributes.toString();
|
||||
}
|
||||
|
||||
// 从company字段获取
|
||||
if (StringUtils.hasText(user.getCompany())) {
|
||||
return user.getCompany();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断是否为管理用户
|
||||
* 管理用户可以切换租户,普通用户只能访问自己的租户
|
||||
* 管理用户可切换租户,普通用户仅访问自身租户
|
||||
*/
|
||||
private boolean isManagementUser(LoginAppUser user) {
|
||||
// 根据用户类型判断:BACKEND用户为管理员,APP用户为C端用户
|
||||
String userType = getUserType(user);
|
||||
return "BACKEND".equals(userType);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取用户类型
|
||||
*/
|
||||
private String getUserType(LoginAppUser user) {
|
||||
// 优先从属性中获取
|
||||
Object typeFromAttributes = user.getAttributes().get("type");
|
||||
if (typeFromAttributes != null) {
|
||||
return typeFromAttributes.toString();
|
||||
}
|
||||
|
||||
// 默认返回BACKEND(兼容处理)
|
||||
return "BACKEND";
|
||||
}
|
||||
|
||||
@@ -123,6 +103,7 @@ public class UserTenantInitializer {
|
||||
}
|
||||
|
||||
// 管理员用户可以访问当前租户及其所有子租户
|
||||
// 优先通过本地服务查询(若存在)
|
||||
if (tenantHierarchyService != null) {
|
||||
try {
|
||||
List<String> subTenants = tenantHierarchyService.getAllSubTenantIds(userTenantId);
|
||||
@@ -130,11 +111,9 @@ public class UserTenantInitializer {
|
||||
return subTenants;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.warn("获取子租户列表失败: tenantId={}, error={}", userTenantId, e.getMessage());
|
||||
log.warn("获取子租户列表失败(本地): tenantId={}, error={}", userTenantId, e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
// 降级处理:返回用户自己的租户
|
||||
return Arrays.asList(userTenantId);
|
||||
}
|
||||
|
||||
@@ -168,32 +147,25 @@ public class UserTenantInitializer {
|
||||
if (user == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// 检查基本字段
|
||||
if (!StringUtils.hasText(user.getCurrentTenantId())) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// 检查可访问租户列表
|
||||
List<String> accessibleTenants = user.getAccessibleTenantIds();
|
||||
if (accessibleTenants == null || accessibleTenants.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// 检查当前租户是否在可访问列表中
|
||||
return accessibleTenants.contains(user.getCurrentTenantId());
|
||||
}
|
||||
|
||||
/**
|
||||
* 刷新用户租户信息
|
||||
* 可在租户信息变更后调用
|
||||
* 刷新用户租户信息(在租户变更后调用)
|
||||
*/
|
||||
public void refreshUserTenantInfo(LoginAppUser user) {
|
||||
if (user == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
log.info("刷新用户 {} 的租户信息", user.getUsername());
|
||||
initializeUserTenantInfo(user);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+13
-4
@@ -204,14 +204,23 @@ public class LoginUserUtils {
|
||||
*/
|
||||
private static void initializeUserTenantInfo(LoginAppUser user) {
|
||||
try {
|
||||
UserTenantInitializer tenantInitializer = SpringUtil.getBean(UserTenantInitializer.class);
|
||||
org.springframework.context.ApplicationContext ctx = SpringUtil.getApplicationContext();
|
||||
UserTenantInitializer tenantInitializer = null;
|
||||
if (ctx != null) {
|
||||
try {
|
||||
tenantInitializer = ctx.getBeanProvider(UserTenantInitializer.class).getIfAvailable();
|
||||
} catch (Exception ignore) {
|
||||
// 忽略:容器中不存在该 Bean
|
||||
}
|
||||
}
|
||||
if (tenantInitializer != null) {
|
||||
tenantInitializer.initializeUserTenantInfo(user);
|
||||
} else {
|
||||
log.debug("未发现 UserTenantInitializer Bean,跳过租户初始化");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
// 如果租户初始化器不存在或初始化失败,记录日志但不影响登录
|
||||
// 这样可以保证在租户功能未完全部署时,系统仍能正常运行
|
||||
System.err.println("租户信息初始化失败,但不影响登录: " + e.getMessage());
|
||||
// 记录为 warn,且不影响登录流程
|
||||
log.warn("租户信息初始化失败(忽略,不影响登录): {}", e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user