|
@@ -1,16 +1,25 @@
|
|
|
package com.kxs.system.api.feign.config;
|
|
package com.kxs.system.api.feign.config;
|
|
|
|
|
|
|
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
|
|
|
+import com.kxs.common.core.constant.SecurityConstants;
|
|
|
import com.kxs.common.core.constant.ServiceNameConstants;
|
|
import com.kxs.common.core.constant.ServiceNameConstants;
|
|
|
-import com.kxs.system.api.feign.RemoteClientDetailsService;
|
|
|
|
|
-import com.kxs.system.api.feign.RemoteLogService;
|
|
|
|
|
|
|
+import com.kxs.common.core.util.WebUtils;
|
|
|
|
|
+import com.kxs.system.api.feign.*;
|
|
|
|
|
+import jakarta.servlet.http.HttpServletRequest;
|
|
|
import lombok.RequiredArgsConstructor;
|
|
import lombok.RequiredArgsConstructor;
|
|
|
import org.springframework.context.annotation.Bean;
|
|
import org.springframework.context.annotation.Bean;
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
|
|
+import org.springframework.http.HttpHeaders;
|
|
|
|
|
+import org.springframework.web.context.request.RequestAttributes;
|
|
|
|
|
+import org.springframework.web.context.request.RequestContextHolder;
|
|
|
|
|
+import org.springframework.web.context.request.ServletRequestAttributes;
|
|
|
import org.springframework.web.reactive.function.client.ClientRequest;
|
|
import org.springframework.web.reactive.function.client.ClientRequest;
|
|
|
import org.springframework.web.reactive.function.client.WebClient;
|
|
import org.springframework.web.reactive.function.client.WebClient;
|
|
|
import org.springframework.web.reactive.function.client.support.WebClientAdapter;
|
|
import org.springframework.web.reactive.function.client.support.WebClientAdapter;
|
|
|
import org.springframework.web.service.invoker.HttpServiceProxyFactory;
|
|
import org.springframework.web.service.invoker.HttpServiceProxyFactory;
|
|
|
|
|
|
|
|
|
|
+import java.util.Optional;
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* <p>
|
|
* <p>
|
|
|
* 当前服务的webClient配置
|
|
* 当前服务的webClient配置
|
|
@@ -28,14 +37,23 @@ public class AdminFeignClientConfiguration {
|
|
|
|
|
|
|
|
@Bean
|
|
@Bean
|
|
|
public WebClient oauthRequestInterceptor() {
|
|
public WebClient oauthRequestInterceptor() {
|
|
|
|
|
+
|
|
|
return WebClient.builder()
|
|
return WebClient.builder()
|
|
|
- //添加全局默认请求头
|
|
|
|
|
- .defaultHeader("source", "http-interface")
|
|
|
|
|
//给请求添加过滤器,添加自定义的认证头
|
|
//给请求添加过滤器,添加自定义的认证头
|
|
|
.filter((request, next) -> {
|
|
.filter((request, next) -> {
|
|
|
- ClientRequest filtered = ClientRequest.from(request)
|
|
|
|
|
- .build();
|
|
|
|
|
- return next.exchange(filtered);
|
|
|
|
|
|
|
+ ClientRequest.Builder filtered = ClientRequest.from(request);
|
|
|
|
|
+
|
|
|
|
|
+ ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
|
|
|
|
|
+ //不是web请求不传递token
|
|
|
|
|
+ if(requestAttributes != null){
|
|
|
|
|
+ HttpServletRequest httpServletRequest = requestAttributes.getRequest();
|
|
|
|
|
+ String token = httpServletRequest.getHeader(HttpHeaders.AUTHORIZATION);
|
|
|
|
|
+ if (!StrUtil.isBlank(token)) {
|
|
|
|
|
+ //传递token
|
|
|
|
|
+ filtered.header(HttpHeaders.AUTHORIZATION, token);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return next.exchange(filtered.build());
|
|
|
})
|
|
})
|
|
|
.baseUrl(ServiceNameConstants.ADMIN_SERVICE).build();
|
|
.baseUrl(ServiceNameConstants.ADMIN_SERVICE).build();
|
|
|
}
|
|
}
|
|
@@ -63,4 +81,40 @@ public class AdminFeignClientConfiguration {
|
|
|
HttpServiceProxyFactory factory = HttpServiceProxyFactory.builder(WebClientAdapter.forClient(client)).build();
|
|
HttpServiceProxyFactory factory = HttpServiceProxyFactory.builder(WebClientAdapter.forClient(client)).build();
|
|
|
return factory.createClient(RemoteClientDetailsService.class);
|
|
return factory.createClient(RemoteClientDetailsService.class);
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 用户
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param client 客户端
|
|
|
|
|
+ * @return 远程用户服务
|
|
|
|
|
+ */
|
|
|
|
|
+ @Bean
|
|
|
|
|
+ RemoteUserService remoteUserService(WebClient client) {
|
|
|
|
|
+ HttpServiceProxyFactory factory = HttpServiceProxyFactory.builder(WebClientAdapter.forClient(client)).build();
|
|
|
|
|
+ return factory.createClient(RemoteUserService.class);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 字典
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param client 客户端
|
|
|
|
|
+ * @return 字典
|
|
|
|
|
+ */
|
|
|
|
|
+ @Bean
|
|
|
|
|
+ RemoteDictService remoteDictService(WebClient client) {
|
|
|
|
|
+ HttpServiceProxyFactory factory = HttpServiceProxyFactory.builder(WebClientAdapter.forClient(client)).build();
|
|
|
|
|
+ return factory.createClient(RemoteDictService.class);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 参数
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param client 客户端
|
|
|
|
|
+ * @return 参数
|
|
|
|
|
+ */
|
|
|
|
|
+ @Bean
|
|
|
|
|
+ RemoteParamService remoteParamService(WebClient client) {
|
|
|
|
|
+ HttpServiceProxyFactory factory = HttpServiceProxyFactory.builder(WebClientAdapter.forClient(client)).build();
|
|
|
|
|
+ return factory.createClient(RemoteParamService.class);
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|