fix(后台管理): 租户条件逻辑优化,优化默认添加tenant_id = NULL改为不添加任何条件

This commit is contained in:
2025-07-29 22:38:21 +08:00
parent beabf92496
commit 9cbada69d3
5 changed files with 78 additions and 22 deletions
@@ -5,8 +5,8 @@ import com.central.common.context.LoginUserContextHolder;
import com.central.common.context.TenantContextHolder;
import com.central.common.model.LoginAppUser;
import com.central.common.properties.TenantProperties;
import lombok.extern.slf4j.Slf4j;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.NullValue;
import net.sf.jsqlparser.expression.StringValue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
@@ -18,6 +18,7 @@ import org.springframework.context.annotation.Bean;
* @author zlt
* @date 2019/8/5
*/
@Slf4j
@EnableConfigurationProperties(TenantProperties.class)
public class TenantAutoConfigure {
@Autowired
@@ -31,21 +32,29 @@ public class TenantAutoConfigure {
*/
@Override
public Expression getTenantId() {
log.debug("=== 开始处理租户过滤 ===");
// 判断当前用户是否为超级管理员
if (isSuperAdmin()) {
// 超级管理员返回Null,不加租户条件
return new NullValue();
log.debug("超级管理员用户,跳过租户过滤");
return new StringValue(""); // 返回空字符串,避免NULL条件
}
String tenant = TenantContextHolder.getTenant();
log.debug("当前租户: {}", tenant);
if (tenant != null) {
// webApp 租户可以查询所有数据,返回 Null 不加租户条件
// webApp 租户可以查询所有数据,返回空字符串
if ("webApp".equals(tenant)) {
return new NullValue();
log.debug("webApp租户,跳过租户过滤,可以查询所有数据");
return new StringValue(""); // 返回空字符串,避免NULL条件
}
log.debug("普通租户: {},添加租户过滤条件", tenant);
return new StringValue(TenantContextHolder.getTenant());
}
return new NullValue();
log.debug("无租户信息,跳过租户过滤");
return new StringValue(""); // 返回空字符串,避免NULL条件
}
/**
@@ -54,9 +63,23 @@ public class TenantAutoConfigure {
*/
@Override
public boolean ignoreTable(String tableName) {
return tenantProperties.getIgnoreTables().stream().anyMatch(
// 获取当前租户
String tenant = TenantContextHolder.getTenant();
// webApp 租户或超级管理员跳过所有表的租户过滤
if ("webApp".equals(tenant) || isSuperAdmin()) {
log.debug("webApp租户或超级管理员,表 {} 被忽略租户过滤", tableName);
return true;
}
// 检查配置的忽略表
boolean ignore = tenantProperties.getIgnoreTables().stream().anyMatch(
(e) -> e.equalsIgnoreCase(tableName)
);
if (ignore) {
log.debug("表 {} 被忽略租户过滤", tableName);
}
return ignore;
}
};
}
@@ -66,6 +89,10 @@ public class TenantAutoConfigure {
// 例如:
LoginAppUser user = LoginUserContextHolder.getUser();
//@TODO 暂定admin和mgnt为系统管理员
return user != null && (user.getUsername().equals("admin") || user.getUsername().equals("mgnt"));
boolean isAdmin = user != null && (user.getUsername().equals("admin") || user.getUsername().equals("mgnt"));
if (isAdmin) {
log.debug("当前用户为超级管理员: {}", user.getUsername());
}
return isAdmin;
}
}
@@ -2,6 +2,7 @@ package com.central.db.interceptor;
import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler;
import com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
@@ -21,6 +22,7 @@ import java.util.List;
* Blog: http://zlt2000.gitee.io
* Github: https://github.com/zlt2000
*/
@Slf4j
public class CustomTenantInterceptor extends TenantLineInnerInterceptor {
private List<String> ignoreSqls;
private List<String> noIgnoreSqls;
@@ -34,13 +36,27 @@ public class CustomTenantInterceptor extends TenantLineInnerInterceptor {
@Override
public void beforeQuery(Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds
, ResultHandler resultHandler, BoundSql boundSql) throws SQLException {
if (isIgnoreMappedStatement(ms.getId())) {
String msId = ms.getId();
log.debug("租户拦截器处理SQL: {}", msId);
// 获取当前租户
String tenant = com.central.common.context.TenantContextHolder.getTenant();
// webApp 租户或超级管理员直接跳过租户过滤
if ("webApp".equals(tenant) || isSuperAdmin()) {
log.debug("webApp租户或超级管理员,SQL {} 跳过租户过滤", msId);
return;
}
if (isNoIgnoreMappedStatement(ms.getId())) {
super.beforeQuery(executor, ms, parameter, rowBounds, resultHandler, boundSql);
// 如果在忽略列表中,直接返回,不执行租户过滤
if (isIgnoreMappedStatement(msId)) {
log.debug("SQL {} 在忽略列表中,跳过租户过滤", msId);
return;
}
// 所有其他SQL都执行租户过滤处理
log.debug("SQL {} 执行租户过滤", msId);
super.beforeQuery(executor, ms, parameter, rowBounds, resultHandler, boundSql);
}
private boolean isIgnoreMappedStatement(String msId) {
@@ -50,4 +66,9 @@ public class CustomTenantInterceptor extends TenantLineInnerInterceptor {
private boolean isNoIgnoreMappedStatement(String msId) {
return noIgnoreSqls.stream().anyMatch((e) -> e.equalsIgnoreCase(msId));
}
private boolean isSuperAdmin() {
com.central.common.model.LoginAppUser user = com.central.common.context.LoginUserContextHolder.getUser();
return user != null && (user.getUsername().equals("admin") || user.getUsername().equals("mgnt"));
}
}