From 8286b9f6ead37481ddb792a6429d43aaeb6f6155 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A6=82=E6=A2=A6=E6=8A=80=E6=9C=AF?= <596392912@qq.com> Date: Mon, 29 Apr 2024 16:26:05 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20=E6=B7=BB=E5=8A=A0=20TenantIdUtil?= =?UTF-8?q?=20=E6=96=B9=E4=BE=BF=E5=9C=A8=E9=9D=9E=20web=20=E5=9C=BA?= =?UTF-8?q?=E6=99=AF=E4=BD=BF=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/boot/tenant/BladeTenantHandler.java | 6 +- .../boot/tenant/BladeTenantProperties.java | 5 ++ .../core/boot/tenant/TenantIdUtil.java | 62 +++++++++++++++++++ 3 files changed, 70 insertions(+), 3 deletions(-) create mode 100644 blade-core-boot/src/main/java/org/springblade/core/boot/tenant/TenantIdUtil.java diff --git a/blade-core-boot/src/main/java/org/springblade/core/boot/tenant/BladeTenantHandler.java b/blade-core-boot/src/main/java/org/springblade/core/boot/tenant/BladeTenantHandler.java index 0780d47..031a206 100644 --- a/blade-core-boot/src/main/java/org/springblade/core/boot/tenant/BladeTenantHandler.java +++ b/blade-core-boot/src/main/java/org/springblade/core/boot/tenant/BladeTenantHandler.java @@ -42,7 +42,7 @@ public class BladeTenantHandler implements TenantLineHandler { */ @Override public Expression getTenantId() { - return new StringValue(Func.toStr(SecureUtil.getTenantId(), TenantConstant.DEFAULT_TENANT_ID)); + return new StringValue(Func.toStr(TenantIdUtil.get(), properties.getDefaultTenantId())); } /** @@ -65,10 +65,10 @@ public class BladeTenantHandler implements TenantLineHandler { public boolean ignoreTable(String tableName) { return !( ( - (properties.getTables().size() > 0 && properties.getTables().contains(tableName)) + (!properties.getTables().isEmpty() && properties.getTables().contains(tableName)) || properties.getBladeTables().contains(tableName) ) - && StringUtil.isNotBlank(SecureUtil.getTenantId()) + && StringUtil.isNotBlank(TenantIdUtil.get()) ); } } diff --git a/blade-core-boot/src/main/java/org/springblade/core/boot/tenant/BladeTenantProperties.java b/blade-core-boot/src/main/java/org/springblade/core/boot/tenant/BladeTenantProperties.java index ef7949a..2078415 100644 --- a/blade-core-boot/src/main/java/org/springblade/core/boot/tenant/BladeTenantProperties.java +++ b/blade-core-boot/src/main/java/org/springblade/core/boot/tenant/BladeTenantProperties.java @@ -38,6 +38,11 @@ public class BladeTenantProperties { */ private String column = "tenant_id"; + /** + * 默认的租户id,默认为:000000 + */ + private String defaultTenantId = TenantConstant.DEFAULT_TENANT_ID; + /** * 多租户数据表 */ diff --git a/blade-core-boot/src/main/java/org/springblade/core/boot/tenant/TenantIdUtil.java b/blade-core-boot/src/main/java/org/springblade/core/boot/tenant/TenantIdUtil.java new file mode 100644 index 0000000..28c3375 --- /dev/null +++ b/blade-core-boot/src/main/java/org/springblade/core/boot/tenant/TenantIdUtil.java @@ -0,0 +1,62 @@ +/** + * Copyright (c) 2018-2028, Chill Zhuang 庄骞 (smallchill@163.com). + *

+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.gnu.org/licenses/lgpl.html + *

+ * 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 org.springblade.core.boot.tenant; + +import org.springblade.core.secure.utils.SecureUtil; +import org.springframework.util.Assert; + +import java.util.function.Supplier; + +/** + * TenantId 工具 + * + * @author L.cm + */ +public class TenantIdUtil { + private static final ThreadLocal tl = new ThreadLocal<>(); + + /** + * 获取租户id + * + * @return 租户id + */ + public static String get() { + String tenantId = tl.get(); + if (tenantId != null) { + return tenantId; + } + return SecureUtil.getTenantId(); + } + + /** + * 使用租户 id 执行函数 + * + * @param tenantId tenantId + * @param supplier supplier + * @param 泛型 + * @return R 函数返回 + */ + public static R use(String tenantId, Supplier supplier) { + Assert.hasText(tenantId, "参数 tenantId 为空"); + tl.set(tenantId); + try { + return supplier.get(); + } finally { + tl.remove(); + } + } + +}