聊聊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.pswp.cn/news/390356.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/390356.shtml
英文地址,請注明出處:http://en.pswp.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統計的時候一般什么情況用啊 還有個…