专栏首页码匠的流水账聊聊spring cloud gateway的XForwardedHeadersFilter

聊聊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

  • _modifying_the_way_remote_addresses_are_resolved

本文分享自微信公众号 - 码匠的流水账(geek_luandun),作者:go4it

原文出处及转载信息见文内详细说明,如有侵权,请联系 yunjia_community@tencent.com 删除。

原始发表时间:2018-05-31

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • spring-cloud-stream-binder-kafka属性配置

    本文简单介绍下spring-cloud-stream-binder-kafka的一些属性配置。

    codecraft
  • 聊聊spring cloud gateway的RemoveHopByHopHeadersFilter

    本文主要研究一下spring cloud gateway的RemoveHopByHopHeadersFilter

    codecraft
  • 聊聊spring cloud的RequestHeaderToRequestUriGatewayFilter

    本文主要研究一下spring cloud的RequestHeaderToRequestUriGatewayFilter

    codecraft
  • springcloud(十二):服务网关 Spring Cloud GateWay 服务化和过滤器

    上篇主要讲解了网关代理单个服务的使用语法,在实际的工作中,服务的相互调用都是依赖于服务中心提供的入口来使用,服务中心往往注册了很多服务,如果每个服务都需要单独配...

    DencyCheng
  • 基因家族分析之获取全部cDNA碱基序列构建进化树

    现在让我们看第一个教程,以趋化因子基因家族为背景来获取序列进行多序列比对后绘制系统发育树。

    生信技能树
  • 客服MM被投诉说下单耗时很长,老板下令必须控制在1秒以内

    近期,公司的订单量一直在持续增加,客服经常被投诉到说我们的下单时间过长,有时要好几秒,然后客服 MM 就反映到我们技术部门,老板得知后就说,这怎么行呢?不能让我...

    架构师修炼
  • 互联网企业安全之端口监控

    背景 外网端口监控系统是整个安全体系中非常重要的一环,它就像眼睛一样,时刻监控外网端口开放情况,并且在发现高危端口时能够及时提醒安全、运维人员做出相应处理。 对...

    美团技术团队
  • GDPR施行现状:只有20%的企业合规

    近日,Dimensional调查组织的一项调研发现,在所有受访公司中,只有20%的企业完全符合GDPR标准,53%正处于实施阶段,27%尚未开始实施。

    FB客服
  • 【MyBatis框架点滴】——MyBatis输出映射

    版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/huyuyang6688/article/...

    DannyHoo
  • 【案例】中国城市规划设计研究院:新型城镇化监控与评估平台

    【数据猿导读】本案例中,调用了人口、土地、经济、资源环境四维度基础数据及各尺度的规划评估模型、检测系统构建了针对规划行业的大数据云平台——全国新型城镇化监控与评...

    数据猿

扫码关注云+社区

领取腾讯云代金券