diff --git a/zlt-commons/zlt-ribbon-spring-boot-starter/src/main/java/com/central/common/ribbon/annotation/EnableBaseFeignInterceptor.java b/zlt-commons/zlt-ribbon-spring-boot-starter/src/main/java/com/central/common/ribbon/annotation/EnableBaseFeignInterceptor.java
new file mode 100644
index 00000000..6dc5fd3f
--- /dev/null
+++ b/zlt-commons/zlt-ribbon-spring-boot-starter/src/main/java/com/central/common/ribbon/annotation/EnableBaseFeignInterceptor.java
@@ -0,0 +1,25 @@
+package com.central.common.ribbon.annotation;
+
+import com.central.common.ribbon.config.FeignInterceptorConfig;
+import org.springframework.context.annotation.Import;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * 开启feign拦截器传递数据给下游服务,只包含基础数据
+ *
+ * @author zlt
+ * @date 2019/10/26
+ *
+ * Blog: https://blog.csdn.net/zlt2000
+ * Github: https://github.com/zlt2000
+ */
+@Target(ElementType.TYPE)
+@Retention(RetentionPolicy.RUNTIME)
+@Import(FeignInterceptorConfig.class)
+public @interface EnableBaseFeignInterceptor {
+
+}
diff --git a/zlt-commons/zlt-ribbon-spring-boot-starter/src/main/java/com/central/common/ribbon/annotation/EnableFeignInterceptor.java b/zlt-commons/zlt-ribbon-spring-boot-starter/src/main/java/com/central/common/ribbon/annotation/EnableFeignInterceptor.java
index bbbe6f54..5f97ac6b 100644
--- a/zlt-commons/zlt-ribbon-spring-boot-starter/src/main/java/com/central/common/ribbon/annotation/EnableFeignInterceptor.java
+++ b/zlt-commons/zlt-ribbon-spring-boot-starter/src/main/java/com/central/common/ribbon/annotation/EnableFeignInterceptor.java
@@ -1,5 +1,6 @@
package com.central.common.ribbon.annotation;
+import com.central.common.ribbon.config.FeignHttpInterceptorConfig;
import com.central.common.ribbon.config.FeignInterceptorConfig;
import org.springframework.context.annotation.Import;
@@ -9,14 +10,13 @@ import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
- * 在启动类上添加该注解来----开启自动登录用户对象注入
- * Token转化SysUser
+ * 开启feign拦截器传递数据给下游服务,包含基础数据和http的相关数据
*
* @author zlt
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
-@Import(FeignInterceptorConfig.class)
+@Import({FeignInterceptorConfig.class, FeignHttpInterceptorConfig.class})
public @interface EnableFeignInterceptor {
}
diff --git a/zlt-commons/zlt-ribbon-spring-boot-starter/src/main/java/com/central/common/ribbon/config/FeignHttpInterceptorConfig.java b/zlt-commons/zlt-ribbon-spring-boot-starter/src/main/java/com/central/common/ribbon/config/FeignHttpInterceptorConfig.java
new file mode 100644
index 00000000..d85ece2e
--- /dev/null
+++ b/zlt-commons/zlt-ribbon-spring-boot-starter/src/main/java/com/central/common/ribbon/config/FeignHttpInterceptorConfig.java
@@ -0,0 +1,91 @@
+package com.central.common.ribbon.config;
+
+import com.central.common.constant.CommonConstant;
+import com.central.common.constant.SecurityConstants;
+import feign.RequestInterceptor;
+import org.springframework.context.annotation.Bean;
+import org.springframework.web.context.request.RequestContextHolder;
+import org.springframework.web.context.request.ServletRequestAttributes;
+
+import javax.annotation.PostConstruct;
+import javax.servlet.http.HttpServletRequest;
+import java.util.ArrayList;
+import java.util.Enumeration;
+import java.util.List;
+
+/**
+ * feign拦截器,只包含http相关数据
+ *
+ * @author zlt
+ * @date 2019/10/26
+ *
+ * Blog: https://blog.csdn.net/zlt2000
+ * Github: https://github.com/zlt2000
+ */
+public class FeignHttpInterceptorConfig {
+ protected List requestHeaders = new ArrayList<>();
+
+ @PostConstruct
+ public void initialize() {
+ requestHeaders.add(SecurityConstants.USER_ID_HEADER);
+ requestHeaders.add(SecurityConstants.USER_HEADER);
+ requestHeaders.add(SecurityConstants.ROLE_HEADER);
+ requestHeaders.add(CommonConstant.Z_L_T_VERSION);
+ }
+
+ /**
+ * 使用feign client访问别的微服务时,将上游传过来的access_token、username、roles等信息放入header传递给下一个服务
+ */
+ @Bean
+ public RequestInterceptor httpFeignInterceptor() {
+ return template -> {
+ ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder
+ .getRequestAttributes();
+ if (attributes != null) {
+ HttpServletRequest request = attributes.getRequest();
+ Enumeration headerNames = request.getHeaderNames();
+ if (headerNames != null) {
+ String headerName;
+ String headerValue;
+ while(headerNames.hasMoreElements()) {
+ headerName = headerNames.nextElement();
+ if (requestHeaders.contains(headerName)) {
+ headerValue = request.getHeader(headerName);
+ template.header(headerName, headerValue);
+ }
+ }
+ }
+ //传递access_token,无网络隔离时需要传递
+ /*
+ String token = extractHeaderToken(request);
+ if (StrUtil.isEmpty(token)) {
+ token = request.getParameter(CommonConstant.ACCESS_TOKEN);
+ }
+ if (StrUtil.isNotEmpty(token)) {
+ template.header(CommonConstant.TOKEN_HEADER, CommonConstant.BEARER_TYPE + " " + token);
+ }
+ */
+ }
+ };
+ }
+
+ /**
+ * 解析head中的token
+ * @param request
+ */
+ private String extractHeaderToken(HttpServletRequest request) {
+ Enumeration headers = request.getHeaders(CommonConstant.TOKEN_HEADER);
+ while (headers.hasMoreElements()) {
+ String value = headers.nextElement();
+ if ((value.toLowerCase().startsWith(CommonConstant.BEARER_TYPE))) {
+ String authHeaderValue = value.substring(CommonConstant.BEARER_TYPE.length()).trim();
+ int commaIndex = authHeaderValue.indexOf(',');
+ if (commaIndex > 0) {
+ authHeaderValue = authHeaderValue.substring(0, commaIndex);
+ }
+ return authHeaderValue;
+ }
+ }
+ return null;
+ }
+}
diff --git a/zlt-commons/zlt-ribbon-spring-boot-starter/src/main/java/com/central/common/ribbon/config/FeignInterceptorConfig.java b/zlt-commons/zlt-ribbon-spring-boot-starter/src/main/java/com/central/common/ribbon/config/FeignInterceptorConfig.java
index 4e7f5918..3ae3c0b6 100644
--- a/zlt-commons/zlt-ribbon-spring-boot-starter/src/main/java/com/central/common/ribbon/config/FeignInterceptorConfig.java
+++ b/zlt-commons/zlt-ribbon-spring-boot-starter/src/main/java/com/central/common/ribbon/config/FeignInterceptorConfig.java
@@ -7,64 +7,19 @@ import com.central.common.context.TenantContextHolder;
import feign.RequestInterceptor;
import org.slf4j.MDC;
import org.springframework.context.annotation.Bean;
-import org.springframework.web.context.request.RequestContextHolder;
-import org.springframework.web.context.request.ServletRequestAttributes;
-
-import javax.annotation.PostConstruct;
-import javax.servlet.http.HttpServletRequest;
-import java.util.ArrayList;
-import java.util.Enumeration;
-import java.util.List;
/**
- * feign拦截器
+ * feign拦截器,只包含基础数据
*
* @author zlt
*/
public class FeignInterceptorConfig {
- protected List requestHeaders = new ArrayList<>();
-
- @PostConstruct
- public void initialize() {
- requestHeaders.add(SecurityConstants.USER_ID_HEADER);
- requestHeaders.add(SecurityConstants.USER_HEADER);
- requestHeaders.add(SecurityConstants.ROLE_HEADER);
- requestHeaders.add(CommonConstant.Z_L_T_VERSION);
- }
-
/**
- * 使用feign client访问别的微服务时,将上游传过来的access_token、username、roles、client等信息放入header传递给下一个服务
+ * 使用feign client访问别的微服务时,将上游传过来的client、traceid等信息放入header传递给下一个服务
*/
@Bean
- public RequestInterceptor requestInterceptor() {
+ public RequestInterceptor baseFeignInterceptor() {
return template -> {
- ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder
- .getRequestAttributes();
- if (attributes != null) {
- HttpServletRequest request = attributes.getRequest();
- Enumeration headerNames = request.getHeaderNames();
- if (headerNames != null) {
- String headerName;
- String headerValue;
- while(headerNames.hasMoreElements()) {
- headerName = headerNames.nextElement();
- if (requestHeaders.contains(headerName)) {
- headerValue = request.getHeader(headerName);
- template.header(headerName, headerValue);
- }
- }
- }
- //传递access_token,无网络隔离时需要传递
- /*
- String token = extractHeaderToken(request);
- if (StrUtil.isEmpty(token)) {
- token = request.getParameter(CommonConstant.ACCESS_TOKEN);
- }
- if (StrUtil.isNotEmpty(token)) {
- template.header(CommonConstant.TOKEN_HEADER, CommonConstant.BEARER_TYPE + " " + token);
- }
- */
- }
//传递client
String tenant = TenantContextHolder.getTenant();
if (StrUtil.isNotEmpty(tenant)) {
@@ -78,24 +33,4 @@ public class FeignInterceptorConfig {
}
};
}
-
- /**
- * 解析head中的token
- * @param request
- */
- private String extractHeaderToken(HttpServletRequest request) {
- Enumeration headers = request.getHeaders(CommonConstant.TOKEN_HEADER);
- while (headers.hasMoreElements()) {
- String value = headers.nextElement();
- if ((value.toLowerCase().startsWith(CommonConstant.BEARER_TYPE))) {
- String authHeaderValue = value.substring(CommonConstant.BEARER_TYPE.length()).trim();
- int commaIndex = authHeaderValue.indexOf(',');
- if (commaIndex > 0) {
- authHeaderValue = authHeaderValue.substring(0, commaIndex);
- }
- return authHeaderValue;
- }
- }
- return null;
- }
}
diff --git a/zlt-gateway/sc-gateway/src/main/java/com/central/SCGatewayApp.java b/zlt-gateway/sc-gateway/src/main/java/com/central/SCGatewayApp.java
index 5aa7e856..e7d55fed 100644
--- a/zlt-gateway/sc-gateway/src/main/java/com/central/SCGatewayApp.java
+++ b/zlt-gateway/sc-gateway/src/main/java/com/central/SCGatewayApp.java
@@ -1,5 +1,6 @@
package com.central;
+import com.central.common.ribbon.annotation.EnableBaseFeignInterceptor;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@@ -13,6 +14,7 @@ import org.springframework.cloud.openfeign.EnableFeignClients;
* Github: https://github.com/zlt2000
*/
@EnableFeignClients
+@EnableBaseFeignInterceptor
@EnableDiscoveryClient
@SpringBootApplication
public class SCGatewayApp {
diff --git a/zlt-gateway/sc-gateway/src/main/java/com/central/gateway/filter/TraceFilter.java b/zlt-gateway/sc-gateway/src/main/java/com/central/gateway/filter/TraceFilter.java
index e8048cfc..9f77dee9 100644
--- a/zlt-gateway/sc-gateway/src/main/java/com/central/gateway/filter/TraceFilter.java
+++ b/zlt-gateway/sc-gateway/src/main/java/com/central/gateway/filter/TraceFilter.java
@@ -1,9 +1,7 @@
package com.central.gateway.filter;
-import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.IdUtil;
import com.central.common.constant.CommonConstant;
-import com.central.common.constant.SecurityConstants;
import com.central.log.properties.TraceProperties;
import org.slf4j.MDC;
import org.springframework.beans.factory.annotation.Autowired;
diff --git a/zlt-gateway/zuul-gateway/src/main/java/com/central/GatewayApp.java b/zlt-gateway/zuul-gateway/src/main/java/com/central/GatewayApp.java
index 2ee08232..7fe04b5f 100644
--- a/zlt-gateway/zuul-gateway/src/main/java/com/central/GatewayApp.java
+++ b/zlt-gateway/zuul-gateway/src/main/java/com/central/GatewayApp.java
@@ -1,5 +1,6 @@
package com.central;
+import com.central.common.ribbon.annotation.EnableBaseFeignInterceptor;
import com.central.common.ribbon.annotation.EnableFeignInterceptor;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@@ -12,7 +13,7 @@ import org.springframework.cloud.openfeign.EnableFeignClients;
*/
@EnableZuulProxy
@EnableFeignClients
-@EnableFeignInterceptor
+@EnableBaseFeignInterceptor
@EnableDiscoveryClient
@SpringBootApplication
public class GatewayApp {