feat(多租户):多租户数据隔离功能实现

This commit is contained in:
2025-05-15 16:51:48 +08:00
parent 4beecb5d3d
commit d4c39ac6b5
15 changed files with 134 additions and 27 deletions
@@ -0,0 +1,34 @@
package com.central.db.interceptor;
import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler;
import com.central.common.context.LoginUserContextHolder;
import com.central.common.context.TenantContextHolder;
import com.central.common.model.LoginAppUser;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.NullValue;
import net.sf.jsqlparser.expression.StringValue;
/**
* 超级管理员可以管理租户数据权限开放
* @date 2025-05-15
* @author lsl
*/
public class CustomTenantLineHandler implements TenantLineHandler {
@Override
public Expression getTenantId() {
// 判断当前用户是否为超级管理员
if (isSuperAdmin()) {
// 超级管理员返回Null,不加租户条件
return new NullValue();
}
// 普通用户返回租户ID
return new StringValue(TenantContextHolder.getTenant());
}
private boolean isSuperAdmin() {
// 你可以通过ThreadLocal、SecurityContext、LoginUserContextHolder等获取当前用户角色
// 例如:
LoginAppUser user = LoginUserContextHolder.getUser();
return user != null && user.getUsername().equals("admin");
}
}