package com.iailab.framework.datapermission.config; import cn.hutool.extra.spring.SpringUtil; import com.iailab.framework.datapermission.core.rule.dept.DeptDataPermissionRule; import com.iailab.framework.datapermission.core.rule.dept.DeptDataPermissionRuleCustomizer; import com.iailab.framework.security.core.LoginUser; import com.iailab.module.system.api.permission.PermissionApi; import org.springframework.boot.autoconfigure.AutoConfiguration; import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.context.annotation.Bean; import java.util.List; /** * 基于部门的数据权限 AutoConfiguration * * @author iailab */ @AutoConfiguration @ConditionalOnClass(LoginUser.class) @ConditionalOnBean(value = DeptDataPermissionRuleCustomizer.class) public class IailabDeptDataPermissionAutoConfiguration { @Bean public DeptDataPermissionRule deptDataPermissionRule(PermissionApi permissionApi, List customizers) { // Cloud 专属逻辑:优先使用本地的 PermissionApi 实现类,而不是 Feign 调用 // 原因:在创建租户时,租户还没创建好,导致 Feign 调用获取数据权限时,报“租户不存在”的错误 try { PermissionApi permissionApiImpl = SpringUtil.getBean("permissionApiImpl", PermissionApi.class); if (permissionApiImpl != null) { permissionApi = permissionApiImpl; } } catch (Exception ignored) {} // 创建 DeptDataPermissionRule 对象 DeptDataPermissionRule rule = new DeptDataPermissionRule(permissionApi); // 补全表配置 customizers.forEach(customizer -> customizer.customize(rule)); return rule; } }