|
|
@@ -1,23 +1,26 @@
|
|
|
package cn.tr.plugin.biz.tenant;
|
|
|
|
|
|
import cn.tr.core.enums.WebFilterOrderEnum;
|
|
|
-import cn.tr.plugin.biz.tenant.config.TenantStrategyConfig;
|
|
|
import cn.tr.plugin.biz.tenant.config.aop.TenantIgnoreAspect;
|
|
|
+import cn.tr.plugin.biz.tenant.config.db.TenantCreateAndUpdateMetaObjectHandler;
|
|
|
import cn.tr.plugin.biz.tenant.config.db.TenantDatabaseInterceptor;
|
|
|
import cn.tr.plugin.biz.tenant.config.ignore.TenantIgnoreUrlConfig;
|
|
|
import cn.tr.plugin.biz.tenant.config.secutiry.TenantSecurityWebFilter;
|
|
|
import cn.tr.plugin.biz.tenant.config.service.TenantFrameworkService;
|
|
|
import cn.tr.plugin.biz.tenant.config.web.TenantContextWebFilter;
|
|
|
import cn.tr.plugin.biz.tenant.properties.TenantProperties;
|
|
|
-import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
|
|
|
-import com.baomidou.mybatisplus.extension.plugins.inner.InnerInterceptor;
|
|
|
+import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
|
|
|
import com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor;
|
|
|
+import org.springframework.beans.BeansException;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.beans.factory.config.BeanPostProcessor;
|
|
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
|
|
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.Lazy;
|
|
|
+import org.springframework.context.annotation.Primary;
|
|
|
import org.springframework.web.SpringServletContainerInitializer;
|
|
|
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
|
|
|
|
|
|
@@ -31,12 +34,23 @@ import java.util.*;
|
|
|
*/
|
|
|
@ConditionalOnProperty(prefix = "tr.tenant", value = "enable", matchIfMissing = true)
|
|
|
@EnableConfigurationProperties(TenantProperties.class)
|
|
|
-public class TrTenantAutoConfiguration {
|
|
|
- @Bean
|
|
|
- public TenantStrategyConfig tenantStrategyConfig(){
|
|
|
- return new TenantStrategyConfig();
|
|
|
- }
|
|
|
+public class TrTenantAutoConfiguration implements BeanPostProcessor {
|
|
|
|
|
|
+ @Override
|
|
|
+ public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
|
|
|
+ if(bean.getClass()==TenantProperties.class){
|
|
|
+ TenantProperties properties= (TenantProperties) bean;
|
|
|
+ properties.getIgnoreUrls()
|
|
|
+ .addAll(Arrays.asList("/actuator/**",
|
|
|
+ "/druid/**",
|
|
|
+ "/favicon.ico",
|
|
|
+ "/**.html",
|
|
|
+ "/**/*.html",
|
|
|
+ "/error","/swagger-resources/**","/swagger-ui/**","/webjars/**","/v2/api-docs/*","/v2/api-docs","/v1/**"));
|
|
|
+
|
|
|
+ }
|
|
|
+ return bean;
|
|
|
+ }
|
|
|
// ========== AOP ==========
|
|
|
@Bean
|
|
|
public TenantIgnoreAspect tenantIgnoreAspect() {
|
|
|
@@ -45,26 +59,18 @@ public class TrTenantAutoConfiguration {
|
|
|
|
|
|
// ========== DB ==========
|
|
|
@Bean
|
|
|
- @ConditionalOnClass(MybatisPlusInterceptor.class)
|
|
|
- public TenantLineInnerInterceptor tenantLineInnerInterceptor(TenantProperties properties,
|
|
|
- MybatisPlusInterceptor interceptor) {
|
|
|
+ @Lazy
|
|
|
+ public TenantLineInnerInterceptor tenantLineInnerInterceptor(TenantProperties properties) {
|
|
|
TenantLineInnerInterceptor inner = new TenantLineInnerInterceptor(new TenantDatabaseInterceptor(properties));
|
|
|
// 添加到 interceptor 中
|
|
|
// 需要加在首个,主要是为了在分页插件前面。这个是 MyBatis Plus 的规定
|
|
|
- addInterceptor(interceptor,inner,0);
|
|
|
return inner;
|
|
|
}
|
|
|
|
|
|
@Bean
|
|
|
- @ConditionalOnClass(MybatisPlusInterceptor.class)
|
|
|
- public MybatisPlusInterceptor mybatisPlusInterceptor(){
|
|
|
- return new MybatisPlusInterceptor();
|
|
|
- }
|
|
|
-
|
|
|
- private void addInterceptor(MybatisPlusInterceptor interceptor, InnerInterceptor inner, int index) {
|
|
|
- List<InnerInterceptor> inners = new ArrayList<>(interceptor.getInterceptors());
|
|
|
- inners.add(index, inner);
|
|
|
- interceptor.setInterceptors(inners);
|
|
|
+ @Primary
|
|
|
+ public MetaObjectHandler tenantCreateAndUpdateMetaObjectHandler(){
|
|
|
+ return new TenantCreateAndUpdateMetaObjectHandler();
|
|
|
}
|
|
|
|
|
|
// ========== Security ==========
|
|
|
@@ -88,9 +94,9 @@ public class TrTenantAutoConfiguration {
|
|
|
return registrationBean;
|
|
|
}
|
|
|
|
|
|
- @Bean
|
|
|
- public TenantIgnoreUrlConfig ignoreConfig(RequestMappingHandlerMapping handlerMapping, TenantProperties tenantProperties){
|
|
|
- return new TenantIgnoreUrlConfig(handlerMapping,tenantProperties);
|
|
|
+ @Bean(initMethod = "init")
|
|
|
+ public TenantIgnoreUrlConfig ignoreConfig(List<RequestMappingHandlerMapping> handlerMappings, TenantProperties tenantProperties){
|
|
|
+ return new TenantIgnoreUrlConfig(handlerMappings,tenantProperties);
|
|
|
}
|
|
|
|
|
|
}
|