Spring Cloud Gateway內置GatewayFilter工廠類(三)


5.13 RewritePath GatewayFilter Factory

  這個過濾器的實現類是:RewritePathGatewayFilterFactory,它需要兩個參數,一個是請求路徑的正則表達式,一個是替換的路徑參數。使用Java的正則表達式重寫請求路徑可以更加靈活。在路由請求轉發給后面的服務的時候,可以根據需要重寫請求路徑,在application.yml中的配置如下所示:

 
        
spring:
  cloud:
    gateway:
      routes:
      - id: rewritepath_route
        uri: http://localhost:8080
        predicates:
        - Path=/foo/**
        filters:
        - RewritePath=/foo/(?<segment>.*), /$\{segment}

在瀏覽器中輸入http://localhost:8080/foo/bar,在經過此過濾器處理之后,將會使用bar代替/foo/bar,使請求路徑變成http://localhost:8080/bar。這里需要注意的是,由於YAML規則的限制,使用$\代替$。

在源碼中的項目中,在路由id為app-a-route的例子中已經使用了這個過濾器。在將請求轉發給后面的服務的時候,會重寫請求路徑。

5.14 RewriteResponseHeader GatewayFilter Factory

  這個過濾器的實現類是:RewriteResponseHeaderGatewayFilterFactory,它用來重寫響應的包頭信息,它需要三個參數,一個是重寫的包頭中的字段名,一個正則表達式,一個是替換參數。這里使用Java的正則表達來可以更靈活的重寫響應中的包頭數據。在application.yml中的配置如下所示:

spring:
  cloud:
    gateway:
      routes:
      - id: rewriteresponseheader_route
        uri: http://example.org
        filters:
        - RewriteResponseHeader=X-Response-Foo, , password=[^&]+, password=***

假如一個響應的包頭中X-Response-Foo的值為/42?user=ford&password=omg!what&flag=true,此過濾器將會把X-Response-Foo的值變成:/42?user=ford&password=***&flag=true,也就是把password=omg!what替換成了passowrd=***。這樣可以保護密碼的安全。在寫正則表達式的時候,由於YAML的語法的限制,需要使用$\代替換$

5.15 SaveSession GatewayFilter Factory

  這個過濾器的實現類是:SaveSessionGatewayFilterFactory,在請求向下面的執鏈發送之前,會強制調用WebSession:save操作,這是一個特殊的使用,比如當集成Spring Session框架時,會延遲存儲數據,需要保證session的狀態已經被保存。Spring Session會將session信息存儲到Redis中,以實現共享session功能。配置如下所示:

spring:
  cloud:
    gateway:
      routes:
      - id: save_session
        uri: http://example.org
        predicates:
        - Path=/foo/**
        filters:
        - SaveSession

如要項目集成了Spring Session中的Spring Security框架,希望安全驗證信息可以轉發到遠程應用,那么這個配置是必須的。

5.16 SecureHeaders GatewayFilter Factory

  這個過濾器工廠的實現類是:SecureHeadersGatewayFilterFactory,它的作用是給響應的包頭中添加一些安全保護的字段信息,可以查看這個博客:https://blog.appcanary.com/2017/http-security-headers.html,它說明了為什么要添加這些字段的原因。下面這些包頭字段是被默認添加的:

X-Xss-Protection:1; mode=block
Strict-Transport-Security:max-age=631138519
X-Frame-Options:DENY
X-Content-Type-Options:nosniff
Referrer-Policy:no-referrer
Content-Security-Policy:default-src 'self' https:; font-src 'self' https: data:; img-src 'self' https: data:; object-src 'none'; script-src https:; style-src 'self' https: 'unsafe-inline'
X-Download-Options:noopen
X-Permitted-Cross-Domain-Policies:none

可以在配置文件中通過下面這個配置修改上述包頭字段的默認值:

spring.cloud.gateway.filter.secure-headers

修改的屬性有:

xss-protection-header
strict-transport-security
frame-options
content-type-options
referrer-policy
content-security-policy
download-options
permitted-cross-domain-policies

5.17 SetPath GatewayFilter Factory

  這個過濾器工廠的實現類是:SetPathGatewayFilterFactory,它需要一個模板參數,它提供了一個簡單的根據模板參數操作請求路徑的方式,使用了Spring Framework的uri templates,也支持匹配多個segments。如下面配置所示:

spring:
  cloud:
    gateway:
      routes:
      - id: setpath_route
        uri: http://example.org
        predicates:
        - Path=/foo/{segment}
        filters:
        - SetPath=/{segment}

這個配置對於URI為foo/bar的請求,將會在向請求鏈發送之前變成/bar

5.18 SetResponseHeader GatewayFilter Factory

  這個過濾器的實現類是:SetResponseHeaderGatewayFilterFactory,它需要兩個參數,一個name,一個value。如下面的配置所示:

spring:
  cloud:
    gateway:
      routes:
      - id: setresponseheader_route
        uri: http://example.org
        filters:
        - SetResponseHeader=X-Response-Foo, Bar

這個過濾器會替換響應包頭中所有給定參數為name的字段值為value,而不是添加一個新的name=value,比如響應包頭中有一個字段:X-Response-Foo:1234,那么通過這個過濾器將會變為:X-Response-Foo:Bar返回給客戶端。

5.19 SetStatus GatewayFilter Factory

  這個過濾器工廠的實現類是:SetStatusGatewayFilterFactory,它需要一個status參數,這個參數必須是一個有效的Spring HttpStatus狀態值,這個status可以是整數值404,也可以HttpStatus枚舉的名字字符值,NOT_FOUND,如下面配置所示:

spring:
  cloud:
    gateway:
      routes:
      - id: setstatusstring_route
        uri: http://example.org
        filters:
        - SetStatus=BAD_REQUEST
      - id: setstatusint_route
        uri: http://example.org
        filters:
        - SetStatus=401

在這個示例中,響應的Http 狀態會被設置為401,這個過濾器可以用來限制舊的接口不可訪問,防止不存在的地址轉發到后面的服務。


 

 點擊查看,支持一下

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM