聊聊spring cloud gateway的XForwardedHeadersFilter
序
本文主要研究spring cloud gateway的XForwardedHeadersFilter
GatewayAutoConfiguration
spring-cloud-gateway-core-2.0.0.RC1-sources.jar!/org/springframework/cloud/gateway/config/GatewayAutoConfiguration.java
@Configuration
@ConditionalOnProperty(name = "spring.cloud.gateway.enabled", matchIfMissing = true)
@EnableConfigurationProperties
@AutoConfigureBefore(HttpHandlerAutoConfiguration.class)
@AutoConfigureAfter({GatewayLoadBalancerClientAutoConfiguration.class, GatewayClassPathWarningAutoConfiguration.class})
@ConditionalOnClass(DispatcherHandler.class)
public class GatewayAutoConfiguration {
//......
@Bean
@ConditionalOnProperty(name = "spring.cloud.gateway.x-forwarded.enabled", matchIfMissing = true)
public XForwardedHeadersFilter xForwardedHeadersFilter() {
return new XForwardedHeadersFilter();
}
//......
}
預設註冊了XForwardedHeadersFilter
XForwardedHeadersFilter
spring-cloud-gateway-core-2.0.0.RC1-sources.jar!/org/springframework/cloud/gateway/filter/headers/XForwardedHeadersFilter.java
@ConfigurationProperties("spring.cloud.gateway.x-forwarded")
public class XForwardedHeadersFilter implements HttpHeadersFilter, Ordered {
//......
@Override
public HttpHeaders filter(HttpHeaders input, ServerWebExchange exchange) {
ServerHttpRequest request = exchange.getRequest();
HttpHeaders original = input;
HttpHeaders updated = new HttpHeaders();
original.entrySet().stream()
.forEach(entry -> updated.addAll(entry.getKey(), entry.getValue()));
if (isForEnabled()) {
String remoteAddr = request.getRemoteAddress().getAddress().getHostAddress();
List<String> xforwarded = original.get(X_FORWARDED_FOR_HEADER);
// prevent duplicates
if (remoteAddr != null &&
(xforwarded == null || !xforwarded.contains(remoteAddr))) {
write(updated, X_FORWARDED_FOR_HEADER, remoteAddr, isForAppend());
}
}
String proto = request.getURI().getScheme();
if (isProtoEnabled()) {
write(updated, X_FORWARDED_PROTO_HEADER, proto, isProtoAppend());
}
if (isPortEnabled()) {
String port = String.valueOf(request.getURI().getPort());
if (request.getURI().getPort() < 0) {
port = String.valueOf(getDefaultPort(proto));
}
write(updated, X_FORWARDED_PORT_HEADER, port, isPortAppend());
}
if (isHostEnabled()) {
String host = toHostHeader(request);
write(updated, X_FORWARDED_HOST_HEADER, host, isHostAppend());
}
return updated;
}
//......
}
- 如果spring.cloud.gateway.x-forwarded.for-enabled為true,則會寫入X-Forwarded-For
- 如果spring.cloud.gateway.x-forwarded.proto-enabled為true,則會寫入X-Forwarded-Proto
- 如果spring.cloud.gateway.x-forwarded.port-enabled為true,則會寫入X-Forwarded-Port
- 如果spring.cloud.gateway.x-forwarded.host-enabled為true,則會寫入X-Forwarded-Host
每個enable屬性都有一個append屬性,用來決定是否是新增還是追加
private void write(HttpHeaders headers, String name, String value, boolean append) {
if (append) {
headers.add(name, value);
// these headers should be treated as a single comma separated header
List<String> values = headers.get(name);
String delimitedValue = StringUtils.collectionToCommaDelimitedString(values);
headers.set(name, delimitedValue);
} else {
headers.set(name, value);
}
}
如果是append,則逗號分隔寫入headers,如果不是則是採取set操作。
配置
{
"sourceType": "org.springframework.cloud.gateway.filter.headers.XForwardedHeadersFilter",
"defaultValue": true,
"name": "spring.cloud.gateway.x-forwarded.enabled",
"description": "If the XForwardedHeadersFilter is enabled.",
"type": "java.lang.Boolean"
},
{
"sourceType": "org.springframework.cloud.gateway.filter.headers.XForwardedHeadersFilter",
"defaultValue": true,
"name": "spring.cloud.gateway.x-forwarded.for-append",
"description": "If appending X-Forwarded-For as a list is enabled.",
"type": "java.lang.Boolean"
},
{
"sourceType": "org.springframework.cloud.gateway.filter.headers.XForwardedHeadersFilter",
"defaultValue": true,
"name": "spring.cloud.gateway.x-forwarded.for-enabled",
"description": "If X-Forwarded-For is enabled.",
"type": "java.lang.Boolean"
},
{
"sourceType": "org.springframework.cloud.gateway.filter.headers.XForwardedHeadersFilter",
"defaultValue": true,
"name": "spring.cloud.gateway.x-forwarded.host-append",
"description": "If appending X-Forwarded-Host as a list is enabled.",
"type": "java.lang.Boolean"
},
{
"sourceType": "org.springframework.cloud.gateway.filter.headers.XForwardedHeadersFilter",
"defaultValue": true,
"name": "spring.cloud.gateway.x-forwarded.host-enabled",
"description": "If X-Forwarded-Host is enabled.",
"type": "java.lang.Boolean"
},
{
"sourceType": "org.springframework.cloud.gateway.filter.headers.XForwardedHeadersFilter",
"defaultValue": 0,
"name": "spring.cloud.gateway.x-forwarded.order",
"description": "The order of the XForwardedHeadersFilter.",
"type": "java.lang.Integer"
},
{
"sourceType": "org.springframework.cloud.gateway.filter.headers.XForwardedHeadersFilter",
"defaultValue": true,
"name": "spring.cloud.gateway.x-forwarded.port-append",
"description": "If appending X-Forwarded-Port as a list is enabled.",
"type": "java.lang.Boolean"
},
{
"sourceType": "org.springframework.cloud.gateway.filter.headers.XForwardedHeadersFilter",
"defaultValue": true,
"name": "spring.cloud.gateway.x-forwarded.port-enabled",
"description": "If X-Forwarded-Port is enabled.",
"type": "java.lang.Boolean"
},
{
"sourceType": "org.springframework.cloud.gateway.filter.headers.XForwardedHeadersFilter",
"defaultValue": true,
"name": "spring.cloud.gateway.x-forwarded.proto-append",
"description": "If appending X-Forwarded-Proto as a list is enabled.",
"type": "java.lang.Boolean"
},
{
"sourceType": "org.springframework.cloud.gateway.filter.headers.XForwardedHeadersFilter",
"defaultValue": true,
"name": "spring.cloud.gateway.x-forwarded.proto-enabled",
"description": "If X-Forwarded-Proto is enabled.",
"type": "java.lang.Boolean"
}
小結
spring cloud gateway提供了XForwardedHeadersFilter,用來決定進行路由轉發的時候轉發哪些X-Forwarded相關的header,同時提供append選項,用來控制是否是追加還是覆蓋到header中。
doc
相關文章
- Spring Cloud Gateway 深入SpringCloudGateway
- Spring cloud 之GatewaySpringCloudGateway
- Spring Cloud Gateway 限流操作SpringCloudGateway
- Spring Cloud Gateway 入門SpringCloudGateway
- Spring Cloud Gateway示例 | DevGlanSpringCloudGatewaydev
- spring cloud gateway 不生效SpringCloudGateway
- 聊聊spring cloud的DefaultEurekaServerContextSpringCloudServerContext
- spring cloud gateway 之限流篇SpringCloudGateway
- spring cloud gateway之filter篇SpringCloudGatewayFilter
- Spring Cloud Gateway 入門案例SpringCloudGateway
- Spring Cloud Gateway 聚合swagger文件SpringCloudGatewaySwagger
- 快速突擊 Spring Cloud GatewaySpringCloudGateway
- Spring Cloud Gateway入坑記SpringCloudGateway
- Spring Cloud Gateway使用簡介SpringCloudGateway
- Spring Cloud Gateway初體驗SpringCloudGateway
- Spring Cloud Gateway限流實戰SpringCloudGateway
- Spring Cloud Gateway入門 - spring.ioSpringCloudGateway
- Spring Cloud Gateway (一)入門篇SpringCloudGateway
- Spring Cloud Gateway WebFilter工廠 | BaeldungSpringCloudGatewayWebFilter
- spring-cloud-gateway靜態路由SpringCloudGateway路由
- 阿里Sentinel支援Spring Cloud Gateway啦阿里SpringCloudGateway
- Spring Cloud Gateway 之 過濾器SpringCloudGateway過濾器
- Spring Cloud Gateway之負載均衡SpringCloudGateway負載
- Spring Cloud Gateway之RouteLocator簡介SpringCloudGateway
- Spring Cloud Gateway---GlobalFilter(入門)SpringCloudGatewayFilter
- Spring Cloud Gateway 實現 gRpc 代理SpringCloudGatewayRPC
- spring-cloud-kubernetes與SpringCloud GatewaySpringCloudGCGateway
- spring cloud 2020 gateway 報錯503SpringCloudGateway
- Spring Cloud Gateway限制API速率 - tanzuSpringCloudGatewayAPI
- Spring Cloud Gateway應用篇(十三)SpringCloudGateway
- Spring Cloud Alibaba(四)--Gateway與SentinelSpringCloudGateway
- Spring Cloud Gateway的斷路器(CircuitBreaker)功能SpringCloudGatewayUI
- Spring Cloud Gateway 閘道器嚐鮮SpringCloudGateway
- spring Cloud Gateway 入門簡單使用SpringCloudGateway
- Spring Cloud Gateway 整合Eureka路由轉發SpringCloudGateway路由
- 手把手教你使用 Spring Cloud GatewaySpringCloudGateway
- 微服務閘道器 Spring Cloud Gateway微服務SpringCloudGateway
- Spring Cloud Gateway實戰之一:初探SpringCloudGateway