From 9eff8467119d20aa7bd710d78f89fd194edd4a9f Mon Sep 17 00:00:00 2001 From: Charles7c Date: Sat, 19 Jul 2025 12:55:54 +0800 Subject: [PATCH] =?UTF-8?q?refactor(tenant):=20=E4=BC=98=E5=8C=96=E7=A7=9F?= =?UTF-8?q?=E6=88=B7=E9=85=8D=E7=BD=AE=EF=BC=8C=E5=A2=9E=E5=8A=A0=20defaul?= =?UTF-8?q?tTenantId=20=E5=B9=B6=E5=BC=B1=E5=8C=96=20TenantProperties=20?= =?UTF-8?q?=E4=BE=9D=E8=B5=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../config/DefaultTenantLineHandler.java | 73 +++++++++++++++++++ .../tenant/config/DefaultTenantProvider.java | 13 +--- .../tenant/config/TenantConfiguration.java | 36 +++++++++ .../config/TenantExtensionProperties.java | 13 ++-- .../tenant/controller/PackageController.java | 2 +- .../service/impl/TenantServiceImpl.java | 9 ++- .../src/main/resources/config/application.yml | 4 +- .../service/impl/OnlineUserServiceImpl.java | 3 - 8 files changed, 129 insertions(+), 24 deletions(-) create mode 100644 continew-plugin/continew-plugin-tenant/src/main/java/top/continew/admin/tenant/config/DefaultTenantLineHandler.java rename {continew-common/src/main/java/top/continew/admin/common => continew-plugin/continew-plugin-tenant/src/main/java/top/continew/admin/tenant}/config/TenantExtensionProperties.java (81%) diff --git a/continew-plugin/continew-plugin-tenant/src/main/java/top/continew/admin/tenant/config/DefaultTenantLineHandler.java b/continew-plugin/continew-plugin-tenant/src/main/java/top/continew/admin/tenant/config/DefaultTenantLineHandler.java new file mode 100644 index 00000000..380d52e4 --- /dev/null +++ b/continew-plugin/continew-plugin-tenant/src/main/java/top/continew/admin/tenant/config/DefaultTenantLineHandler.java @@ -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); + } +} \ No newline at end of file diff --git a/continew-plugin/continew-plugin-tenant/src/main/java/top/continew/admin/tenant/config/DefaultTenantProvider.java b/continew-plugin/continew-plugin-tenant/src/main/java/top/continew/admin/tenant/config/DefaultTenantProvider.java index 6c8fc21b..5ab8a716 100644 --- a/continew-plugin/continew-plugin-tenant/src/main/java/top/continew/admin/tenant/config/DefaultTenantProvider.java +++ b/continew-plugin/continew-plugin-tenant/src/main/java/top/continew/admin/tenant/config/DefaultTenantProvider.java @@ -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; diff --git a/continew-plugin/continew-plugin-tenant/src/main/java/top/continew/admin/tenant/config/TenantConfiguration.java b/continew-plugin/continew-plugin-tenant/src/main/java/top/continew/admin/tenant/config/TenantConfiguration.java index e516c3bd..4eedb563 100644 --- a/continew-plugin/continew-plugin-tenant/src/main/java/top/continew/admin/tenant/config/TenantConfiguration.java +++ b/continew-plugin/continew-plugin-tenant/src/main/java/top/continew/admin/tenant/config/TenantConfiguration.java @@ -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); + } } diff --git a/continew-common/src/main/java/top/continew/admin/common/config/TenantExtensionProperties.java b/continew-plugin/continew-plugin-tenant/src/main/java/top/continew/admin/tenant/config/TenantExtensionProperties.java similarity index 81% rename from continew-common/src/main/java/top/continew/admin/common/config/TenantExtensionProperties.java rename to continew-plugin/continew-plugin-tenant/src/main/java/top/continew/admin/tenant/config/TenantExtensionProperties.java index 0bc9e3c2..bf18e033 100644 --- a/continew-common/src/main/java/top/continew/admin/common/config/TenantExtensionProperties.java +++ b/continew-plugin/continew-plugin-tenant/src/main/java/top/continew/admin/tenant/config/TenantExtensionProperties.java @@ -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(租户不能使用的菜单) diff --git a/continew-plugin/continew-plugin-tenant/src/main/java/top/continew/admin/tenant/controller/PackageController.java b/continew-plugin/continew-plugin-tenant/src/main/java/top/continew/admin/tenant/controller/PackageController.java index 6fa0fe9c..6419c6c3 100644 --- a/continew-plugin/continew-plugin-tenant/src/main/java/top/continew/admin/tenant/controller/PackageController.java +++ b/continew-plugin/continew-plugin-tenant/src/main/java/top/continew/admin/tenant/controller/PackageController.java @@ -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; diff --git a/continew-plugin/continew-plugin-tenant/src/main/java/top/continew/admin/tenant/service/impl/TenantServiceImpl.java b/continew-plugin/continew-plugin-tenant/src/main/java/top/continew/admin/tenant/service/impl/TenantServiceImpl.java index dfbe13c5..63f95d5f 100644 --- a/continew-plugin/continew-plugin-tenant/src/main/java/top/continew/admin/tenant/service/impl/TenantServiceImpl.java +++ b/continew-plugin/continew-plugin-tenant/src/main/java/top/continew/admin/tenant/service/impl/TenantServiceImpl.java @@ -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 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 page(OnlineUserQuery query, PageQuery pageQuery) {