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,这个过滤器可以用来限制旧的接口不可访问,防止不存在的地址转发到后面的服务。


 点击查看,支持一下

原文地址:https://www.cnblogs.com/wgslucky/p/11527457.html