refactor(tenant): 优化租户配置,增加 defaultTenantId 并弱化 TenantProperties 依赖

This commit is contained in:
2025-07-19 12:55:54 +08:00
parent aadf879be0
commit 9eff846711
8 changed files with 129 additions and 24 deletions

View File

@@ -0,0 +1,73 @@
/*
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package top.continew.admin.tenant.config;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.LongValue;
import net.sf.jsqlparser.expression.NullValue;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import top.continew.starter.extension.tenant.autoconfigure.TenantProperties;
import top.continew.starter.extension.tenant.context.TenantContextHolder;
import top.continew.starter.extension.tenant.enums.TenantIsolationLevel;
/**
* 默认租户行级隔离处理器,等待 ContiNew Starter 2.13.2 发布后替换
*
* @author Charles7c
* @since 2025/7/9 11:40
*/
public class DefaultTenantLineHandler implements TenantLineHandler {
private static final Logger log = LoggerFactory.getLogger(DefaultTenantLineHandler.class);
private final TenantProperties tenantProperties;
public DefaultTenantLineHandler(TenantProperties tenantProperties) {
this.tenantProperties = tenantProperties;
}
@Override
public Expression getTenantId() {
Long tenantId = TenantContextHolder.getTenantId();
if (tenantId != null) {
return new LongValue(tenantId);
}
log.warn("Tenant ID not found in current context.");
return new NullValue();
}
@Override
public String getTenantIdColumn() {
return tenantProperties.getTenantIdColumn();
}
@Override
public boolean ignoreTable(String tableName) {
// 忽略租户
if (TenantContextHolder.isIgnore()) {
return true;
}
// 忽略数据源级隔离
if (TenantIsolationLevel.DATASOURCE.equals(TenantContextHolder.getIsolationLevel())) {
return true;
}
// 忽略指定表
return CollUtil.contains(tenantProperties.getIgnoreTables(), tableName);
}
}

View File

@@ -19,13 +19,10 @@ package top.continew.admin.tenant.config;
import cn.hutool.core.util.StrUtil;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import top.continew.admin.common.config.TenantExtensionProperties;
import top.continew.admin.tenant.model.entity.TenantDO;
import top.continew.admin.tenant.service.TenantService;
import top.continew.starter.core.util.ServletUtils;
import top.continew.starter.core.util.validation.CheckUtils;
import top.continew.starter.extension.tenant.autoconfigure.TenantProperties;
import top.continew.starter.extension.tenant.config.TenantProvider;
import top.continew.starter.extension.tenant.context.TenantContext;
@@ -36,21 +33,19 @@ import top.continew.starter.extension.tenant.context.TenantContext;
* @author Charles7c
* @since 2024/12/12 15:35
*/
@Service
@RequiredArgsConstructor
public class DefaultTenantProvider implements TenantProvider {
private final TenantProperties tenantProperties;
private final TenantExtensionProperties tenantExtensionProperties;
private final TenantService tenantService;
@Override
public TenantContext getByTenantId(String tenantIdAsString, boolean verify) {
TenantContext context = new TenantContext();
Long superTenantId = tenantProperties.getSuperTenantId();
context.setTenantId(superTenantId);
// 超级租户
if (superTenantId.toString().equals(tenantIdAsString)) {
Long defaultTenantId = tenantExtensionProperties.getDefaultTenantId();
context.setTenantId(defaultTenantId);
// 默认租户
if (defaultTenantId.toString().equals(tenantIdAsString)) {
return context;
}
Long tenantId;

View File

@@ -16,9 +16,15 @@
package top.continew.admin.tenant.config;
import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler;
import org.springdoc.core.models.GroupedOpenApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import top.continew.admin.tenant.service.TenantService;
import top.continew.starter.extension.tenant.annotation.ConditionalOnEnabledTenant;
import top.continew.starter.extension.tenant.autoconfigure.TenantProperties;
import top.continew.starter.extension.tenant.config.TenantProvider;
/**
* 租户配置
@@ -29,6 +35,9 @@ import org.springframework.context.annotation.Configuration;
@Configuration
public class TenantConfiguration {
@Autowired(required = false)
private TenantProperties tenantProperties;
/**
* API 文档分组配置
*/
@@ -36,4 +45,31 @@ public class TenantConfiguration {
public GroupedOpenApi tenantApi() {
return GroupedOpenApi.builder().group("tenant").displayName("租户管理").pathsToMatch("/tenant/**").build();
}
/**
* 租户扩展配置属性
*/
@Bean
public TenantExtensionProperties tenantExtensionProperties() {
return new TenantExtensionProperties();
}
/**
* 租户行级隔离处理器(默认),等待 ContiNew Starter 2.13.2 发布后替换
*/
@Bean
@ConditionalOnEnabledTenant
public TenantLineHandler tenantLineHandler() {
return new DefaultTenantLineHandler(tenantProperties);
}
/**
* 租户提供者
*/
@Bean
@ConditionalOnEnabledTenant
public TenantProvider tenantProvider(TenantExtensionProperties tenantExtensionProperties,
TenantService tenantService) {
return new DefaultTenantProvider(tenantExtensionProperties, tenantService);
}
}

View File

@@ -14,11 +14,10 @@
* limitations under the License.
*/
package top.continew.admin.common.config;
package top.continew.admin.tenant.config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import top.continew.starter.core.constant.PropertiesConstants;
import java.util.List;
@@ -31,14 +30,18 @@ import java.util.List;
* @since 2024/11/29 12:05
*/
@Data
@Component
@ConfigurationProperties(prefix = PropertiesConstants.TENANT)
public class TenantExtensionProperties {
/**
* 请求头中租户编码键名
* 请求头中租户编码键名默认X-Tenant-Code
*/
private String tenantCodeHeader;
private String tenantCodeHeader = "X-Tenant-Code";
/**
* 默认租户 ID默认0
*/
private Long defaultTenantId = 0L;
/**
* 忽略菜单 ID租户不能使用的菜单

View File

@@ -24,7 +24,7 @@ import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import top.continew.admin.common.base.controller.BaseController;
import top.continew.admin.common.config.TenantExtensionProperties;
import top.continew.admin.tenant.config.TenantExtensionProperties;
import top.continew.admin.common.enums.DisEnableStatusEnum;
import top.continew.admin.system.model.query.MenuQuery;
import top.continew.admin.system.service.MenuService;

View File

@@ -25,6 +25,7 @@ import top.continew.admin.common.base.service.BaseServiceImpl;
import top.continew.admin.common.enums.DisEnableStatusEnum;
import top.continew.admin.system.model.entity.MenuDO;
import top.continew.admin.system.service.MenuService;
import top.continew.admin.tenant.config.TenantExtensionProperties;
import top.continew.admin.tenant.constant.TenantCacheConstants;
import top.continew.admin.tenant.constant.TenantConstants;
import top.continew.admin.tenant.handler.TenantDataHandler;
@@ -39,7 +40,6 @@ import top.continew.admin.tenant.service.PackageService;
import top.continew.admin.tenant.service.TenantService;
import top.continew.starter.cache.redisson.util.RedisUtils;
import top.continew.starter.core.util.validation.CheckUtils;
import top.continew.starter.extension.tenant.autoconfigure.TenantProperties;
import top.continew.starter.extension.tenant.util.TenantUtils;
import java.io.Serializable;
@@ -58,7 +58,7 @@ import java.util.List;
@RequiredArgsConstructor
public class TenantServiceImpl extends BaseServiceImpl<TenantMapper, TenantDO, TenantResp, TenantDetailResp, TenantQuery, TenantReq> implements TenantService {
private final TenantProperties tenantProperties;
private final TenantExtensionProperties tenantExtensionProperties;
private final PackageService packageService;
private final IdGeneratorProvider idGeneratorProvider;
private final TenantDataHandler tenantDataHandler;
@@ -129,10 +129,11 @@ public class TenantServiceImpl extends BaseServiceImpl<TenantMapper, TenantDO, T
@Override
public void checkStatus(Long id) {
TenantDO tenant = this.getById(id);
if (id.equals(tenantProperties.getSuperTenantId())) {
// 默认租户
if (tenantExtensionProperties.getDefaultTenantId().equals(id)) {
return;
}
TenantDO tenant = this.getById(id);
CheckUtils.throwIfEqual(DisEnableStatusEnum.DISABLE, tenant.getStatus(), "租户已被禁用");
CheckUtils.throwIf(tenant.getExpireTime() != null && tenant.getExpireTime()
.isBefore(LocalDateTime.now()), "租户已过期");

View File

@@ -202,12 +202,12 @@ continew-starter.tenant:
enabled: true
# 隔离级别默认LINE行级
isolation-level: LINE
# 超级/默认租户 ID超管用户所在租户默认0
super-tenant-id: 0
# 请求头中租户 ID 键名
tenant-id-header: X-Tenant-Id
# 请求头中租户编码键名
tenant-code-header: X-Tenant-Code
# 默认租户 ID超管用户所在租户默认0
default-tenant-id: 0
# 忽略表(忽略拼接租户条件)
ignore-tables:
- tenant # 租户表

View File

@@ -29,7 +29,6 @@ import org.springframework.stereotype.Service;
import top.continew.admin.auth.model.query.OnlineUserQuery;
import top.continew.admin.auth.model.resp.OnlineUserResp;
import top.continew.admin.auth.service.OnlineUserService;
import top.continew.admin.common.config.TenantExtensionProperties;
import top.continew.admin.common.context.UserContext;
import top.continew.admin.common.context.UserContextHolder;
import top.continew.admin.common.context.UserExtraContext;
@@ -52,8 +51,6 @@ import java.util.stream.Collectors;
@RequiredArgsConstructor
public class OnlineUserServiceImpl implements OnlineUserService {
private final TenantExtensionProperties tenantExtensionProperties;
@Override
@AutoOperate(type = OnlineUserResp.class, on = "list")
public PageResp<OnlineUserResp> page(OnlineUserQuery query, PageQuery pageQuery) {