【新增】新增 `@TenantIgnore` 注解,标记指定方法,忽略多租户的自动过滤,适合实现跨租户的逻辑
parent
2598c033a9
commit
4d53944771
@ -0,0 +1,106 @@
|
|||||||
|
package cn.iocoder.yudao.framework.tenant.config;
|
||||||
|
|
||||||
|
import cn.hutool.core.annotation.AnnotationUtil;
|
||||||
|
import cn.iocoder.yudao.framework.common.enums.WebFilterOrderEnum;
|
||||||
|
import cn.iocoder.yudao.framework.mybatis.core.util.MyBatisUtils;
|
||||||
|
import cn.iocoder.yudao.framework.quartz.core.handler.JobHandler;
|
||||||
|
import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnoreAspect;
|
||||||
|
import cn.iocoder.yudao.framework.tenant.core.db.TenantDatabaseInterceptor;
|
||||||
|
import cn.iocoder.yudao.framework.tenant.core.job.TenantJob;
|
||||||
|
import cn.iocoder.yudao.framework.tenant.core.job.TenantJobHandlerDecorator;
|
||||||
|
import cn.iocoder.yudao.framework.tenant.core.mq.TenantRedisMessageInterceptor;
|
||||||
|
import cn.iocoder.yudao.framework.tenant.core.security.TenantSecurityWebFilter;
|
||||||
|
import cn.iocoder.yudao.framework.tenant.core.service.TenantFrameworkService;
|
||||||
|
import cn.iocoder.yudao.framework.tenant.core.web.TenantContextWebFilter;
|
||||||
|
import cn.iocoder.yudao.framework.web.config.WebProperties;
|
||||||
|
import cn.iocoder.yudao.framework.web.core.handler.GlobalExceptionHandler;
|
||||||
|
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
|
||||||
|
import com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor;
|
||||||
|
import org.springframework.beans.BeansException;
|
||||||
|
import org.springframework.beans.factory.config.BeanPostProcessor;
|
||||||
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||||
|
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||||
|
import org.springframework.boot.web.servlet.FilterRegistrationBean;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
@ConditionalOnProperty(prefix = "yudao.tenant", value = "enable", matchIfMissing = true) // 允许使用 yudao.tenant.enable=false 禁用多租户
|
||||||
|
@EnableConfigurationProperties(TenantProperties.class)
|
||||||
|
public class YudaoTenantAutoConfiguration {
|
||||||
|
|
||||||
|
// ========== AOP ==========
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public TenantIgnoreAspect tenantIgnoreAspect() {
|
||||||
|
return new TenantIgnoreAspect();
|
||||||
|
}
|
||||||
|
|
||||||
|
// ========== DB ==========
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public TenantLineInnerInterceptor tenantLineInnerInterceptor(TenantProperties properties,
|
||||||
|
MybatisPlusInterceptor interceptor) {
|
||||||
|
TenantLineInnerInterceptor inner = new TenantLineInnerInterceptor(new TenantDatabaseInterceptor(properties));
|
||||||
|
// 添加到 interceptor 中
|
||||||
|
// 需要加在首个,主要是为了在分页插件前面。这个是 MyBatis Plus 的规定
|
||||||
|
MyBatisUtils.addInterceptor(interceptor, inner, 0);
|
||||||
|
return inner;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ========== WEB ==========
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public FilterRegistrationBean<TenantContextWebFilter> tenantContextWebFilter() {
|
||||||
|
FilterRegistrationBean<TenantContextWebFilter> registrationBean = new FilterRegistrationBean<>();
|
||||||
|
registrationBean.setFilter(new TenantContextWebFilter());
|
||||||
|
registrationBean.setOrder(WebFilterOrderEnum.TENANT_CONTEXT_FILTER);
|
||||||
|
return registrationBean;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ========== Security ==========
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public FilterRegistrationBean<TenantSecurityWebFilter> tenantSecurityWebFilter(TenantProperties tenantProperties,
|
||||||
|
WebProperties webProperties,
|
||||||
|
GlobalExceptionHandler globalExceptionHandler,
|
||||||
|
TenantFrameworkService tenantFrameworkService) {
|
||||||
|
FilterRegistrationBean<TenantSecurityWebFilter> registrationBean = new FilterRegistrationBean<>();
|
||||||
|
registrationBean.setFilter(new TenantSecurityWebFilter(tenantProperties, webProperties,
|
||||||
|
globalExceptionHandler, tenantFrameworkService));
|
||||||
|
registrationBean.setOrder(WebFilterOrderEnum.TENANT_SECURITY_FILTER);
|
||||||
|
return registrationBean;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ========== MQ ==========
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public TenantRedisMessageInterceptor tenantRedisMessageInterceptor() {
|
||||||
|
return new TenantRedisMessageInterceptor();
|
||||||
|
}
|
||||||
|
|
||||||
|
// ========== Job ==========
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
|
||||||
|
public BeanPostProcessor jobHandlerBeanPostProcessor(TenantFrameworkService tenantFrameworkService) {
|
||||||
|
return new BeanPostProcessor() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
|
||||||
|
if (!(bean instanceof JobHandler)) {
|
||||||
|
return bean;
|
||||||
|
}
|
||||||
|
// 有 TenantJob 注解的情况下,才会进行处理
|
||||||
|
if (!AnnotationUtil.hasAnnotation(bean.getClass(), TenantJob.class)) {
|
||||||
|
return bean;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 使用 TenantJobHandlerDecorator 装饰
|
||||||
|
return new TenantJobHandlerDecorator(tenantFrameworkService, (JobHandler) bean);
|
||||||
|
}
|
||||||
|
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -1,33 +0,0 @@
|
|||||||
package cn.iocoder.yudao.framework.tenant.config;
|
|
||||||
|
|
||||||
import cn.iocoder.yudao.framework.mybatis.core.util.MyBatisUtils;
|
|
||||||
import cn.iocoder.yudao.framework.tenant.core.db.TenantDatabaseInterceptor;
|
|
||||||
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
|
|
||||||
import com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor;
|
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
|
||||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
|
||||||
import org.springframework.context.annotation.Bean;
|
|
||||||
import org.springframework.context.annotation.Configuration;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 多租户针对 DB 的自动配置
|
|
||||||
*
|
|
||||||
* @author 芋道源码
|
|
||||||
*/
|
|
||||||
@Configuration
|
|
||||||
// 允许使用 yudao.tenant.enable=false 禁用多租户
|
|
||||||
@ConditionalOnProperty(prefix = "yudao.tenant", value = "enable", matchIfMissing = true)
|
|
||||||
@EnableConfigurationProperties(TenantProperties.class)
|
|
||||||
public class YudaoTenantDatabaseAutoConfiguration {
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
public TenantLineInnerInterceptor tenantLineInnerInterceptor(TenantProperties properties,
|
|
||||||
MybatisPlusInterceptor interceptor) {
|
|
||||||
TenantLineInnerInterceptor inner = new TenantLineInnerInterceptor(new TenantDatabaseInterceptor(properties));
|
|
||||||
// 添加到 interceptor 中
|
|
||||||
// 需要加在首个,主要是为了在分页插件前面。这个是 MyBatis Plus 的规定
|
|
||||||
MyBatisUtils.addInterceptor(interceptor, inner, 0);
|
|
||||||
return inner;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@ -1,46 +0,0 @@
|
|||||||
package cn.iocoder.yudao.framework.tenant.config;
|
|
||||||
|
|
||||||
import cn.hutool.core.annotation.AnnotationUtil;
|
|
||||||
import cn.iocoder.yudao.framework.quartz.core.handler.JobHandler;
|
|
||||||
import cn.iocoder.yudao.framework.tenant.core.job.TenantJob;
|
|
||||||
import cn.iocoder.yudao.framework.tenant.core.job.TenantJobHandlerDecorator;
|
|
||||||
import cn.iocoder.yudao.framework.tenant.core.service.TenantFrameworkService;
|
|
||||||
import org.springframework.beans.BeansException;
|
|
||||||
import org.springframework.beans.factory.config.BeanPostProcessor;
|
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
|
||||||
import org.springframework.context.annotation.Bean;
|
|
||||||
import org.springframework.context.annotation.Configuration;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 多租户针对 Job 的自动配置
|
|
||||||
*
|
|
||||||
* @author 芋道源码
|
|
||||||
*/
|
|
||||||
@Configuration
|
|
||||||
// 允许使用 yudao.tenant.enable=false 禁用多租户
|
|
||||||
@ConditionalOnProperty(prefix = "yudao.tenant", value = "enable", matchIfMissing = true)
|
|
||||||
public class YudaoTenantJobAutoConfiguration {
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
|
|
||||||
public BeanPostProcessor jobHandlerBeanPostProcessor(TenantFrameworkService tenantFrameworkService) {
|
|
||||||
return new BeanPostProcessor() {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
|
|
||||||
if (!(bean instanceof JobHandler)) {
|
|
||||||
return bean;
|
|
||||||
}
|
|
||||||
// 有 TenantJob 注解的情况下,才会进行处理
|
|
||||||
if (!AnnotationUtil.hasAnnotation(bean.getClass(), TenantJob.class)) {
|
|
||||||
return bean;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 使用 TenantJobHandlerDecorator 装饰
|
|
||||||
return new TenantJobHandlerDecorator(tenantFrameworkService, (JobHandler) bean);
|
|
||||||
}
|
|
||||||
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@ -1,23 +0,0 @@
|
|||||||
package cn.iocoder.yudao.framework.tenant.config;
|
|
||||||
|
|
||||||
import cn.iocoder.yudao.framework.tenant.core.mq.TenantRedisMessageInterceptor;
|
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
|
||||||
import org.springframework.context.annotation.Bean;
|
|
||||||
import org.springframework.context.annotation.Configuration;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 多租户针对 MQ 的自动配置
|
|
||||||
*
|
|
||||||
* @author 芋道源码
|
|
||||||
*/
|
|
||||||
@Configuration
|
|
||||||
// 允许使用 yudao.tenant.enable=false 禁用多租户
|
|
||||||
@ConditionalOnProperty(prefix = "yudao.tenant", value = "enable", matchIfMissing = true)
|
|
||||||
public class YudaoTenantMQAutoConfiguration {
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
public TenantRedisMessageInterceptor tenantRedisMessageInterceptor() {
|
|
||||||
return new TenantRedisMessageInterceptor();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@ -1,37 +0,0 @@
|
|||||||
package cn.iocoder.yudao.framework.tenant.config;
|
|
||||||
|
|
||||||
import cn.iocoder.yudao.framework.common.enums.WebFilterOrderEnum;
|
|
||||||
import cn.iocoder.yudao.framework.tenant.core.security.TenantSecurityWebFilter;
|
|
||||||
import cn.iocoder.yudao.framework.tenant.core.service.TenantFrameworkService;
|
|
||||||
import cn.iocoder.yudao.framework.web.config.WebProperties;
|
|
||||||
import cn.iocoder.yudao.framework.web.core.handler.GlobalExceptionHandler;
|
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
|
||||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
|
||||||
import org.springframework.boot.web.servlet.FilterRegistrationBean;
|
|
||||||
import org.springframework.context.annotation.Bean;
|
|
||||||
import org.springframework.context.annotation.Configuration;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 多租户针对 Web 的自动配置
|
|
||||||
*
|
|
||||||
* @author 芋道源码
|
|
||||||
*/
|
|
||||||
@Configuration
|
|
||||||
// 允许使用 yudao.tenant.enable=false 禁用多租户
|
|
||||||
@ConditionalOnProperty(prefix = "yudao.tenant", value = "enable", matchIfMissing = true)
|
|
||||||
@EnableConfigurationProperties(TenantProperties.class)
|
|
||||||
public class YudaoTenantSecurityAutoConfiguration {
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
public FilterRegistrationBean<TenantSecurityWebFilter> tenantSecurityWebFilter(TenantProperties tenantProperties,
|
|
||||||
WebProperties webProperties,
|
|
||||||
GlobalExceptionHandler globalExceptionHandler,
|
|
||||||
TenantFrameworkService tenantFrameworkService) {
|
|
||||||
FilterRegistrationBean<TenantSecurityWebFilter> registrationBean = new FilterRegistrationBean<>();
|
|
||||||
registrationBean.setFilter(new TenantSecurityWebFilter(tenantProperties, webProperties,
|
|
||||||
globalExceptionHandler, tenantFrameworkService));
|
|
||||||
registrationBean.setOrder(WebFilterOrderEnum.TENANT_SECURITY_FILTER);
|
|
||||||
return registrationBean;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@ -1,28 +0,0 @@
|
|||||||
package cn.iocoder.yudao.framework.tenant.config;
|
|
||||||
|
|
||||||
import cn.iocoder.yudao.framework.common.enums.WebFilterOrderEnum;
|
|
||||||
import cn.iocoder.yudao.framework.tenant.core.web.TenantContextWebFilter;
|
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
|
||||||
import org.springframework.boot.web.servlet.FilterRegistrationBean;
|
|
||||||
import org.springframework.context.annotation.Bean;
|
|
||||||
import org.springframework.context.annotation.Configuration;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 多租户针对 Web 的自动配置
|
|
||||||
*
|
|
||||||
* @author 芋道源码
|
|
||||||
*/
|
|
||||||
@Configuration
|
|
||||||
// 允许使用 yudao.tenant.enable=false 禁用多租户
|
|
||||||
@ConditionalOnProperty(prefix = "yudao.tenant", value = "enable", matchIfMissing = true)
|
|
||||||
public class YudaoTenantWebAutoConfiguration {
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
public FilterRegistrationBean<TenantContextWebFilter> tenantContextWebFilter() {
|
|
||||||
FilterRegistrationBean<TenantContextWebFilter> registrationBean = new FilterRegistrationBean<>();
|
|
||||||
registrationBean.setFilter(new TenantContextWebFilter());
|
|
||||||
registrationBean.setOrder(WebFilterOrderEnum.TENANT_CONTEXT_FILTER);
|
|
||||||
return registrationBean;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@ -1,14 +0,0 @@
|
|||||||
package cn.iocoder.yudao.framework.tenant.core.job;
|
|
||||||
|
|
||||||
import cn.iocoder.yudao.framework.quartz.core.handler.JobHandlerInvoker;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 多租户 JobHandlerInvoker 拓展实现类
|
|
||||||
*
|
|
||||||
* @author 芋道源码
|
|
||||||
*/
|
|
||||||
public class TenantJobHandlerInvoker extends JobHandlerInvoker {
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
@ -1,6 +1,2 @@
|
|||||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||||
cn.iocoder.yudao.framework.tenant.config.YudaoTenantDatabaseAutoConfiguration,\
|
cn.iocoder.yudao.framework.tenant.config.YudaoTenantAutoConfiguration
|
||||||
cn.iocoder.yudao.framework.tenant.config.YudaoTenantWebAutoConfiguration,\
|
|
||||||
cn.iocoder.yudao.framework.tenant.config.YudaoTenantJobAutoConfiguration,\
|
|
||||||
cn.iocoder.yudao.framework.tenant.config.YudaoTenantMQAutoConfiguration,\
|
|
||||||
cn.iocoder.yudao.framework.tenant.config.YudaoTenantSecurityAutoConfiguration
|
|
||||||
|
|||||||
Loading…
Reference in New Issue