聊聊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 {//......@Overridepublic 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 duplicatesif (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 headerList<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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/390356.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

node缓冲区_Node.js缓冲区介绍

node缓冲区什么是缓冲液&#xff1f; (What are Buffers?) Binary is simply a set or a collection of 1 and 0. Each number in a binary, each 1 and 0 in a set are called a bit. Computer converts the data to this binary format to store and perform operations. Fo…

专访赵加雨:WebRTC在网易云信的落地

去年的这个时候&#xff0c;在市面上公开表示使用WebRTC的公司还没几家&#xff0c;但2018年以来&#xff0c;宣布采用或支持WebRTC的公司已经越来越多。实时音视频提供商网易云信也在自研的NRTC中集成了WebRTC。在他们眼里&#xff0c;2017年是WebRTC的转折之年&#xff0c;而…

html/css杂题

1、css选择器&#xff1a;详细&#xff08;http://www.ruanyifeng.com/blog/2009/03/css_selectors.html&#xff09; 派生选择器&#xff1a;按标签 类别选择器&#xff1a;按class ID选择器&#xff1a;按ID 通用选择器&#xff1a;* 匹配所有 属性选择器&#xff1a;按属性&…

黑客马拉松 招募_我如何赢得第一次黑客马拉松-研究,设计和编码的2个狂野日子

黑客马拉松 招募I had no coding or engineering background. I studied biology in college, with no clue about what to do with my degree. 我没有编码或工程背景。 我在大学学习生物学&#xff0c;但不知道如何处理我的学位。 My first jobs were making cold calls in s…

1、Linux命令随笔

1 Linux命令总结2 3 man 命令帮助;4 help 命令的帮助&#xff08;bash的内置命令&#xff09;;5 ls list,查看目录列表;6 -ld&#xff1a;查看目录权限;7 -l:(long)长格式显示属性;8 -F:给不同的文件类型结尾加标识9 -p:给目录加斜线10 …

1137. 第 N 个泰波那契数

泰波那契序列 Tn 定义如下&#xff1a; T0 0, T1 1, T2 1, 且在 n > 0 的条件下 Tn3 Tn Tn1 Tn2 给你整数 n&#xff0c;请返回第 n 个泰波那契数 Tn 的值。 示例 1&#xff1a; 输入&#xff1a;n 4 输出&#xff1a;4 解释&#xff1a; T_3 0 1 1 2 T_4 1…

web图像_Web图像优化的基本介绍

web图像Images are an essential ingredient of most websites. The visual quality of pictures has a direct impact on the brand image and the message those images convey. And the weight of images usually accounts for a 40-60% of the data transferred on the web…

ElasticSearch客户端注解使用介绍

The best elasticsearch highlevel java rest api-----bboss 1.ElasticSearch客户端bboss提供了一系列注解 ESId 用于标识实体对象中作为docid的属性&#xff0c;该注解只有一个persistent 布尔值属性&#xff0c;用于控制被本注解标注的字段属性是否作为普通文档属性保存&am…

5827. 检查操作是否合法

给你一个下标从 0 开始的 8 x 8 网格 board &#xff0c;其中 board[r][c] 表示游戏棋盘上的格子 (r, c) 。棋盘上空格用 ‘.’ 表示&#xff0c;白色格子用 ‘W’ 表示&#xff0c;黑色格子用 ‘B’ 表示。 游戏中每次操作步骤为&#xff1a;选择一个空格子&#xff0c;将它变…

团队的远程管理_远程团队指南:如何管理您的远程软件开发团队

团队的远程管理Guides to help you work remotely seem to have swept through the Internet these days. 这些天来&#xff0c;帮助您远程工作的指南似乎席卷了Internet。 Do this, avoid that, stay productive, and all those run-of-the-mill tips we’ve already tried o…

JS 正则 钱

function ValidateIsDecial(sValue) {return (!sValue && !!!sValue && /^[0-9]{1,10}(\.[0-9]{0,2})?$/.test(sValue)); };验证 decimal(12,2) 小数点前允许10位,小数点后允许2位 1234567890 true 12345678901 false 0123456789 true 01234567891 false 123.…

5193. 删除字符使字符串变好

5193. 删除字符使字符串变好 一个字符串如果没有 三个连续 相同字符&#xff0c;那么它就是一个 好字符串 。 给你一个字符串 s &#xff0c;请你从 s 删除 最少 的字符&#xff0c;使它变成一个 好字符串 。 请你返回删除后的字符串。题目数据保证答案总是 唯一的 。 示例 …

2020计算机顶级大会_2020年顶级远程调试工具

2020计算机顶级大会When it comes to debugging, the tool you use is extremely important and can determine how easy is is to fix problems within your code. 在调试方面&#xff0c;您使用的工具非常重要&#xff0c;可以确定在代码中修复问题的难易程度。 In the earl…

BZOJ5292 洛谷4457 LOJ2513:[BJOI2018]治疗之雨——题解

https://www.lydsy.com/JudgeOnline/problem.php?id5292 https://www.luogu.org/problemnew/show/P4457 https://loj.ac/problem/2513 你现在有m1个数&#xff1a;第一个为p&#xff0c;最小值为0&#xff0c;最大值为n&#xff1b;剩下m个都是无穷&#xff0c;没有最小值或最…

PHP--------微信网页开发实现微信扫码功能

今天说说微商城项目中用到的扫一扫这个功能&#xff0c;分享一下&#xff0c;希望对各位有所帮助。 前提&#xff1a;要有公众号&#xff0c;和通过微信认证&#xff0c;绑定域名&#xff0c;得到相应信息&#xff0c;appid&#xff0c;appsecret等。 微信开发文档&#xff1a;…

313. 超级丑数

超级丑数 是一个正整数&#xff0c;并满足其所有质因数都出现在质数数组 primes 中。 给你一个整数 n 和一个整数数组 primes &#xff0c;返回第 n 个 超级丑数 。 题目数据保证第 n 个 超级丑数 在 32-bit 带符号整数范围内。 示例 1&#xff1a; 输入&#xff1a;n 12,…

初创公司股本结构_我如何向初创公司的开发团队添加一些结构-以及从过程中学到的东西

初创公司股本结构Until recently, Id spent the last 4 years of my career at FinTech start-ups. Id always worked for smaller companies, and being at a start-up was the next logical step in looking for roles where I could make the biggest difference. 直到最近…

拿什么拯救你,我的面试之——从零打卡刷Leetcode(No.003)

写在前边&#xff1a;小詹一直觉得自己编程能力不强&#xff0c;想在网上刷题&#xff0c;又怕不能坚持。不知道有木有和小伙伴和小詹一样想找个人一起刷题呢&#xff1f;欢迎和小詹一起定期刷leetcode&#xff0c;每周一周五更新一题&#xff0c;每一题都吃透&#xff0c;欢迎…

146. LRU 缓存机制

146. LRU 缓存机制 运用你所掌握的数据结构&#xff0c;设计和实现一个 LRU (最近最少使用) 缓存机制 。 实现 LRUCache 类&#xff1a; LRUCache(int capacity) 以正整数作为容量 capacity 初始化 LRU 缓存 int get(int key) 如果关键字 key 存在于缓存中&#xff0c;则返回…

[SQL] 请教一下 count里面有case when 一般情况下啥时候用

http://www.itpub.net/forum.php?modviewthread&tid1810967 问题: 比如 count(case when pday_id${deal_date} then 1 end) 我有点想不明白具体什么情况下count&#xff08;&#xff09; 这个小括号里面还要用case when 大家做BI统计的时候一般什么情况用啊 还有个…