过滤器 RewriteLocationResponseHeader · SpringCloud微服务实战 · 看云

10. 过滤器 RewriteLocationResponseHeader

导航

RewriteLocationResponseHeader GatewayFilter 工厂 用来重写 响应头的Location 的值,以摆脱后端特定的详细信息。这需要 stripVersionMode , locationHeaderName , hostValue 和 protocolsRegex 参数。

本节代码地址


10.1 应用配置

server:
  port: 8699
spring:
  application:
    name: fw-gateways-gateway
  profiles:
    active: rewritelocationresponseheader_route
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
---
spring:
  cloud:
    gateway:
      routes:
        - id: rewritelocationresponseheader_route
          uri: lb://fw-cloud-ribbon-server
          predicates:
          - After=2020-01-08T18:30:11.965+08:00[Asia/Shanghai]
          filters:
          - RewriteLocationResponseHeader=AS_IN_REQUEST, Location, ,
  profiles: rewritelocationresponseheader_route

参数 stripVersionMode 可选值如下:

  • NEVER_STRIP:版本信息不会被剥离,即使原始请求路径不包含版本
  • AS_IN_REQUEST:仅当原始请求路径不包含任何版本时,才会剥离版本【默认】
  • ALWAYS_STRIP:即使原始请求路径包含版本,也会剥离版本

参数 hostValue,如果提供,会替换 Response Header Location 值中的 host:port 部分;如果不提供,则会使用 Request 的 Host 作为默认值

参数 protocolRegex,协议会与该值匹配,如果不匹配,过滤器不回做任何操作,默认值 http|https|ftp|ftps