【功能优化】全局:支持数据权限在 RPC 调用时,传递忽略数据权限的能力

pull/126/MERGE
YunaiV 2024-07-20 13:55:28 +08:00
parent 825be64d6d
commit e18069d45f
5 changed files with 107 additions and 0 deletions

View File

@ -34,6 +34,13 @@
<artifactId>yudao-spring-boot-starter-mybatis</artifactId>
</dependency>
<!-- RPC 远程调用相关 -->
<dependency>
<groupId>cn.iocoder.cloud</groupId>
<artifactId>yudao-spring-boot-starter-rpc</artifactId>
<optional>true</optional>
</dependency>
<!-- 业务组件 -->
<dependency>
<groupId>cn.iocoder.cloud</groupId>

View File

@ -0,0 +1,34 @@
package cn.iocoder.yudao.framework.datapermission.config;
import cn.iocoder.yudao.framework.datapermission.core.rpc.DataPermissionRequestInterceptor;
import cn.iocoder.yudao.framework.datapermission.core.rpc.DataPermissionRpcWebFilter;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import static cn.iocoder.yudao.framework.common.enums.WebFilterOrderEnum.TENANT_CONTEXT_FILTER;
/**
* RPC
*
* @author
*/
@AutoConfiguration
@ConditionalOnClass(name = "feign.RequestInterceptor")
public class YudaoDataPermissionRpcAutoConfiguration {
@Bean
public DataPermissionRequestInterceptor dataPermissionRequestInterceptor() {
return new DataPermissionRequestInterceptor();
}
@Bean
public FilterRegistrationBean<DataPermissionRpcWebFilter> dataPermissionRpcFilter() {
FilterRegistrationBean<DataPermissionRpcWebFilter> registrationBean = new FilterRegistrationBean<>();
registrationBean.setFilter(new DataPermissionRpcWebFilter());
registrationBean.setOrder(TENANT_CONTEXT_FILTER - 1); // 顺序没有绝对的要求,在租户 Filter 前面稳妥点
return registrationBean;
}
}

View File

@ -0,0 +1,27 @@
package cn.iocoder.yudao.framework.datapermission.core.rpc;
import cn.iocoder.yudao.framework.datapermission.core.annotation.DataPermission;
import cn.iocoder.yudao.framework.datapermission.core.aop.DataPermissionContextHolder;
import feign.RequestInterceptor;
import feign.RequestTemplate;
/**
* DataPermission RequestInterceptor Feign {@link DataPermission} header
*
* {@link DataPermission} enable
*
* @author
*/
public class DataPermissionRequestInterceptor implements RequestInterceptor {
public static final String ENABLE_HEADER_NAME = "data-permission-enable";
@Override
public void apply(RequestTemplate requestTemplate) {
DataPermission dataPermission = DataPermissionContextHolder.get();
if (dataPermission != null && Boolean.FALSE.equals(dataPermission.enable())) {
requestTemplate.header(ENABLE_HEADER_NAME, "false");
}
}
}

View File

@ -0,0 +1,38 @@
package cn.iocoder.yudao.framework.datapermission.core.rpc;
import cn.iocoder.yudao.framework.datapermission.core.aop.DataPermissionContextHolder;
import cn.iocoder.yudao.framework.datapermission.core.util.DataPermissionUtils;
import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.filter.OncePerRequestFilter;
import java.io.IOException;
import java.util.Objects;
/**
* {@link DataPermissionRequestInterceptor} RPC {@link DataPermissionContextHolder}
*
* @author
*/
public class DataPermissionRpcWebFilter extends OncePerRequestFilter {
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
throws ServletException, IOException {
String enable = request.getHeader(DataPermissionRequestInterceptor.ENABLE_HEADER_NAME);
if (Objects.equals(enable, Boolean.FALSE.toString())) {
DataPermissionUtils.executeIgnore(() -> {
try {
chain.doFilter(request, response);
} catch (IOException | ServletException e) {
throw new RuntimeException(e);
}
});
} else {
chain.doFilter(request, response);
}
}
}

View File

@ -1,2 +1,3 @@
cn.iocoder.yudao.framework.datapermission.config.YudaoDataPermissionAutoConfiguration
cn.iocoder.yudao.framework.datapermission.config.YudaoDeptDataPermissionAutoConfiguration
cn.iocoder.yudao.framework.datapermission.config.YudaoDataPermissionRpcAutoConfiguration