拆分feign拦截器适配webflux和http,修复sc-gateway开启url权限时租户的传递问题
This commit is contained in:
+25
@@ -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
|
||||||
|
* <p>
|
||||||
|
* Blog: https://blog.csdn.net/zlt2000
|
||||||
|
* Github: https://github.com/zlt2000
|
||||||
|
*/
|
||||||
|
@Target(ElementType.TYPE)
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
@Import(FeignInterceptorConfig.class)
|
||||||
|
public @interface EnableBaseFeignInterceptor {
|
||||||
|
|
||||||
|
}
|
||||||
+3
-3
@@ -1,5 +1,6 @@
|
|||||||
package com.central.common.ribbon.annotation;
|
package com.central.common.ribbon.annotation;
|
||||||
|
|
||||||
|
import com.central.common.ribbon.config.FeignHttpInterceptorConfig;
|
||||||
import com.central.common.ribbon.config.FeignInterceptorConfig;
|
import com.central.common.ribbon.config.FeignInterceptorConfig;
|
||||||
import org.springframework.context.annotation.Import;
|
import org.springframework.context.annotation.Import;
|
||||||
|
|
||||||
@@ -9,14 +10,13 @@ import java.lang.annotation.RetentionPolicy;
|
|||||||
import java.lang.annotation.Target;
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 在启动类上添加该注解来----开启自动登录用户对象注入
|
* 开启feign拦截器传递数据给下游服务,包含基础数据和http的相关数据
|
||||||
* Token转化SysUser
|
|
||||||
*
|
*
|
||||||
* @author zlt
|
* @author zlt
|
||||||
*/
|
*/
|
||||||
@Target(ElementType.TYPE)
|
@Target(ElementType.TYPE)
|
||||||
@Retention(RetentionPolicy.RUNTIME)
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
@Import(FeignInterceptorConfig.class)
|
@Import({FeignInterceptorConfig.class, FeignHttpInterceptorConfig.class})
|
||||||
public @interface EnableFeignInterceptor {
|
public @interface EnableFeignInterceptor {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
+91
@@ -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
|
||||||
|
* <p>
|
||||||
|
* Blog: https://blog.csdn.net/zlt2000
|
||||||
|
* Github: https://github.com/zlt2000
|
||||||
|
*/
|
||||||
|
public class FeignHttpInterceptorConfig {
|
||||||
|
protected List<String> 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<String> 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<String> 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
+3
-68
@@ -7,64 +7,19 @@ import com.central.common.context.TenantContextHolder;
|
|||||||
import feign.RequestInterceptor;
|
import feign.RequestInterceptor;
|
||||||
import org.slf4j.MDC;
|
import org.slf4j.MDC;
|
||||||
import org.springframework.context.annotation.Bean;
|
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
|
* @author zlt
|
||||||
*/
|
*/
|
||||||
public class FeignInterceptorConfig {
|
public class FeignInterceptorConfig {
|
||||||
protected List<String> 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
|
@Bean
|
||||||
public RequestInterceptor requestInterceptor() {
|
public RequestInterceptor baseFeignInterceptor() {
|
||||||
return template -> {
|
return template -> {
|
||||||
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder
|
|
||||||
.getRequestAttributes();
|
|
||||||
if (attributes != null) {
|
|
||||||
HttpServletRequest request = attributes.getRequest();
|
|
||||||
Enumeration<String> 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
|
//传递client
|
||||||
String tenant = TenantContextHolder.getTenant();
|
String tenant = TenantContextHolder.getTenant();
|
||||||
if (StrUtil.isNotEmpty(tenant)) {
|
if (StrUtil.isNotEmpty(tenant)) {
|
||||||
@@ -78,24 +33,4 @@ public class FeignInterceptorConfig {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 解析head中的token
|
|
||||||
* @param request
|
|
||||||
*/
|
|
||||||
private String extractHeaderToken(HttpServletRequest request) {
|
|
||||||
Enumeration<String> 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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package com.central;
|
package com.central;
|
||||||
|
|
||||||
|
import com.central.common.ribbon.annotation.EnableBaseFeignInterceptor;
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
|
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
|
||||||
@@ -13,6 +14,7 @@ import org.springframework.cloud.openfeign.EnableFeignClients;
|
|||||||
* Github: https://github.com/zlt2000
|
* Github: https://github.com/zlt2000
|
||||||
*/
|
*/
|
||||||
@EnableFeignClients
|
@EnableFeignClients
|
||||||
|
@EnableBaseFeignInterceptor
|
||||||
@EnableDiscoveryClient
|
@EnableDiscoveryClient
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
public class SCGatewayApp {
|
public class SCGatewayApp {
|
||||||
|
|||||||
@@ -1,9 +1,7 @@
|
|||||||
package com.central.gateway.filter;
|
package com.central.gateway.filter;
|
||||||
|
|
||||||
import cn.hutool.core.collection.CollectionUtil;
|
|
||||||
import cn.hutool.core.util.IdUtil;
|
import cn.hutool.core.util.IdUtil;
|
||||||
import com.central.common.constant.CommonConstant;
|
import com.central.common.constant.CommonConstant;
|
||||||
import com.central.common.constant.SecurityConstants;
|
|
||||||
import com.central.log.properties.TraceProperties;
|
import com.central.log.properties.TraceProperties;
|
||||||
import org.slf4j.MDC;
|
import org.slf4j.MDC;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package com.central;
|
package com.central;
|
||||||
|
|
||||||
|
import com.central.common.ribbon.annotation.EnableBaseFeignInterceptor;
|
||||||
import com.central.common.ribbon.annotation.EnableFeignInterceptor;
|
import com.central.common.ribbon.annotation.EnableFeignInterceptor;
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
@@ -12,7 +13,7 @@ import org.springframework.cloud.openfeign.EnableFeignClients;
|
|||||||
*/
|
*/
|
||||||
@EnableZuulProxy
|
@EnableZuulProxy
|
||||||
@EnableFeignClients
|
@EnableFeignClients
|
||||||
@EnableFeignInterceptor
|
@EnableBaseFeignInterceptor
|
||||||
@EnableDiscoveryClient
|
@EnableDiscoveryClient
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
public class GatewayApp {
|
public class GatewayApp {
|
||||||
|
|||||||
Reference in New Issue
Block a user