Spring Cloud GatewayFilter工厂(三)

RemoveResponseHeader GatewayFilter Factory

RemoveResponseHeader GatewayFilter Factory采用名称参数。它是要删除的标头的名称。

application.yml

spring:
  cloud:
    gateway:
      routes:
      - id: removeresponseheader_route
        uri: http://example.org
        filters:
        - RemoveResponseHeader=X-Response-Foo

这将在响应返回到网关客户端之前从响应中删除X-Response-Foo标头。

要删除任何类型的敏感标头,您应该为您可能要执行此操作的任何路由配置此过滤器。此外,您可以使用spring.cloud.gateway.default-filters配置此过滤器一次,并将其应用于所有路由。 

RewritePath GatewayFilter Factory

RewritePath GatewayFilter Factory采用路径regexp参数和替换参数。这使用Java正则表达式来灵活地重写请求路径。

application.yml

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

对于/ foo / bar的请求路径,这将在发出下游请求之前将路径设置为/ bar。注意由于YAML规范,$ \替换为$。

RewriteResponseHeader GatewayFilter Factory

RewriteResponseHeader GatewayFilter Factory采用名称,正则表达式和替换参数。它使用Java正则表达式以灵活的方式重写响应头值。

application.yml

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

对于标题值/ 42?user = ford&password = omg!what&flag = true,在发出下游请求后,它将被设置为/ 42?user = ford&password = ***&flag = true。由于YAML规范,请使用$ \表示$。

SaveSession GatewayFilter Factory 

SaveSession GatewayFilter Factory在转发下游调用之前强制执行WebSession :: save操作。当使用Spring Session与惰性数据存储之类的东西时,这是特别有用的,并且需要确保在转发调用之前已保存会话状态。

application.yml

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

如果要将Spring Security与Spring Session集成,并且希望确保将安全性详细信息转发到远程进程,则这很关键。

SecureHeaders GatewayFilter Factory

SecureHeaders GatewayFilter Factory根据此博客文章的建议为响应添加了许多标头。添加以下标头(以及默认值):

  • 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命名空间中设置相应的属性:

Property to change:

  • xss-protection-header

  • strict-transport-security

  • frame-options

  • content-type-options

  • referrer-policy

  • content-security-policy

  • download-options

  • permitted-cross-domain-policies

SetPath GatewayFilter Factory

SetPath GatewayFilter Factory采用路径模板参数。它提供了一种通过允许模板化路径段来操作请求路径的简单方法。这使用了Spring Framework中的uri模板。允许多个匹配的段。

application.yml

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

对于/ foo / bar的请求路径,这将在发出下游请求之前将路径设置为/ bar。

SetResponseHeader GatewayFilter Factory

SetResponseHeader GatewayFilter Factory获取名称和值参数。

application.yml

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

此GatewayFilter用给定名称替换所有标头,而不是添加。因此,如果下游服务器以X-Response-Foo:1234响应,则将替换为X-Response-Foo:Bar,这是网关客户端将接收的内容。

SetStatus GatewayFilter Factory

SetStatus GatewayFilter Factory采用单个状态参数。它必须是有效的Spring HttpStatus。它可以是整数值404或枚举NOT_FOUND的字符串表示形式。

application.yml

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

In either case, the HTTP status of the response will be set to 401.

猜你喜欢

转载自blog.csdn.net/u013702678/article/details/88745327
今日推荐