♻️ Refactoring code. close #4209 支持自定义数据权限拦截器、和租户拦截器

This commit is contained in:
lbw committed 2022-08-09 13:29:20 +08:00
1 parent b52d813aa4
commit 5ade945037
5 files changed
+45 -33

No files matched your search

@@ -3,7 +3,6 @@ package com.pig4cloud.pigx.common.data.datascope;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.toolkit.PluginUtils;
import com.baomidou.mybatisplus.extension.plugins.inner.InnerInterceptor;
import lombok.Setter;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.BoundSql;
@@ -18,7 +17,7 @@ import java.util.Map;
* @author lengleng
* @date 2020/11/29
*/
public class DataScopeInnerInterceptor implements InnerInterceptor {
public class DataScopeInnerInterceptor implements DataScopeInterceptor {
@Setter
private DataScopeHandle dataScopeHandle;
@@ -0,0 +1,13 @@
package com.pig4cloud.pigx.common.data.datascope;
import com.baomidou.mybatisplus.extension.plugins.inner.InnerInterceptor;
/**
* 数据权限抽象
*
* @author lengleng
* @date 2022/8/9
*/
public interface DataScopeInterceptor extends InnerInterceptor {
}
@@ -26,7 +26,7 @@ import com.pig4cloud.pigx.common.core.constant.SecurityConstants;
import com.pig4cloud.pigx.common.core.util.RetOps;
import com.pig4cloud.pigx.common.security.service.PigxUser;
import com.pig4cloud.pigx.common.security.util.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import java.util.Arrays;
@@ -41,10 +41,10 @@ import java.util.stream.Collectors;
* <p>
* 默认data scope 判断处理器
*/
@RequiredArgsConstructor
public class PigxDefaultDatascopeHandle implements DataScopeHandle {
@Autowired
private RemoteDataScopeService dataScopeService;
private final RemoteDataScopeService dataScopeService;
/**
* 计算用户数据权限
@@ -21,12 +21,11 @@ import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor;
import com.pig4cloud.pigx.admin.api.feign.RemoteDataScopeService;
import com.pig4cloud.pigx.common.data.config.PigxMybatisProperties;
import com.pig4cloud.pigx.common.data.datascope.DataScopeHandle;
import com.pig4cloud.pigx.common.data.datascope.DataScopeInnerInterceptor;
import com.pig4cloud.pigx.common.data.datascope.DataScopeSqlInjector;
import com.pig4cloud.pigx.common.data.datascope.PigxDefaultDatascopeHandle;
import com.pig4cloud.pigx.common.data.datascope.*;
import com.pig4cloud.pigx.common.data.resolver.SqlFilterArgumentResolver;
import com.pig4cloud.pigx.common.data.tenant.PigxTenantConfigProperties;
import com.pig4cloud.pigx.common.data.tenant.PigxTenantHandler;
import com.pig4cloud.pigx.common.security.service.PigxUser;
import org.apache.ibatis.mapping.DatabaseIdProvider;
@@ -65,32 +64,18 @@ public class MybatisPlusConfiguration implements WebMvcConfigurer {
resolverList.add(new SqlFilterArgumentResolver());
}
/**
* pigx 默认数据权限处理器
* @return PigxDefaultDatascopeHandle
*/
@Bean
@ConditionalOnMissingBean
@ConditionalOnClass(PigxUser.class)
public DataScopeHandle dataScopeHandle() {
return new PigxDefaultDatascopeHandle();
}
/**
* mybatis plus 拦截器配置
* @return PigxDefaultDatascopeHandle
*/
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
public MybatisPlusInterceptor mybatisPlusInterceptor(TenantLineInnerInterceptor tenantLineInnerInterceptor,
DataScopeInterceptor dataScopeInterceptor) {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
// 多租户支持
TenantLineInnerInterceptor tenantLineInnerInterceptor = new TenantLineInnerInterceptor();
tenantLineInnerInterceptor.setTenantLineHandler(pigxTenantHandler());
// 注入多租户支持
interceptor.addInnerInterceptor(tenantLineInnerInterceptor);
// 数据权限
DataScopeInnerInterceptor dataScopeInnerInterceptor = new DataScopeInnerInterceptor();
dataScopeInnerInterceptor.setDataScopeHandle(dataScopeHandle());
interceptor.addInnerInterceptor(dataScopeInnerInterceptor);
interceptor.addInnerInterceptor(dataScopeInterceptor);
// 分页支持
PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor();
paginationInnerInterceptor.setMaxLimit(1000L);
@@ -104,8 +89,23 @@ public class MybatisPlusConfiguration implements WebMvcConfigurer {
*/
@Bean
@ConditionalOnMissingBean
public PigxTenantHandler pigxTenantHandler() {
return new PigxTenantHandler();
public TenantLineInnerInterceptor tenantLineInnerInterceptor(PigxTenantConfigProperties tenantConfigProperties) {
TenantLineInnerInterceptor tenantLineInnerInterceptor = new TenantLineInnerInterceptor();
tenantLineInnerInterceptor.setTenantLineHandler(new PigxTenantHandler(tenantConfigProperties));
return tenantLineInnerInterceptor;
}
/**
* 数据权限拦截器
* @return DataScopeInterceptor
*/
@Bean
@ConditionalOnMissingBean
@ConditionalOnClass(PigxUser.class)
public DataScopeInterceptor dataScopeInterceptor(RemoteDataScopeService dataScopeService) {
DataScopeInnerInterceptor dataScopeInnerInterceptor = new DataScopeInnerInterceptor();
dataScopeInnerInterceptor.setDataScopeHandle(new PigxDefaultDatascopeHandle(dataScopeService));
return dataScopeInnerInterceptor;
}
/**
@@ -113,7 +113,7 @@ public class MybatisPlusConfiguration implements WebMvcConfigurer {
* @return
*/
@Bean
@ConditionalOnBean(DataScopeHandle.class)
@ConditionalOnBean(DataScopeInterceptor.class)
public DataScopeSqlInjector dataScopeSqlInjector() {
return new DataScopeSqlInjector();
}
@@ -18,11 +18,11 @@
package com.pig4cloud.pigx.common.data.tenant;
import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.LongValue;
import net.sf.jsqlparser.expression.NullValue;
import org.springframework.beans.factory.annotation.Autowired;
/**
* @author lengleng
@@ -31,10 +31,10 @@ import org.springframework.beans.factory.annotation.Autowired;
* 租户维护处理器
*/
@Slf4j
@RequiredArgsConstructor
public class PigxTenantHandler implements TenantLineHandler {
@Autowired
private PigxTenantConfigProperties properties;
private final PigxTenantConfigProperties properties;
/**
* 获取租户 ID 值表达式,只支持单个 ID 值