Commit 4d53944771c66b563da1e3d68d3ba43405af8a06
1 parent
2598c033
【新增】新增 `@TenantIgnore` 注解,标记指定方法,忽略多租户的自动过滤,适合实现跨租户的逻辑
Showing
20 changed files
with
195 additions
and
215 deletions
yudao-framework/yudao-spring-boot-starter-biz-tenant/src/main/java/cn/iocoder/yudao/framework/tenant/config/YudaoTenantAutoConfiguration.java
0 → 100644
| 1 | +package cn.iocoder.yudao.framework.tenant.config; | |
| 2 | + | |
| 3 | +import cn.hutool.core.annotation.AnnotationUtil; | |
| 4 | +import cn.iocoder.yudao.framework.common.enums.WebFilterOrderEnum; | |
| 5 | +import cn.iocoder.yudao.framework.mybatis.core.util.MyBatisUtils; | |
| 6 | +import cn.iocoder.yudao.framework.quartz.core.handler.JobHandler; | |
| 7 | +import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnoreAspect; | |
| 8 | +import cn.iocoder.yudao.framework.tenant.core.db.TenantDatabaseInterceptor; | |
| 9 | +import cn.iocoder.yudao.framework.tenant.core.job.TenantJob; | |
| 10 | +import cn.iocoder.yudao.framework.tenant.core.job.TenantJobHandlerDecorator; | |
| 11 | +import cn.iocoder.yudao.framework.tenant.core.mq.TenantRedisMessageInterceptor; | |
| 12 | +import cn.iocoder.yudao.framework.tenant.core.security.TenantSecurityWebFilter; | |
| 13 | +import cn.iocoder.yudao.framework.tenant.core.service.TenantFrameworkService; | |
| 14 | +import cn.iocoder.yudao.framework.tenant.core.web.TenantContextWebFilter; | |
| 15 | +import cn.iocoder.yudao.framework.web.config.WebProperties; | |
| 16 | +import cn.iocoder.yudao.framework.web.core.handler.GlobalExceptionHandler; | |
| 17 | +import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor; | |
| 18 | +import com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor; | |
| 19 | +import org.springframework.beans.BeansException; | |
| 20 | +import org.springframework.beans.factory.config.BeanPostProcessor; | |
| 21 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | |
| 22 | +import org.springframework.boot.context.properties.EnableConfigurationProperties; | |
| 23 | +import org.springframework.boot.web.servlet.FilterRegistrationBean; | |
| 24 | +import org.springframework.context.annotation.Bean; | |
| 25 | +import org.springframework.context.annotation.Configuration; | |
| 26 | + | |
| 27 | +@Configuration | |
| 28 | +@ConditionalOnProperty(prefix = "yudao.tenant", value = "enable", matchIfMissing = true) // 允许使用 yudao.tenant.enable=false 禁用多租户 | |
| 29 | +@EnableConfigurationProperties(TenantProperties.class) | |
| 30 | +public class YudaoTenantAutoConfiguration { | |
| 31 | + | |
| 32 | + // ========== AOP ========== | |
| 33 | + | |
| 34 | + @Bean | |
| 35 | + public TenantIgnoreAspect tenantIgnoreAspect() { | |
| 36 | + return new TenantIgnoreAspect(); | |
| 37 | + } | |
| 38 | + | |
| 39 | + // ========== DB ========== | |
| 40 | + | |
| 41 | + @Bean | |
| 42 | + public TenantLineInnerInterceptor tenantLineInnerInterceptor(TenantProperties properties, | |
| 43 | + MybatisPlusInterceptor interceptor) { | |
| 44 | + TenantLineInnerInterceptor inner = new TenantLineInnerInterceptor(new TenantDatabaseInterceptor(properties)); | |
| 45 | + // 添加到 interceptor 中 | |
| 46 | + // 需要加在首个,主要是为了在分页插件前面。这个是 MyBatis Plus 的规定 | |
| 47 | + MyBatisUtils.addInterceptor(interceptor, inner, 0); | |
| 48 | + return inner; | |
| 49 | + } | |
| 50 | + | |
| 51 | + // ========== WEB ========== | |
| 52 | + | |
| 53 | + @Bean | |
| 54 | + public FilterRegistrationBean<TenantContextWebFilter> tenantContextWebFilter() { | |
| 55 | + FilterRegistrationBean<TenantContextWebFilter> registrationBean = new FilterRegistrationBean<>(); | |
| 56 | + registrationBean.setFilter(new TenantContextWebFilter()); | |
| 57 | + registrationBean.setOrder(WebFilterOrderEnum.TENANT_CONTEXT_FILTER); | |
| 58 | + return registrationBean; | |
| 59 | + } | |
| 60 | + | |
| 61 | + // ========== Security ========== | |
| 62 | + | |
| 63 | + @Bean | |
| 64 | + public FilterRegistrationBean<TenantSecurityWebFilter> tenantSecurityWebFilter(TenantProperties tenantProperties, | |
| 65 | + WebProperties webProperties, | |
| 66 | + GlobalExceptionHandler globalExceptionHandler, | |
| 67 | + TenantFrameworkService tenantFrameworkService) { | |
| 68 | + FilterRegistrationBean<TenantSecurityWebFilter> registrationBean = new FilterRegistrationBean<>(); | |
| 69 | + registrationBean.setFilter(new TenantSecurityWebFilter(tenantProperties, webProperties, | |
| 70 | + globalExceptionHandler, tenantFrameworkService)); | |
| 71 | + registrationBean.setOrder(WebFilterOrderEnum.TENANT_SECURITY_FILTER); | |
| 72 | + return registrationBean; | |
| 73 | + } | |
| 74 | + | |
| 75 | + // ========== MQ ========== | |
| 76 | + | |
| 77 | + @Bean | |
| 78 | + public TenantRedisMessageInterceptor tenantRedisMessageInterceptor() { | |
| 79 | + return new TenantRedisMessageInterceptor(); | |
| 80 | + } | |
| 81 | + | |
| 82 | + // ========== Job ========== | |
| 83 | + | |
| 84 | + @Bean | |
| 85 | + @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection") | |
| 86 | + public BeanPostProcessor jobHandlerBeanPostProcessor(TenantFrameworkService tenantFrameworkService) { | |
| 87 | + return new BeanPostProcessor() { | |
| 88 | + | |
| 89 | + @Override | |
| 90 | + public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { | |
| 91 | + if (!(bean instanceof JobHandler)) { | |
| 92 | + return bean; | |
| 93 | + } | |
| 94 | + // 有 TenantJob 注解的情况下,才会进行处理 | |
| 95 | + if (!AnnotationUtil.hasAnnotation(bean.getClass(), TenantJob.class)) { | |
| 96 | + return bean; | |
| 97 | + } | |
| 98 | + | |
| 99 | + // 使用 TenantJobHandlerDecorator 装饰 | |
| 100 | + return new TenantJobHandlerDecorator(tenantFrameworkService, (JobHandler) bean); | |
| 101 | + } | |
| 102 | + | |
| 103 | + }; | |
| 104 | + } | |
| 105 | + | |
| 106 | +} | ... | ... |
yudao-framework/yudao-spring-boot-starter-biz-tenant/src/main/java/cn/iocoder/yudao/framework/tenant/config/YudaoTenantDatabaseAutoConfiguration.java deleted
| 1 | -package cn.iocoder.yudao.framework.tenant.config; | |
| 2 | - | |
| 3 | -import cn.iocoder.yudao.framework.mybatis.core.util.MyBatisUtils; | |
| 4 | -import cn.iocoder.yudao.framework.tenant.core.db.TenantDatabaseInterceptor; | |
| 5 | -import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor; | |
| 6 | -import com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor; | |
| 7 | -import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | |
| 8 | -import org.springframework.boot.context.properties.EnableConfigurationProperties; | |
| 9 | -import org.springframework.context.annotation.Bean; | |
| 10 | -import org.springframework.context.annotation.Configuration; | |
| 11 | - | |
| 12 | -/** | |
| 13 | - * 多租户针对 DB 的自动配置 | |
| 14 | - * | |
| 15 | - * @author 芋道源码 | |
| 16 | - */ | |
| 17 | -@Configuration | |
| 18 | -// 允许使用 yudao.tenant.enable=false 禁用多租户 | |
| 19 | -@ConditionalOnProperty(prefix = "yudao.tenant", value = "enable", matchIfMissing = true) | |
| 20 | -@EnableConfigurationProperties(TenantProperties.class) | |
| 21 | -public class YudaoTenantDatabaseAutoConfiguration { | |
| 22 | - | |
| 23 | - @Bean | |
| 24 | - public TenantLineInnerInterceptor tenantLineInnerInterceptor(TenantProperties properties, | |
| 25 | - MybatisPlusInterceptor interceptor) { | |
| 26 | - TenantLineInnerInterceptor inner = new TenantLineInnerInterceptor(new TenantDatabaseInterceptor(properties)); | |
| 27 | - // 添加到 interceptor 中 | |
| 28 | - // 需要加在首个,主要是为了在分页插件前面。这个是 MyBatis Plus 的规定 | |
| 29 | - MyBatisUtils.addInterceptor(interceptor, inner, 0); | |
| 30 | - return inner; | |
| 31 | - } | |
| 32 | - | |
| 33 | -} |
yudao-framework/yudao-spring-boot-starter-biz-tenant/src/main/java/cn/iocoder/yudao/framework/tenant/config/YudaoTenantJobAutoConfiguration.java deleted
| 1 | -package cn.iocoder.yudao.framework.tenant.config; | |
| 2 | - | |
| 3 | -import cn.hutool.core.annotation.AnnotationUtil; | |
| 4 | -import cn.iocoder.yudao.framework.quartz.core.handler.JobHandler; | |
| 5 | -import cn.iocoder.yudao.framework.tenant.core.job.TenantJob; | |
| 6 | -import cn.iocoder.yudao.framework.tenant.core.job.TenantJobHandlerDecorator; | |
| 7 | -import cn.iocoder.yudao.framework.tenant.core.service.TenantFrameworkService; | |
| 8 | -import org.springframework.beans.BeansException; | |
| 9 | -import org.springframework.beans.factory.config.BeanPostProcessor; | |
| 10 | -import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | |
| 11 | -import org.springframework.context.annotation.Bean; | |
| 12 | -import org.springframework.context.annotation.Configuration; | |
| 13 | - | |
| 14 | -/** | |
| 15 | - * 多租户针对 Job 的自动配置 | |
| 16 | - * | |
| 17 | - * @author 芋道源码 | |
| 18 | - */ | |
| 19 | -@Configuration | |
| 20 | -// 允许使用 yudao.tenant.enable=false 禁用多租户 | |
| 21 | -@ConditionalOnProperty(prefix = "yudao.tenant", value = "enable", matchIfMissing = true) | |
| 22 | -public class YudaoTenantJobAutoConfiguration { | |
| 23 | - | |
| 24 | - @Bean | |
| 25 | - @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection") | |
| 26 | - public BeanPostProcessor jobHandlerBeanPostProcessor(TenantFrameworkService tenantFrameworkService) { | |
| 27 | - return new BeanPostProcessor() { | |
| 28 | - | |
| 29 | - @Override | |
| 30 | - public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { | |
| 31 | - if (!(bean instanceof JobHandler)) { | |
| 32 | - return bean; | |
| 33 | - } | |
| 34 | - // 有 TenantJob 注解的情况下,才会进行处理 | |
| 35 | - if (!AnnotationUtil.hasAnnotation(bean.getClass(), TenantJob.class)) { | |
| 36 | - return bean; | |
| 37 | - } | |
| 38 | - | |
| 39 | - // 使用 TenantJobHandlerDecorator 装饰 | |
| 40 | - return new TenantJobHandlerDecorator(tenantFrameworkService, (JobHandler) bean); | |
| 41 | - } | |
| 42 | - | |
| 43 | - }; | |
| 44 | - } | |
| 45 | - | |
| 46 | -} |
yudao-framework/yudao-spring-boot-starter-biz-tenant/src/main/java/cn/iocoder/yudao/framework/tenant/config/YudaoTenantMQAutoConfiguration.java deleted
| 1 | -package cn.iocoder.yudao.framework.tenant.config; | |
| 2 | - | |
| 3 | -import cn.iocoder.yudao.framework.tenant.core.mq.TenantRedisMessageInterceptor; | |
| 4 | -import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | |
| 5 | -import org.springframework.context.annotation.Bean; | |
| 6 | -import org.springframework.context.annotation.Configuration; | |
| 7 | - | |
| 8 | -/** | |
| 9 | - * 多租户针对 MQ 的自动配置 | |
| 10 | - * | |
| 11 | - * @author 芋道源码 | |
| 12 | - */ | |
| 13 | -@Configuration | |
| 14 | -// 允许使用 yudao.tenant.enable=false 禁用多租户 | |
| 15 | -@ConditionalOnProperty(prefix = "yudao.tenant", value = "enable", matchIfMissing = true) | |
| 16 | -public class YudaoTenantMQAutoConfiguration { | |
| 17 | - | |
| 18 | - @Bean | |
| 19 | - public TenantRedisMessageInterceptor tenantRedisMessageInterceptor() { | |
| 20 | - return new TenantRedisMessageInterceptor(); | |
| 21 | - } | |
| 22 | - | |
| 23 | -} |
yudao-framework/yudao-spring-boot-starter-biz-tenant/src/main/java/cn/iocoder/yudao/framework/tenant/config/YudaoTenantSecurityAutoConfiguration.java deleted
| 1 | -package cn.iocoder.yudao.framework.tenant.config; | |
| 2 | - | |
| 3 | -import cn.iocoder.yudao.framework.common.enums.WebFilterOrderEnum; | |
| 4 | -import cn.iocoder.yudao.framework.tenant.core.security.TenantSecurityWebFilter; | |
| 5 | -import cn.iocoder.yudao.framework.tenant.core.service.TenantFrameworkService; | |
| 6 | -import cn.iocoder.yudao.framework.web.config.WebProperties; | |
| 7 | -import cn.iocoder.yudao.framework.web.core.handler.GlobalExceptionHandler; | |
| 8 | -import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | |
| 9 | -import org.springframework.boot.context.properties.EnableConfigurationProperties; | |
| 10 | -import org.springframework.boot.web.servlet.FilterRegistrationBean; | |
| 11 | -import org.springframework.context.annotation.Bean; | |
| 12 | -import org.springframework.context.annotation.Configuration; | |
| 13 | - | |
| 14 | -/** | |
| 15 | - * 多租户针对 Web 的自动配置 | |
| 16 | - * | |
| 17 | - * @author 芋道源码 | |
| 18 | - */ | |
| 19 | -@Configuration | |
| 20 | -// 允许使用 yudao.tenant.enable=false 禁用多租户 | |
| 21 | -@ConditionalOnProperty(prefix = "yudao.tenant", value = "enable", matchIfMissing = true) | |
| 22 | -@EnableConfigurationProperties(TenantProperties.class) | |
| 23 | -public class YudaoTenantSecurityAutoConfiguration { | |
| 24 | - | |
| 25 | - @Bean | |
| 26 | - public FilterRegistrationBean<TenantSecurityWebFilter> tenantSecurityWebFilter(TenantProperties tenantProperties, | |
| 27 | - WebProperties webProperties, | |
| 28 | - GlobalExceptionHandler globalExceptionHandler, | |
| 29 | - TenantFrameworkService tenantFrameworkService) { | |
| 30 | - FilterRegistrationBean<TenantSecurityWebFilter> registrationBean = new FilterRegistrationBean<>(); | |
| 31 | - registrationBean.setFilter(new TenantSecurityWebFilter(tenantProperties, webProperties, | |
| 32 | - globalExceptionHandler, tenantFrameworkService)); | |
| 33 | - registrationBean.setOrder(WebFilterOrderEnum.TENANT_SECURITY_FILTER); | |
| 34 | - return registrationBean; | |
| 35 | - } | |
| 36 | - | |
| 37 | -} |
yudao-framework/yudao-spring-boot-starter-biz-tenant/src/main/java/cn/iocoder/yudao/framework/tenant/config/YudaoTenantWebAutoConfiguration.java deleted
| 1 | -package cn.iocoder.yudao.framework.tenant.config; | |
| 2 | - | |
| 3 | -import cn.iocoder.yudao.framework.common.enums.WebFilterOrderEnum; | |
| 4 | -import cn.iocoder.yudao.framework.tenant.core.web.TenantContextWebFilter; | |
| 5 | -import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | |
| 6 | -import org.springframework.boot.web.servlet.FilterRegistrationBean; | |
| 7 | -import org.springframework.context.annotation.Bean; | |
| 8 | -import org.springframework.context.annotation.Configuration; | |
| 9 | - | |
| 10 | -/** | |
| 11 | - * 多租户针对 Web 的自动配置 | |
| 12 | - * | |
| 13 | - * @author 芋道源码 | |
| 14 | - */ | |
| 15 | -@Configuration | |
| 16 | -// 允许使用 yudao.tenant.enable=false 禁用多租户 | |
| 17 | -@ConditionalOnProperty(prefix = "yudao.tenant", value = "enable", matchIfMissing = true) | |
| 18 | -public class YudaoTenantWebAutoConfiguration { | |
| 19 | - | |
| 20 | - @Bean | |
| 21 | - public FilterRegistrationBean<TenantContextWebFilter> tenantContextWebFilter() { | |
| 22 | - FilterRegistrationBean<TenantContextWebFilter> registrationBean = new FilterRegistrationBean<>(); | |
| 23 | - registrationBean.setFilter(new TenantContextWebFilter()); | |
| 24 | - registrationBean.setOrder(WebFilterOrderEnum.TENANT_CONTEXT_FILTER); | |
| 25 | - return registrationBean; | |
| 26 | - } | |
| 27 | - | |
| 28 | -} |
yudao-framework/yudao-spring-boot-starter-biz-tenant/src/main/java/cn/iocoder/yudao/framework/tenant/core/aop/TenantIgnore.java
0 → 100644
| 1 | +package cn.iocoder.yudao.framework.tenant.core.aop; | |
| 2 | + | |
| 3 | +import java.lang.annotation.*; | |
| 4 | + | |
| 5 | +/** | |
| 6 | + * 忽略租户,标记指定方法不进行租户的自动过滤 | |
| 7 | + * | |
| 8 | + * 注意,只有 DB 的场景会过滤,其它场景暂时不过滤: | |
| 9 | + * 1、Redis 场景:因为是基于 Key 实现多租户的能力,所以忽略没有意义,不像 DB 是一个 column 实现的 | |
| 10 | + * 2、MQ 场景:有点难以抉择,目前可以通过 Consumer 手动在消费的方法上,添加 @TenantIgnore 进行忽略 | |
| 11 | + * | |
| 12 | + * @author 芋道源码 | |
| 13 | + */ | |
| 14 | +@Target({ElementType.METHOD}) | |
| 15 | +@Retention(RetentionPolicy.RUNTIME) | |
| 16 | +@Inherited | |
| 17 | +public @interface TenantIgnore { | |
| 18 | +} | ... | ... |
yudao-framework/yudao-spring-boot-starter-biz-tenant/src/main/java/cn/iocoder/yudao/framework/tenant/core/aop/TenantIgnoreAspect.java
0 → 100644
| 1 | +package cn.iocoder.yudao.framework.tenant.core.aop; | |
| 2 | + | |
| 3 | +import cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder; | |
| 4 | +import lombok.extern.slf4j.Slf4j; | |
| 5 | +import org.aspectj.lang.ProceedingJoinPoint; | |
| 6 | +import org.aspectj.lang.annotation.Around; | |
| 7 | +import org.aspectj.lang.annotation.Aspect; | |
| 8 | + | |
| 9 | +/** | |
| 10 | + * 忽略多租户的 Aspect,基于 {@link TenantIgnore} 注解实现,用于一些全局的逻辑。 | |
| 11 | + * 例如说,一个定时任务,读取所有数据,进行处理。 | |
| 12 | + * 又例如说,读取所有数据,进行缓存。 | |
| 13 | + * | |
| 14 | + * @author 芋道源码 | |
| 15 | + */ | |
| 16 | +@Aspect | |
| 17 | +@Slf4j | |
| 18 | +public class TenantIgnoreAspect { | |
| 19 | + | |
| 20 | + @Around("@annotation(tenantIgnore)") | |
| 21 | + public Object around(ProceedingJoinPoint joinPoint, TenantIgnore tenantIgnore) throws Throwable { | |
| 22 | + Boolean oldIgnore = TenantContextHolder.isIgnore(); | |
| 23 | + try { | |
| 24 | + TenantContextHolder.setIgnore(true); | |
| 25 | + // 执行逻辑 | |
| 26 | + return joinPoint.proceed(); | |
| 27 | + } finally { | |
| 28 | + TenantContextHolder.setIgnore(oldIgnore); | |
| 29 | + } | |
| 30 | + } | |
| 31 | + | |
| 32 | +} | ... | ... |
yudao-framework/yudao-spring-boot-starter-biz-tenant/src/main/java/cn/iocoder/yudao/framework/tenant/core/context/TenantContextHolder.java
| ... | ... | @@ -9,9 +9,17 @@ import com.alibaba.ttl.TransmittableThreadLocal; |
| 9 | 9 | */ |
| 10 | 10 | public class TenantContextHolder { |
| 11 | 11 | |
| 12 | + /** | |
| 13 | + * 当前租户编号 | |
| 14 | + */ | |
| 12 | 15 | private static final ThreadLocal<Long> TENANT_ID = new TransmittableThreadLocal<>(); |
| 13 | 16 | |
| 14 | 17 | /** |
| 18 | + * 是否忽略租户 | |
| 19 | + */ | |
| 20 | + private static final ThreadLocal<Boolean> IGNORE = new TransmittableThreadLocal<>(); | |
| 21 | + | |
| 22 | + /** | |
| 15 | 23 | * 获得租户编号。 |
| 16 | 24 | * |
| 17 | 25 | * @return 租户编号 |
| ... | ... | @@ -37,8 +45,22 @@ public class TenantContextHolder { |
| 37 | 45 | TENANT_ID.set(tenantId); |
| 38 | 46 | } |
| 39 | 47 | |
| 48 | + public static void setIgnore(Boolean ignore) { | |
| 49 | + IGNORE.set(ignore); | |
| 50 | + } | |
| 51 | + | |
| 52 | + /** | |
| 53 | + * 当前是否忽略租户 | |
| 54 | + * | |
| 55 | + * @return 是否忽略 | |
| 56 | + */ | |
| 57 | + public static boolean isIgnore() { | |
| 58 | + return Boolean.TRUE.equals(IGNORE.get()); | |
| 59 | + } | |
| 60 | + | |
| 40 | 61 | public static void clear() { |
| 41 | 62 | TENANT_ID.remove(); |
| 63 | + IGNORE.remove(); | |
| 42 | 64 | } |
| 43 | 65 | |
| 44 | 66 | } | ... | ... |
yudao-framework/yudao-spring-boot-starter-biz-tenant/src/main/java/cn/iocoder/yudao/framework/tenant/core/db/TenantDatabaseInterceptor.java
| ... | ... | @@ -6,7 +6,7 @@ import cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder; |
| 6 | 6 | import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler; |
| 7 | 7 | import lombok.AllArgsConstructor; |
| 8 | 8 | import net.sf.jsqlparser.expression.Expression; |
| 9 | -import net.sf.jsqlparser.expression.StringValue; | |
| 9 | +import net.sf.jsqlparser.expression.LongValue; | |
| 10 | 10 | |
| 11 | 11 | /** |
| 12 | 12 | * 基于 MyBatis Plus 多租户的功能,实现 DB 层面的多租户的功能 |
| ... | ... | @@ -20,12 +20,13 @@ public class TenantDatabaseInterceptor implements TenantLineHandler { |
| 20 | 20 | |
| 21 | 21 | @Override |
| 22 | 22 | public Expression getTenantId() { |
| 23 | - return new StringValue(TenantContextHolder.getRequiredTenantId().toString()); | |
| 23 | + return new LongValue( TenantContextHolder.getRequiredTenantId()); | |
| 24 | 24 | } |
| 25 | 25 | |
| 26 | 26 | @Override |
| 27 | 27 | public boolean ignoreTable(String tableName) { |
| 28 | - return CollUtil.contains(properties.getIgnoreTables(), tableName); | |
| 28 | + return TenantContextHolder.isIgnore() // 情况一,全局忽略多租户 | |
| 29 | + || CollUtil.contains(properties.getIgnoreTables(), tableName); // 情况二,忽略多租户的表 | |
| 29 | 30 | } |
| 30 | 31 | |
| 31 | 32 | } | ... | ... |
yudao-framework/yudao-spring-boot-starter-biz-tenant/src/main/java/cn/iocoder/yudao/framework/tenant/core/job/TenantJobHandlerInvoker.java deleted
| 1 | -package cn.iocoder.yudao.framework.tenant.core.job; | |
| 2 | - | |
| 3 | -import cn.iocoder.yudao.framework.quartz.core.handler.JobHandlerInvoker; | |
| 4 | - | |
| 5 | -/** | |
| 6 | - * 多租户 JobHandlerInvoker 拓展实现类 | |
| 7 | - * | |
| 8 | - * @author 芋道源码 | |
| 9 | - */ | |
| 10 | -public class TenantJobHandlerInvoker extends JobHandlerInvoker { | |
| 11 | - | |
| 12 | - | |
| 13 | - | |
| 14 | -} |
yudao-framework/yudao-spring-boot-starter-biz-tenant/src/main/java/cn/iocoder/yudao/framework/tenant/core/util/TenantUtils.java
| ... | ... | @@ -12,17 +12,23 @@ public class TenantUtils { |
| 12 | 12 | /** |
| 13 | 13 | * 使用指定租户,执行对应的逻辑 |
| 14 | 14 | * |
| 15 | + * 注意,如果当前是忽略租户的情况下,会被强制设置成不忽略租户 | |
| 16 | + * 当然,执行完成后,还是会恢复回去 | |
| 17 | + * | |
| 15 | 18 | * @param tenantId 租户编号 |
| 16 | 19 | * @param runnable 逻辑 |
| 17 | 20 | */ |
| 18 | 21 | public static void execute(Long tenantId, Runnable runnable) { |
| 19 | 22 | Long oldTenantId = TenantContextHolder.getTenantId(); |
| 23 | + Boolean oldIgnore = TenantContextHolder.isIgnore(); | |
| 20 | 24 | try { |
| 21 | 25 | TenantContextHolder.setTenantId(tenantId); |
| 26 | + TenantContextHolder.setIgnore(false); | |
| 22 | 27 | // 执行逻辑 |
| 23 | 28 | runnable.run(); |
| 24 | 29 | } finally { |
| 25 | 30 | TenantContextHolder.setTenantId(oldTenantId); |
| 31 | + TenantContextHolder.setIgnore(oldIgnore); | |
| 26 | 32 | } |
| 27 | 33 | } |
| 28 | 34 | ... | ... |
yudao-framework/yudao-spring-boot-starter-biz-tenant/src/main/resources/META-INF/spring.factories
| 1 | 1 | org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ |
| 2 | - cn.iocoder.yudao.framework.tenant.config.YudaoTenantDatabaseAutoConfiguration,\ | |
| 3 | - cn.iocoder.yudao.framework.tenant.config.YudaoTenantWebAutoConfiguration,\ | |
| 4 | - cn.iocoder.yudao.framework.tenant.config.YudaoTenantJobAutoConfiguration,\ | |
| 5 | - cn.iocoder.yudao.framework.tenant.config.YudaoTenantMQAutoConfiguration,\ | |
| 6 | - cn.iocoder.yudao.framework.tenant.config.YudaoTenantSecurityAutoConfiguration | |
| 2 | + cn.iocoder.yudao.framework.tenant.config.YudaoTenantAutoConfiguration | ... | ... |
yudao-module-infra/yudao-module-infra-impl/src/main/java/cn/iocoder/yudao/module/infra/dal/mysql/file/FileMapper.java
| ... | ... | @@ -5,7 +5,6 @@ import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX; |
| 5 | 5 | import cn.iocoder.yudao.framework.mybatis.core.query.QueryWrapperX; |
| 6 | 6 | import cn.iocoder.yudao.module.infra.controller.admin.file.vo.FilePageReqVO; |
| 7 | 7 | import cn.iocoder.yudao.module.infra.dal.dataobject.file.FileDO; |
| 8 | -import com.baomidou.mybatisplus.annotation.InterceptorIgnore; | |
| 9 | 8 | import org.apache.ibatis.annotations.Mapper; |
| 10 | 9 | |
| 11 | 10 | /** |
| ... | ... | @@ -31,12 +30,10 @@ public interface FileMapper extends BaseMapperX<FileDO> { |
| 31 | 30 | /** |
| 32 | 31 | * 基于 Path 获取文件 |
| 33 | 32 | * 实际上,是基于 ID 查询 |
| 34 | - * 由于前端使用 <img /> 的方式获取图片,所以需要忽略租户的查询 | |
| 35 | 33 | * |
| 36 | 34 | * @param path 路径 |
| 37 | 35 | * @return 文件 |
| 38 | 36 | */ |
| 39 | - @InterceptorIgnore(tenantLine = "true") | |
| 40 | 37 | default FileDO selectByPath(String path) { |
| 41 | 38 | return selectById(path); |
| 42 | 39 | } | ... | ... |
yudao-module-pay/yudao-module-pay-impl/src/main/java/cn/iocoder/yudao/module/pay/dal/mysql/merchant/PayChannelMapper.java
| 1 | 1 | package cn.iocoder.yudao.module.pay.dal.mysql.merchant; |
| 2 | 2 | |
| 3 | 3 | import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| 4 | +import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX; | |
| 4 | 5 | import cn.iocoder.yudao.framework.mybatis.core.query.QueryWrapperX; |
| 5 | 6 | import cn.iocoder.yudao.module.pay.controller.admin.merchant.vo.channel.PayChannelExportReqVO; |
| 6 | 7 | import cn.iocoder.yudao.module.pay.controller.admin.merchant.vo.channel.PayChannelPageReqVO; |
| 7 | 8 | import cn.iocoder.yudao.module.pay.dal.dataobject.merchant.PayChannelDO; |
| 8 | -import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX; | |
| 9 | -import com.baomidou.mybatisplus.annotation.InterceptorIgnore; | |
| 10 | 9 | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| 11 | 10 | import org.apache.ibatis.annotations.Mapper; |
| 12 | 11 | import org.apache.ibatis.annotations.Select; |
| ... | ... | @@ -23,7 +22,6 @@ public interface PayChannelMapper extends BaseMapperX<PayChannelDO> { |
| 23 | 22 | } |
| 24 | 23 | |
| 25 | 24 | @Select("SELECT id FROM pay_channel WHERE update_time > #{maxUpdateTime} LIMIT 1") |
| 26 | - @InterceptorIgnore(tenantLine = "true") // 该方法忽略多租户。原因:该方法被异步 task 调用,此时获取不到租户编号 | |
| 27 | 25 | Long selectExistsByUpdateTimeAfter(Date maxUpdateTime); |
| 28 | 26 | |
| 29 | 27 | default PageResult<PayChannelDO> selectPage(PayChannelPageReqVO reqVO) { | ... | ... |
yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/dal/mysql/dept/DeptMapper.java
| ... | ... | @@ -4,7 +4,6 @@ import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX; |
| 4 | 4 | import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX; |
| 5 | 5 | import cn.iocoder.yudao.module.system.controller.admin.dept.vo.dept.DeptListReqVO; |
| 6 | 6 | import cn.iocoder.yudao.module.system.dal.dataobject.dept.DeptDO; |
| 7 | -import com.baomidou.mybatisplus.annotation.InterceptorIgnore; | |
| 8 | 7 | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| 9 | 8 | import org.apache.ibatis.annotations.Mapper; |
| 10 | 9 | import org.apache.ibatis.annotations.Select; |
| ... | ... | @@ -30,12 +29,6 @@ public interface DeptMapper extends BaseMapperX<DeptDO> { |
| 30 | 29 | } |
| 31 | 30 | |
| 32 | 31 | @Select("SELECT id FROM system_dept WHERE update_time > #{maxUpdateTime} LIMIT 1") |
| 33 | - @InterceptorIgnore(tenantLine = "true") // 该方法忽略多租户。原因:该方法被异步 task 调用,此时获取不到租户编号 | |
| 34 | 32 | Long selectExistsByUpdateTimeAfter(Date maxUpdateTime); |
| 35 | 33 | |
| 36 | - // TODO 芋艿:后续想想,有没可能优化下。大体思路,是支持某个方法,忽略租户 | |
| 37 | - @Select("SELECT * FROM system_dept") | |
| 38 | - @InterceptorIgnore(tenantLine = "true") | |
| 39 | - List<DeptDO> selectListIgnoreTenant(); | |
| 40 | - | |
| 41 | 34 | } | ... | ... |
yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/dal/mysql/permission/MenuMapper.java
| 1 | 1 | package cn.iocoder.yudao.module.system.dal.mysql.permission; |
| 2 | 2 | |
| 3 | 3 | import cn.iocoder.yudao.framework.mybatis.core.enums.SqlConstants; |
| 4 | -import cn.iocoder.yudao.module.system.controller.admin.permission.vo.menu.MenuListReqVO; | |
| 5 | -import cn.iocoder.yudao.module.system.dal.dataobject.permission.MenuDO; | |
| 6 | 4 | import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX; |
| 7 | 5 | import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX; |
| 8 | -import com.baomidou.mybatisplus.annotation.InterceptorIgnore; | |
| 6 | +import cn.iocoder.yudao.module.system.controller.admin.permission.vo.menu.MenuListReqVO; | |
| 7 | +import cn.iocoder.yudao.module.system.dal.dataobject.permission.MenuDO; | |
| 9 | 8 | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| 10 | 9 | import org.apache.ibatis.annotations.Mapper; |
| 11 | 10 | |
| ... | ... | @@ -29,7 +28,6 @@ public interface MenuMapper extends BaseMapperX<MenuDO> { |
| 29 | 28 | .eqIfPresent(MenuDO::getStatus, reqVO.getStatus())); |
| 30 | 29 | } |
| 31 | 30 | |
| 32 | - @InterceptorIgnore(tenantLine = "true") // 该方法忽略多租户。原因:该方法被异步 task 调用,此时获取不到租户编号 | |
| 33 | 31 | default boolean selectExistsByUpdateTimeAfter(Date maxUpdateTime) { |
| 34 | 32 | return selectOne(new LambdaQueryWrapper<MenuDO>().select(MenuDO::getId) |
| 35 | 33 | .gt(MenuDO::getUpdateTime, maxUpdateTime).last(SqlConstants.LIMIT1)) != null; | ... | ... |
yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/dal/mysql/permission/RoleMenuMapper.java
| ... | ... | @@ -2,7 +2,6 @@ package cn.iocoder.yudao.module.system.dal.mysql.permission; |
| 2 | 2 | |
| 3 | 3 | import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX; |
| 4 | 4 | import cn.iocoder.yudao.module.system.dal.dataobject.permission.RoleMenuDO; |
| 5 | -import com.baomidou.mybatisplus.annotation.InterceptorIgnore; | |
| 6 | 5 | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| 7 | 6 | import org.apache.ibatis.annotations.Mapper; |
| 8 | 7 | import org.apache.ibatis.annotations.Select; |
| ... | ... | @@ -44,7 +43,6 @@ public interface RoleMenuMapper extends BaseMapperX<RoleMenuDO> { |
| 44 | 43 | } |
| 45 | 44 | |
| 46 | 45 | @Select("SELECT id FROM system_role_menu WHERE update_time > #{maxUpdateTime} LIMIT 1") |
| 47 | - @InterceptorIgnore(tenantLine = "true") // 该方法忽略多租户。原因:该方法被异步 task 调用,此时获取不到租户编号 | |
| 48 | 46 | Long selectExistsByUpdateTimeAfter(Date maxUpdateTime); |
| 49 | 47 | |
| 50 | 48 | } | ... | ... |
yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/dal/mysql/sms/SmsChannelMapper.java
| 1 | 1 | package cn.iocoder.yudao.module.system.dal.mysql.sms; |
| 2 | 2 | |
| 3 | -import cn.iocoder.yudao.module.system.controller.admin.sms.vo.channel.SmsChannelPageReqVO; | |
| 4 | -import cn.iocoder.yudao.module.system.dal.dataobject.sms.SmsChannelDO; | |
| 5 | 3 | import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| 6 | 4 | import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX; |
| 7 | 5 | import cn.iocoder.yudao.framework.mybatis.core.query.QueryWrapperX; |
| 8 | -import com.baomidou.mybatisplus.annotation.InterceptorIgnore; | |
| 6 | +import cn.iocoder.yudao.module.system.controller.admin.sms.vo.channel.SmsChannelPageReqVO; | |
| 7 | +import cn.iocoder.yudao.module.system.dal.dataobject.sms.SmsChannelDO; | |
| 9 | 8 | import org.apache.ibatis.annotations.Mapper; |
| 10 | 9 | import org.apache.ibatis.annotations.Select; |
| 11 | 10 | |
| ... | ... | @@ -23,7 +22,6 @@ public interface SmsChannelMapper extends BaseMapperX<SmsChannelDO> { |
| 23 | 22 | } |
| 24 | 23 | |
| 25 | 24 | @Select("SELECT id FROM system_sms_channel WHERE update_time > #{maxUpdateTime} LIMIT 1") |
| 26 | - @InterceptorIgnore(tenantLine = "true") // 该方法忽略多租户。原因:该方法被异步 task 调用,此时获取不到租户编号 | |
| 27 | 25 | Long selectExistsByUpdateTimeAfter(Date maxUpdateTime); |
| 28 | 26 | |
| 29 | 27 | } | ... | ... |
yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/dal/mysql/sms/SmsTemplateMapper.java
| 1 | 1 | package cn.iocoder.yudao.module.system.dal.mysql.sms; |
| 2 | 2 | |
| 3 | 3 | import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| 4 | +import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX; | |
| 4 | 5 | import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX; |
| 5 | 6 | import cn.iocoder.yudao.module.system.controller.admin.sms.vo.template.SmsTemplateExportReqVO; |
| 6 | 7 | import cn.iocoder.yudao.module.system.controller.admin.sms.vo.template.SmsTemplatePageReqVO; |
| 7 | 8 | import cn.iocoder.yudao.module.system.dal.dataobject.sms.SmsTemplateDO; |
| 8 | -import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX; | |
| 9 | -import com.baomidou.mybatisplus.annotation.InterceptorIgnore; | |
| 10 | 9 | import org.apache.ibatis.annotations.Mapper; |
| 11 | 10 | import org.apache.ibatis.annotations.Select; |
| 12 | 11 | |
| ... | ... | @@ -17,7 +16,6 @@ import java.util.List; |
| 17 | 16 | public interface SmsTemplateMapper extends BaseMapperX<SmsTemplateDO> { |
| 18 | 17 | |
| 19 | 18 | @Select("SELECT id FROM system_sms_template WHERE update_time > #{maxUpdateTime} LIMIT 1") |
| 20 | - @InterceptorIgnore(tenantLine = "true") // 该方法忽略多租户。原因:该方法被异步 task 调用,此时获取不到租户编号 | |
| 21 | 19 | Long selectExistsByUpdateTimeAfter(Date maxUpdateTime); |
| 22 | 20 | |
| 23 | 21 | default SmsTemplateDO selectByCode(String code) { | ... | ... |