• 【微服务|SCG】Filters的33种用法


    增加请求头/请求参数/响应头

    spring:
      cloud:
        gateway:
          discovery:
            locator:
              enabled: true
              lower-case-service-id: true
          routes:
            - id: ossa-service-producer
              uri: lb://ossa-service-producer
              predicates:
                - Path=/gw/producer/**
                - After=2022-03-03T15:50:30.337+08:00[Asia/Shanghai]
                - Before=2024-03-03T15:50:30.337+08:00[Asia/Shanghai]
              filters:
                - StripPrefix=1
                # 增加请求头、请求参数
                - AddRequestHeader=origin-header,producer-header
                - AddRequestParameter= origin-param,producer-param
                - AddResponseHeader=rep-header,rep-header
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    新增动态header请求头

    spring:
      cloud:
        gateway:
          discovery:
            locator:
              enabled: true
              lower-case-service-id: true
          routes:
            - id: ossa-service-producer
              uri: lb://ossa-service-producer
              predicates:
                - Path=/gw/producer/{id}
                - After=2022-03-03T15:50:30.337+08:00[Asia/Shanghai]
                - Before=2024-03-03T15:50:30.337+08:00[Asia/Shanghai]
              filters:
                - StripPrefix=1
                # 增加请求头、请求参数
                - AddRequestHeader=origin-header,producer-header-{id}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    设置请求头、过滤器配置,修改移除请求头

    filters:
       - StripPrefix=1
       # 增加请求头、请求参数
       - AddRequestHeader=origin-header,producer-header-{id}
       - AddRequestParameter=origin-param,producer-param
       - AddResponseHeader=rep-header,rep-header
       # 设置请求头,没有则新增,有则修改
       - SetRequestHeader=X-Request-id,value
       # 移除请求头、请求参数
       - RemoveRequestHeader=X-Request-id
       - RemoveRequestParameter=X-Request
       - RemoveRequestParameter=X-Request-author
       - AddResponseHeader=req-param,req-param
       # 先从header取from-Header,如果有则赋值给to-Header,如果没from-Header则无效果
       - MapRequestHeader=from-Header,to-Header
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    PreserveHostHeader

    默认开启,在gateway转发请求前把原始请求的host头部带上,转发给目的服务。

    spring:
      cloud:
        gateway:
          routes:
          - id: preserve_host_route
            uri: https://example.org
            filters:
            - PreserveHostHeader
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    重写response加密密码

    #例如: /42?user=ford&passwd=omg!what&flag=true 经过下面配置替换为/42?user=ford&passwd=***&flag=true

    spring:
      cloud:
        gateway:
          routes:
          - id: rewriteresponseheader_route
            uri: https://example.org
            filters:
            - RewriteResponseHeader=X-Response-Red, , password=[^&]+, password=***
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    response去重

    1. RETAIN_FIRST保留第一个(default)

    2. RETAIN_LAST保留最后一个

    3. RETAIN_FIRST RETAIN_UNIQUE 保留唯一

    spring:
      cloud:
        gateway:
          routes:
          - id: dedupe_response_header_route
            uri: https://example.org
            filters:
            - DedupeResponseHeader=Access-Control-Allow-Credentials Access-Control-Allow-Origin
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    路径带前缀

    请求/ 其实是/mypath

    spring:
      cloud:
        gateway:
          routes:
          - id: prefixpath_route
            uri: https://example.org
            filters:
            - PrefixPath=/mypath
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    配置30几 跳转到指定地址

    #300 Multiple多种选择,请求的资源可能包括多个位置,相应可返回一个资源特征与地址的列表用于用户终端选择

    #301 Moved Permanently永久移动,请求的资源已被永久移动新位置,返回信息会包含URI,浏览器会自动定向到新地址,以后请求应该用新的URI代替

    #302 Found 临时移动,与301类似,资源临时被移动,客户端应该继续使用原URI

    #303 See Other查看其它地址,与301类似。使用GET和POST请求查看

    #304 未修改,所请求资源未修改,服务器返回此状态吗时,不会返回任何资源

    #305 Use proxy 使用代理,所请求资源必须通过代理

    #306 Unused 已经废弃的HTTP状态码

    #307 Temporary Redirect 临时重定向,与302类似,使用GET请求重定向

    - RedirectTo=302,http://www.baidu.com 
    
    - RedirectTo=301,http://www.taobao.com
    
    • 1
    • 2
    • 3

    修改状态码

    修改状态码,可以org.springframework.http.HttpStatus枚举,也可以直接写状态码

    无论哪种情况,响应的 HTTP 状态都设置为 401。

    spring:
      cloud:
        gateway:
          routes:
          - id: setstatusstring_route
            uri: https://example.org
            filters:
            - SetStatus=UNAUTHORIZED
          - id: setstatusint_route
            uri: https://example.org
            filters:
            - SetStatus=401
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    转发地址

    访问/api/getheader 实际访问:/getheader,转发的目的地址必须写 / 否则会出错

    - RewritePath=/consumingServiceEndpoint, /backingServiceEndpoint
    
    • 1
    spring:
      cloud:
        gateway:
          routes:
          - id: rewritepath_route
            uri: https://example.org
            predicates:
            - Path=/red/**
            filters:
            - RewritePath=/red/?(?<segment>.*), /$\{segment}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    去掉\增加请求路径中部分层级

    请求/name/first/123 实际请求/123

    spring:
      cloud:
        gateway:
          routes:
          - id: nameRoot
            uri: https://nameservice
            predicates:
            - Path=/name/first/**
            filters:
            # 从前往后去掉一些路径
            - StripPrefix=2
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    请求/name/first/123 实际请求/prefix/name/first/123

    spring:
      cloud:
        gateway:
          routes:
          - id: nameRoot
            uri: https://nameservice
            predicates:
            - Path=/name/first/**
            filters:
            # 增加前缀路径
            - PrefixPath=/prefix
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    重试过滤器

    spring:
      cloud:
        gateway:
          routes:
          - id: retry_test
            uri: http://localhost:8080/flakey
            predicates:
            - Host=*.retry.com
            filters:
            - name: Retry
              args:
                retries: 3
                statuses: BAD_GATEWAY
                methods: GET,POST
                backoff:
                  firstBackoff: 10ms
                  maxBackoff: 50ms
                  factor: 2
                  basedOnPreviousValue: false
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    过滤器设置请求大小

    filters:
        - name: RequestSize #过滤器名称
          args:   #控制请求的大小,超过则返回413.请求最大默认5000000 约5M
            maxSize:1000  #1KB
    
    • 1
    • 2
    • 3
    • 4

    spring-session

    只有当集成Spring Session才会将session放到Redis,来实现共享Session功能,如果项目继承了Spring Session中的Spring Security框架,如果希望安全验证信息可以转发到远程应用,那么这个配置是必须的。

    filters:
        - SaveSession
    
    • 1
    • 2

    默认filter

    spring:
      cloud:
        gateway:
          default-filters:
          - AddResponseHeader=X-Response-Default-Red, Default-Blue
          - PrefixPath=/httpbin
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
  • 相关阅读:
    python yield send 用法简明理解
    oracle 如何使用脚本实现访问控制(无需额外插件)
    信效度常见问题
    git常用命令等相关操作
    如何设置OBS虚拟摄像头给钉钉视频会议使用
    LVGL V8字体格式
    position: sticky;实现顶部悬停,吸顶效果
    SAS|proc report
    基于数字电路交通灯信号灯控制系统设计-单片机设计
    Java 如何将String转化为Int
  • 原文地址:https://blog.csdn.net/CSDN_SAVIOR/article/details/125610924