摘要:歡迎訪問我的歡迎訪問我的內容所有原創文章分類匯總及配套源碼,涉及等本篇概覽本篇概覽作為實戰系列的第五篇,是時候了解過濾器的作用了,本篇咱們一起來了解內置好的過濾器,真是種類繁多功能強大過濾器顧名思義,就是在請求頭部添加指定的內容帶有的完整配
https://github.com/zq2599/blog_demos
內容:所有原創文章分類匯總及配套源碼,涉及Java、Docker、Kubernetes、DevOPS等;
server: #服務端口 port: 8081spring: application: name: hello-gateway cloud: gateway: routes: - id: path_route uri: http://127.0.0.1:8082 predicates: - Path=/hello/** filters: - AddRequestHeader=x-request-foo, bar-config
[ { "id": "path_route_addr", "uri": "http://127.0.0.1:8082", "predicates": [ { "name": "Path", "args": { "pattern": "/hello/**" } } ], "filters": [ { "name": "AddRequestHeader", "args": { "name": "x-request-foo", "value": "bar-dynamic" } } ] }]
AddRequestParameter過濾器顧名思義,就是添加請求參數
配置如下,服務提供方收到的請求中會多一個參數,名為foo,值為bar-config:
server: #服務端口 port: 8081spring: application: name: hello-gateway cloud: gateway: routes: - id: path_route uri: http://127.0.0.1:8082 predicates: - Path=/hello/** filters: - AddRequestParameter=foo, bar-config
[ { "id": "path_route_addr", "uri": "http://127.0.0.1:8082", "predicates": [ { "name": "Path", "args": { "pattern": "/hello/**" } } ], "filters": [ { "name": "AddRequestParameter", "args": { "name": "foo", "value": "bar-dynamic" } } ] }]
AddResponseHeader過濾器就是在響應的header中添加參數
配置如下,客戶端收到的響應,其header中會多一個參數,名為foo,值為bar-config-response:
server: #服務端口 port: 8081spring: application: name: hello-gateway cloud: gateway: routes: - id: path_route uri: http://127.0.0.1:8082 predicates: - Path=/hello/** filters: - AddResponseHeader=foo, bar-config-response
[ { "id": "path_route_addr", "uri": "http://127.0.0.1:8082", "predicates": [ { "name": "Path", "args": { "pattern": "/hello/**" } } ], "filters": [ { "name": "AddResponseHeader", "args": { "name": "foo", "value": "bar-dynamic-response" } } ] }]
服務提供方返回的response的header中,如果有的key出線了多個value(例如跨域場景下的Access-Control-Allow-Origin),DedupeResponseHeader過濾器可以將重復的value剔除調,剔除策略有三種:RETAIN_FIRST (保留第一個,默認), RETAIN_LAST(保留最后一個), RETAIN_UNIQUE(去重)
配置如下,指定了兩個header key的去重,策略是保留最后一個:
server: #服務端口 port: 8081spring: application: name: hello-gateway cloud: gateway: routes: - id: path_route uri: http://127.0.0.1:8082 predicates: - Path=/hello/** filters: - DedupeResponseHeader=Access-Control-Allow-Credentials Access-Control-Allow-Origin, RETAIN_LAST
服務提供方返回的response的header中,如果有的key出線了多個value(例如跨域場景下的Access-Control-Allow-Origin),DedupeResponseHeader過濾器可以將重復的value剔除調,剔除策略有三種:RETAIN_FIRST (保留第一個,默認), RETAIN_LAST(保留最后一個), RETAIN_UNIQUE(去重)
配置如下,指定了兩個header key的去重,策略是保留最后一個:
server: #服務端口 port: 8081spring: application: name: hello-gateway cloud: gateway: routes: - id: path_route uri: http://127.0.0.1:8082 predicates: - Path=/hello/** filters: - DedupeResponseHeader=Access-Control-Allow-Credentials Access-Control-Allow-Origin, RETAIN_LAST
spring: cloud: gateway: routes: - id: ingredients uri: lb://ingredients predicates: - Path=//ingredients/** filters: - name: CircuitBreaker args: name: fetchIngredients fallbackUri: forward:/fallback - id: ingredients-fallback uri: http://localhost:9994 predicates: - Path=/fallback filters: - name: FallbackHeaders args: executionExceptionTypeHeaderName: Test-Header
MapRequestHeader用于header中的鍵值對復制,如下配置的意思是:如果請求header中有Blue就新增名為X-Request-Red的key,其值和Blue的值一樣
配置如下,指定了兩個header key的去重,策略是保留最后一個:
server: #服務端口 port: 8081spring: application: name: hello-gateway cloud: gateway: routes: - id: path_route uri: http://127.0.0.1:8082 predicates: - Path=/hello/** filters: - MapRequestHeader=Blue, X-Request-Red
PrefixPath很好理解,就是轉發到服務提供者的時候,給path加前綴
例如我這邊服務提供者原始地址是http://127.0.0.1:8082/hello/str配置如下,如果我給網關配置PrefixPath=hello,那么訪問網關的時候,請求路徑中就不需要hello了,配置如下:
server: #服務端口 port: 8081spring: application: name: hello-gateway cloud: gateway: routes: - id: path_route uri: http://127.0.0.1:8082 predicates: - Path=/str filters: - PrefixPath=/hello
PreserveHostHeader在轉發請求到服務提供者的時候,會保留host信息(否則就只能由HTTP client來決定了)
先看不使用PreserveHostHeader的效果,如下圖,服務提供者收到的請求header中的host就是網關配置的信息:
server: #服務端口 port: 8081spring: application: name: hello-gateway cloud: gateway: routes: - id: path_route uri: http://127.1.1.1:11111 predicates: - Path=/hello/** filters: - RedirectTo=302, http://127.0.0.1:8082/hello/str
RemoveRequestHeader很好理解,刪除請求header中的指定值
下面的配置會刪除請求header中的foo:
server: #服務端口 port: 8081spring: application: name: hello-gateway cloud: gateway: routes: - id: path_route uri: http://127.0.0.1:8082 predicates: - Path=/hello/** filters: - RemoveRequestHeader=foo
RemoveResponseHeader刪除響應header中的指定值
下面的配置會刪除響應header中的foo:
server: #服務端口 port: 8081spring: application: name: hello-gateway cloud: gateway: routes: - id: path_route uri: http://127.0.0.1:8082 predicates: - Path=/hello/** filters: - RemoveResponseHeader=foo
RemoveRequestParameter 刪除請求參數中的指定參數
下面的配置會刪除請求參數中的foo:
server: #服務端口 port: 8081spring: application: name: hello-gateway cloud: gateway: routes: - id: path_route uri: http://127.0.0.1:8082 predicates: - Path=/hello/** filters: - RemoveRequestParameter=foo1
RewritePath非常實用,將請求參數中的路徑做變換
下面的配置會將/test/str轉成/hello/str:
server: #服務端口 port: 8081spring: application: name: hello-gateway cloud: gateway: routes: - id: path_route uri: http://127.0.0.1:8082 predicates: - Path=/test/** filters: - RewritePath=/test/?(?.*), /hello/$/{segment}
RewriteLocationResponseHeader用于改寫response中的location信息
配置如下,一共是四個參數:stripVersionMode、locationHeaderName、hostValue、protocolsRegex
例如請求是api.example.com/some/object/name,response的location是object-service.prod.example.net/v2/some/object/id,最終會被下面的filter改寫為api.example.com/some/object/id
spring: cloud: gateway: routes: - id: rewritelocationresponseheader_route uri: http://example.org filters: - RewriteLocationResponseHeader=AS_IN_REQUEST, Location, ,
NEVER_STRIP:不執行
AS_IN_REQUEST :原始請求沒有vesion,就執行
ALWAYS_STRIP :固定執行
Location用于替換host:port部分,如果沒有就是用Request中的host
protocolsRegex用于匹配協議,如果匹配不上,name過濾器啥都不做
RewriteResponseHeader很好理解:修改響應header,參數有三個:header的key,匹配value的正則表達式,修改value的結果
下面的配置表示修改響應header中X-Response-Red這個key的value,找到password=xxx的內容,改成password=***
server: #服務端口 port: 8081spring: application: name: hello-gateway cloud: gateway: routes: - id: path_route uri: http://127.0.0.1:8082 predicates: - Path=/test/** filters: - RewriteResponseHeader=X-Response-Red, , password=[^&]+, password=***
server: #服務端口 port: 8081spring: application: name: hello-gateway cloud: gateway: routes: - id: path_route uri: http://127.0.0.1:8082 predicates: - Path=/hello/** filters: - SecureHeaders
server: #服務端口 port: 8081spring: application: name: hello-gateway cloud: gateway: filter: secure-headers: disable: - x-frame-options - strict-transport-security routes: - id: path_route uri: http://127.0.0.1:8082 predicates: - Path=/test/{segment} filters: - SetPath=/hello/{segment}
server: #服務端口 port: 8081spring: application: name: hello-gateway cloud: gateway: filter: secure-headers: disable: - x-frame-options - strict-transport-security routes: - id: path_route uri: http://127.0.0.1:8082 predicates: - Path=/hello/** filters: - SetRequestHeader=X-Request-Red, Blue
server: #服務端口 port: 8081spring: application: name: hello-gateway cloud: gateway: filter: secure-headers: disable: - x-frame-options - strict-transport-security routes: - id: path_route uri: http://127.0.0.1:8082 predicates: - Path=/hello/{segment} filters: - SetRequestHeader=X-Request-Red, Blue-{segment}
server: #服務端口 port: 8081spring: application: name: hello-gateway cloud: gateway: filter: secure-headers: disable: - x-frame-options - strict-transport-security routes: - id: path_route uri: http://127.0.0.1:8082 predicates: - Path=/hello/** filters: - SetResponseHeader=X-Request-Red, Blue
server: #服務端口 port: 8081spring: application: name: hello-gateway cloud: gateway: routes: - id: path_route uri: http://127.0.0.1:8082 predicates: - Path=/hello/** filters: - SetStatus=500
server: #服務端口 port: 8081spring: application: name: hello-gateway cloud: gateway: set-status: original-status-header-name: aaabbbccc routes: - id: path_route uri: http://127.0.0.1:8082 predicates: - Path=/hello/** filters: - SetStatus=500
server: #服務端口 port: 8081spring: application: name: hello-gateway cloud: gateway: set-status: original-status-header-name: aaabbbccc routes: - id: path_route uri: http://127.0.0.1:8082 predicates: - Path=/aaa/** filters: - StripPrefix=2
spring: cloud: gateway: routes: - id: retry_test uri: http://localhost:8080/flakey predicates: - Host=*.retry.com filters: - name: Retry args: retries: 3 statuses: BAD_GATEWAY methods: GET,POST backoff: firstBackoff: 10ms maxBackoff: 50ms factor: 2 basedOnPreviousValue: false
spring: cloud: gateway: routes: - id: request_size_route uri: http://localhost:8080/upload predicates: - Path=/upload filters: - name: RequestSize args: maxSize: 5000000
SetRequestHostHeader會修改請求header中的host值
下面的配置,會將請求header中的host改為aaabbb
server: #服務端口 port: 8081spring: application: name: hello-gateway cloud: gateway: routes: - id: path_route uri: http://127.0.0.1:8082 predicates: - Path=/hello/** filters: - name: SetRequestHostHeader args: host: aaabbb
@Beanpublic RouteLocator routes(RouteLocatorBuilder builder) { return builder.routes() .route("rewrite_request_obj", r -> r.host("*.rewriterequestobj.org") .filters(f -> f.prefixPath("/httpbin") .modifyRequestBody(String.class, Hello.class, MediaType.APPLICATION_JSON_VALUE, (exchange, s) -> return Mono.just(new Hello(s.toUpperCase())))).uri(uri)) .build();}static class Hello { String message; public Hello() { } public Hello(String message) { this.message = message; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; }}
@Beanpublic RouteLocator routes(RouteLocatorBuilder builder) { return builder.routes() .route("rewrite_response_upper", r -> r.host("*.rewriteresponseupper.org") .filters(f -> f.prefixPath("/httpbin") .modifyResponseBody(String.class, String.class, (exchange, s) -> Mono.just(s.toUpperCase()))).uri(uri)) .build();}
spring: cloud: gateway: routes: - id: resource uri: http://localhost:9000 predicates: - Path=/resource filters: - TokenRelay=
spring: cloud: gateway: default-filters: - AddResponseHeader=X-Response-Default-Red, Default-Blue - PrefixPath=/httpbin
微信搜索「程序員欣宸」,我是欣宸,期待與您一同暢游Java世界...
https://github.com/zq2599/blog_demos
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/123758.html
摘要:歡迎訪問我的歡迎訪問我的內容所有原創文章分類匯總及配套源碼,涉及等本篇概覽本篇概覽作為實戰系列的第九篇,咱們聊聊如何用修改原始請求和響應內容,以及修改過程中遇到的問題首先是修改請求,如下圖,瀏覽器是請求發起方,真實參數只有,經過網關時被塞歡迎訪問我的GitHubhttps://github.com/zq2599/blog_demos內容:所有原創文章分類匯總及配套源碼,涉及Java、Dock...
摘要:類似的工廠類還有和,,,這幾個就不做單獨講解了,使用方式是一樣的。配置示列配置示列討論時間文章中講的這幾個工廠類的作用我們已經了解了,那具體的使用場景有哪些適合在什么場景下使用呢歡迎大家留言討論。 今天我們來學習下GatewayFilter Factory,中文解釋就是過濾器工廠。 官方文檔對GatewayFilter Factory的介紹: Route filters allow t...
摘要:歡迎訪問我的歡迎訪問我的內容所有原創文章分類匯總及配套源碼,涉及等本篇概覽本篇概覽本文是實戰系列的第八篇,經過前面的學習,咱們對過濾器已了解得差不多,今天來補全過濾器的最后一個版塊限流默認的限流器是基于實現的,限流算法是大家熟悉的令牌桶關于歡迎訪問我的GitHubhttps://github.com/zq2599/blog_demos內容:所有原創文章分類匯總及配套源碼,涉及Java、Doc...
閱讀 1731·2023-04-25 23:43
閱讀 908·2021-11-24 09:39
閱讀 713·2021-11-22 15:25
閱讀 1709·2021-11-22 12:08
閱讀 1085·2021-11-18 10:07
閱讀 2066·2021-09-23 11:22
閱讀 3338·2021-09-22 15:23
閱讀 2469·2021-09-13 10:32