SpringCloudGateway內建GatewayFilter工廠類之Path類過濾器 (三)
阿新 • • 發佈:2021-11-25
1:PrefixPath
將所有請求路徑前加上路徑;value
- PrefixPath=value
spring:
cloud:
gateway:
routes:
- id: prefixpath_route
uri: http://example.org
filters:
- PrefixPath=value
2:RewritePath
利用路徑正則表示式靈活替換請求路徑
eg:
spring: cloud: gateway: routes: #外掛請求轉發 #(?<segment>/?.*):匹配 /任意字元,此處/出現0次或1次。將匹配到的結果捕獲到名稱為segment的組中 #$\{segment}:將 segment所捕獲到的文字置換到此處,注意,\的出現是由於避免yaml認為這是一個變數,在gateway進行解析時,會被替換為${segment} - id: "ms-api-gateway" uri: "lb://ms-api-gateway" predicates: - Path=/isv/mini/common/** filters: - RewritePath=/isv(?<segment>/?.*), /isv/ms/app$\{segment}
3:SetPath
SetPath GatewayFilter Factory採用路徑模板引數。 它提供了一種通過允許模板化路徑段來操作請求路徑的簡單方法。 這使用了Spring Framework中的uri模板。 允許多個匹配的段。
- SetPath=/{segment}
eg:
spring: cloud: gateway: routes: - id: setpath_route uri: http://example.org predicates: - Path=/foo/{segment} filters: - SetPath=/{segment}
4:StripPrefix
StripPrefix閘道器過濾器工廠採用一個引數StripPrefix。 StripPrefix引數表示在將請求傳送到下游之前從請求中剝離的路徑個數。
- StripPrefix
eg:
spring:
cloud:
gateway:
routes:
- id: nameRoot
uri: http://nameservice
predicates:
- Path=/name/**
filters:
- StripPrefix=1