fix(后台管理): 租户条件逻辑优化,优化默认添加tenant_id = NULL改为不添加任何条件
This commit is contained in:
@@ -29,7 +29,7 @@ knife4j:
|
||||
zlt:
|
||||
#多租户配置
|
||||
tenant:
|
||||
enable: false
|
||||
enable: true
|
||||
ignoreTables:
|
||||
- edit_task
|
||||
- home_carousel
|
||||
@@ -40,16 +40,14 @@ zlt:
|
||||
- profit_sharing
|
||||
- order_statistics
|
||||
ignoreSqls:
|
||||
# 查询全部列表
|
||||
# - com.central.order.mapper.OrderMapper.findList
|
||||
# 获取某个订单
|
||||
# - com.central.order.mapper.OrderMapper.getOrderByNumber
|
||||
# 移除所有ignoreSqls配置,让所有SQL都经过租户过滤处理
|
||||
# 这样webApp租户的特殊处理逻辑才能生效
|
||||
noIgnoreSqls:
|
||||
- com.central.order.mapper.OrderMapper.findList
|
||||
- com.central.order.mapper.TourismMapper.findList
|
||||
- com.central.order.mapper.OrderFileMapper.findList
|
||||
- com.central.order.mapper.QrCodeMapper.findList
|
||||
- com.central.order.mapper.QrCodeMapper.save
|
||||
# 移除所有noIgnoreSqls配置,让所有SQL都经过租户过滤处理
|
||||
# - com.central.order.mapper.TourismMapper.findList
|
||||
# - com.central.order.mapper.OrderFileMapper.findList
|
||||
# - com.central.order.mapper.QrCodeMapper.findList
|
||||
# - com.central.order.mapper.QrCodeMapper.save
|
||||
# 微信支付商户号
|
||||
tencent:
|
||||
merchantId: ${zlt.wxpay.merchantId}
|
||||
@@ -140,4 +138,4 @@ cdn:
|
||||
# 日志配置
|
||||
logging:
|
||||
level:
|
||||
com.central.order.controller.OrderFileController: DEBUG
|
||||
com.central.order.controller.OrderFileController: DEBUG
|
||||
|
||||
@@ -39,7 +39,7 @@ zlt:
|
||||
- sys_user
|
||||
- sys_role_user
|
||||
- sys_role_menu
|
||||
- mini_app_config
|
||||
- wechat_config
|
||||
ignoreSqls:
|
||||
# 用户关联角色时,显示所有角色
|
||||
- com.central.user.mapper.SysRoleMapper.findAll
|
||||
|
||||
+10
@@ -9,6 +9,7 @@ import jakarta.servlet.FilterChain;
|
||||
import jakarta.servlet.ServletException;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.web.filter.OncePerRequestFilter;
|
||||
|
||||
@@ -20,6 +21,7 @@ import java.io.IOException;
|
||||
* @author zlt
|
||||
* @date 2019/9/15
|
||||
*/
|
||||
@Slf4j
|
||||
@ConditionalOnClass(Filter.class)
|
||||
public class TenantFilter extends OncePerRequestFilter {
|
||||
@Override
|
||||
@@ -31,13 +33,21 @@ public class TenantFilter extends OncePerRequestFilter {
|
||||
if (StrUtil.isEmpty(tenantId)) {
|
||||
tenantId = request.getHeader(SecurityConstants.TENANT_HEADER);
|
||||
}
|
||||
|
||||
log.debug("请求URL: {}, 租户ID: {}", request.getRequestURI(), tenantId);
|
||||
|
||||
//保存租户id
|
||||
if (StrUtil.isNotEmpty(tenantId)) {
|
||||
TenantContextHolder.setTenant(tenantId);
|
||||
log.debug("设置租户上下文: {}", tenantId);
|
||||
} else {
|
||||
log.debug("未找到租户ID,使用默认租户");
|
||||
}
|
||||
|
||||
filterChain.doFilter(request, response);
|
||||
} finally {
|
||||
String currentTenant = TenantContextHolder.getTenant();
|
||||
log.debug("清理租户上下文: {}", currentTenant);
|
||||
TenantContextHolder.clear();
|
||||
}
|
||||
}
|
||||
|
||||
+34
-7
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
+24
-3
@@ -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"));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user