1. 增加 RPC starter

2. security 组件:feign 调用时,通过 header 透传 LoginUser 信息
pull/4/head
YunaiV 2022-06-11 22:24:15 +08:00
parent 0cf1738c0a
commit ca6e7a4528
19 changed files with 159 additions and 34 deletions

View File

@ -222,6 +222,13 @@
<version>${redisson.version}</version>
</dependency>
<!-- RPC 远程调用相关 -->
<dependency>
<groupId>cn.iocoder.cloud</groupId>
<artifactId>yudao-spring-boot-starter-rpc</artifactId>
<version>${revision}</version>
</dependency>
<!-- Registry 注册中心相关 -->
<!-- Config 配置中心相关 -->

View File

@ -21,6 +21,7 @@
<module>yudao-spring-boot-starter-protection</module>
<module>yudao-spring-boot-starter-job</module>
<module>yudao-spring-boot-starter-mq</module>
<module>yudao-spring-boot-starter-rpc</module>
<module>yudao-spring-boot-starter-excel</module>
<module>yudao-spring-boot-starter-test</module>

View File

@ -0,0 +1,42 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>yudao-framework</artifactId>
<groupId>cn.iocoder.cloud</groupId>
<version>${revision}</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>yudao-spring-boot-starter-rpc</artifactId>
<packaging>jar</packaging>
<name>${project.artifactId}</name>
<description>
OpenFeign提供 RESTful API 的调用
Dubbo提供 Dubbo RPC 的调用
</description>
<url>https://github.com/YunaiV/ruoyi-vue-pro</url>
<dependencies>
<dependency>
<groupId>cn.iocoder.cloud</groupId>
<artifactId>yudao-common</artifactId>
</dependency>
<!-- RPC 远程调用相关 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-dubbo</artifactId>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,4 @@
/**
* TODO
*/
package cn.iocoder.yudao.framework.rpc.config;

View File

@ -0,0 +1,4 @@
/**
* TODO
*/
package cn.iocoder.yudao.framework.rpc.core;

View File

@ -0,0 +1,48 @@
package cn.iocoder.yudao.framework.rpc.core.util;
import cn.hutool.core.util.ReflectUtil;
import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
import feign.RequestTemplate;
import feign.template.HeaderTemplate;
import feign.template.Literal;
import feign.template.Template;
import feign.template.TemplateChunk;
import java.util.List;
import java.util.Map;
/**
* {@link feign.Feign}
*
* @author
*/
public class FeignUtils {
/**
* JSON Header
*
* @param requestTemplate
* @param name header
* @param value header
*/
@SuppressWarnings("unchecked")
public static void createJsonHeader(RequestTemplate requestTemplate, String name, Object value) {
if (value == null) {
return;
}
// 添加 header
String valueStr = JsonUtils.toJsonString(value);
requestTemplate.header(name, valueStr);
// fix由于 OpenFeign 针对 { 会进行分词,所以需要反射修改
// 具体分析,可见 https://zhuanlan.zhihu.com/p/360501330 文档
Map<String, HeaderTemplate> headers = (Map<String, HeaderTemplate>)
ReflectUtil.getFieldValue(requestTemplate, "headers");
HeaderTemplate template = headers.get(name);
List<Template> templateValues = (List<Template>)
ReflectUtil.getFieldValue(template, "values");
List<TemplateChunk> templateChunks = (List<TemplateChunk>)
ReflectUtil.getFieldValue(templateValues.get(0), "templateChunks");
templateChunks.set(0, Literal.create(valueStr));
}
}

View File

@ -0,0 +1,7 @@
/**
* OpenFeign RESTful API
* Dubbo Dubbo RPC
*
* @author
*/
package cn.iocoder.yudao.framework.rpc;

View File

@ -0,0 +1 @@
<http://www.iocoder.cn/Spring-Boot/Dubbo/?yudao>

View File

@ -0,0 +1 @@
<http://www.iocoder.cn/Spring-Boot/Feign/?yudao>

View File

@ -0,0 +1 @@
<http://www.iocoder.cn/Spring-Cloud-Alibaba/Dubbo/?yudao>

View File

@ -0,0 +1 @@
<http://www.iocoder.cn/Spring-Cloud/Feign/?yudao>

View File

@ -53,17 +53,9 @@
<!-- RPC 远程调用相关 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-dubbo</artifactId>
<groupId>cn.iocoder.cloud</groupId>
<artifactId>yudao-spring-boot-starter-rpc</artifactId>
<optional>true</optional>
</dependency>
</dependencies>

View File

@ -1,8 +1,11 @@
package cn.iocoder.yudao.framework.security.config;
import cn.iocoder.yudao.framework.security.core.rpc.LoginUserRequestInterceptor;
import cn.iocoder.yudao.module.system.api.oauth2.OAuth2TokenApi;
import cn.iocoder.yudao.module.system.api.permission.PermissionApi;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
@ -13,5 +16,11 @@ import org.springframework.context.annotation.Configuration;
@Configuration(proxyBeanMethods = false)
@EnableFeignClients(clients = {OAuth2TokenApi.class, // 主要是引入相关的 API 服务
PermissionApi.class})
public class YudaoSecurityFeignAutoConfiguration {
public class YudaoSecurityRpcAutoConfiguration {
@Bean
public LoginUserRequestInterceptor loginUserRequestInterceptor() {
return new LoginUserRequestInterceptor();
}
}

View File

@ -0,0 +1,19 @@
package cn.iocoder.yudao.framework.security.core.rpc;
import cn.iocoder.yudao.framework.rpc.core.util.FeignUtils;
import cn.iocoder.yudao.framework.security.core.LoginUser;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import feign.RequestInterceptor;
import feign.RequestTemplate;
public class LoginUserRequestInterceptor implements RequestInterceptor {
@Override
public void apply(RequestTemplate requestTemplate) {
LoginUser user = SecurityFrameworkUtils.getLoginUser();
if (user != null) {
FeignUtils.createJsonHeader(requestTemplate, SecurityFrameworkUtils.LOGIN_USER_HEADER, user);
}
}
}

View File

@ -1,4 +1,4 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
cn.iocoder.yudao.framework.security.config.YudaoSecurityFeignAutoConfiguration,\
cn.iocoder.yudao.framework.security.config.YudaoSecurityRpcAutoConfiguration,\
cn.iocoder.yudao.framework.security.config.YudaoSecurityAutoConfiguration,\
cn.iocoder.yudao.framework.security.config.YudaoWebSecurityConfigurerAdapter

View File

@ -37,15 +37,10 @@
<!-- RPC 远程调用相关 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
<groupId>cn.iocoder.cloud</groupId>
<artifactId>yudao-spring-boot-starter-rpc</artifactId>
</dependency>
<!-- <dependency>-->
<!-- <groupId>org.springframework.cloud</groupId>-->
<!-- <artifactId>spring-cloud-starter-openfeign</artifactId>-->
<!-- </dependency>-->
<!-- Registry 注册中心相关 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>

View File

@ -1,4 +1,7 @@
spring:
main:
allow-circular-references: true # 允许循环依赖,因为项目是三层架构,无法避免这个情况。
cloud:
# Spring Cloud Gateway 配置项,对应 GatewayProperties 类
gateway:

View File

@ -66,13 +66,8 @@
<!-- RPC 远程调用相关 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-dubbo</artifactId>
<groupId>cn.iocoder.cloud</groupId>
<artifactId>yudao-spring-boot-starter-rpc</artifactId>
</dependency>
<!-- Registry 注册中心相关 -->

View File

@ -81,13 +81,8 @@
<!-- RPC 远程调用相关 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-dubbo</artifactId>
<groupId>cn.iocoder.cloud</groupId>
<artifactId>yudao-spring-boot-starter-rpc</artifactId>
</dependency>
<!-- Registry 注册中心相关 -->