SpringCloud gateway内置过滤器之四

shigongp / 2023-05-06 / 原文

1、RewriteLocationResponseHeader GatewayFilter

RewriteLocationResponseHeader GatewayFilter修改Location响应标头的值,通常是为了消除后端特定的详细信息。有stripVersionMode、locationHeaderName、hostValue和protocolsRegex参数。protocolsRegx参数必须是有效的正则表达式字符串,协议名称与此字符串匹配。如果不匹配,过滤器将不执行任何操作。默认值为http|https|ftp|ftps。

spring:
  cloud:
    gateway:
      routes:
      - id: rewritelocationresponseheader_route
        uri: http://example.org
        filters:
        - RewriteLocationResponseHeader=AS_IN_REQUEST, Location, ,

stripVersionMode参数有以下可能的值:NEVER_STRIP、AS_IN_REQUEST(默认值)和ALWAYS_STRIP。

  • NEVER_STRIP:即使原始请求路径不包含任何版本,该版本也不会被剥离。
  • AS_IN_REQUEST:只有当原始请求路径不包含版本时,才会剥离版本。
  • ALWAYS_STRIP:版本总是被剥离的,即使原始请求路径包含版本。

2、RewriteResponseHeader GatewayFilter

RewriteResponseHeader GatewayFilter采用名称、正则表达式和替换参数。它使用Java正则表达式来灵活地重写响应头值。

spring:
  cloud:
    gateway:
      enabled: true
      routes:
        - id: Goods-Server  # 路由 id,唯一标识
          uri: lb://producer
          predicates:
            #  - Path=/**  # 断言,路由匹配条件,匹配 /product 开头的所有 api
              - Path=/producer/{segment}
          filters:
              - StripPrefix=1
              - RewriteResponseHeader=X-Response-Red, , password=[^&]+, password=***

修改Producer模块的Controller:

@RequestMapping("/hello")
public String hello(String name, HttpServletRequest request, HttpServletResponse response) {
    response.setHeader("X-Response-Red", "/42?user=ford&password=omg!what&flag=true");
    return "hello," + name + "," + port;
}

访问http://localhost:8500/producer/hello,从浏览器控制台看到:
 

 
响应头中的password的值被替换成***。

3、SaveSession GatewayFilter

SaveSession GatewayFilter在将调用转发到下游之前强制执行WebSession::save操作。当将Spring Session之类的东西与惰性数据存储一起使用时,这尤其有用,并且需要确保在进行转发调用之前已经保存了会话状态。

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

4、SecureHeaders GatewayFilter

在响应加入以下头:

  • 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

  • x-frame-options

  • x-content-type-options

  • referrer-policy

  • content-security-policy

  • x-download-options

  • x-permitted-cross-domain-policies

 

spring:
  cloud:
    gateway:
      enabled: true
      routes:
        - id: Goods-Server  # 路由 id,唯一标识
          uri: lb://producer
          predicates:
            #  - Path=/**  # 断言,路由匹配条件,匹配 /product 开头的所有 api
              - Path=/producer/{segment}
          filters:
              - StripPrefix=1
              - SaveSession
              - SecureHeaders

访问http://localhost:8500/producer/hello,从浏览器控制台中看到:

5、SetPath GatewayFilter

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

spring:
  cloud:
    gateway:
      enabled: true
      routes:
        - id: Goods-Server  # 路由 id,唯一标识
          uri: lb://producer
          predicates:
            #  - Path=/**  # 断言,路由匹配条件,匹配 /product 开头的所有 api
              - Path=/producer/{segment}
          filters:
              - SetPath=/{segment}

将/producer/{segment}请求路径设置成/hello。访问http://localhost:8500/producer/hello,成功。