• Spring-Cloud-Gateway Filter详细配置说明


    Spring-Cloud-Gateway 常见Filter详细配置说明

    gateway Filter官网:https://docs.spring.io/spring-cloud-gateway/docs/current/reference/html/#gatewayfilter-factories

    概述:

    SpringMVC里面的的拦截器Interceptor,Servlet的过滤器。

    “pre”和 “post” 分别会在请求被执行前调用和被执行后调用,用来修改请求和响应信息

    作用:

    • 请求鉴权
    • 异常处理
    • 记录接口调用时长统计

    类别

    • 全局默认过滤器:官网:https://docs.spring.io/spring-cloud-gateway/docs/current/reference/html/#global-filters,出厂默认已有的,直接用,作用于所有的路由,不推荐。
    • 单一内置过滤器:官网:https://docs.spring.io/spring-cloud-gateway/docs/4.0.4/reference/html/#gatewayfilter-factories,也可以称为网关过滤器,这种过滤器主要是作用于单一路由或者某个路由分组
    • 自定义过滤器

    过滤器配置详解

    常见内置过滤器
    1.增删改请求头响应头
     spring:
      cloud:
       gateway:
          routes:
            - id: pay_routh1 #pay_routh1                #路由的ID(类似mysql主键ID),没有固定规则但要求唯一,建议配合服务名
              #uri: http://localhost:8001                #匹配后提供服务的路由地址
              uri: lb://cloud-payment-service                #匹配后提供服务的路由地址
              predicates:
                - Path=/pay/gateway/get/**              # 断言,路径相匹配的进行路由
              filters:
                - AddRequestHeader=X-Request-username,value  # 请求头kv,若一头含有多参则重写一行设置
                - RemoveRequestHeader=sec-fetch-site  # 删除请求头sec-fetch-site
                - SetRequestHeader=sec-fetch-mode, Blue-updatebyzzyy # 将请求头sec-fetch-mode对应的值修改为Blue-updatebyzzyy
                - AddRequestParameter=customerId,9527001 # 新增请求参数Parameter:k ,v
                - RemoveRequestParameter=customerName   # 删除url请求参数customerName,你传递过来也是null
                - AddResponseHeader=X-Response-hello, BlueResponse # 新增响应参数X-Response-atguigu并设值为BlueResponse
                - SetResponseHeader=Date,2099-11-11 # 设置回应头Date值为2099-11-11
                - RemoveResponseHeader=Content-Type # 将默认自带Content-Type回应属性删除
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    2.对微服务路径和前缀进行配置

    前缀修改

     spring:
      cloud:
       gateway:
          routes:
            - id: pay_routh1 #pay_routh1                #路由的ID(类似mysql主键ID),没有固定规则但要求唯一,建议配合服务名
              #uri: http://localhost:8001                #匹配后提供服务的路由地址
              uri: lb://cloud-payment-service                #匹配后提供服务的路由地址
              predicates:
               #- Path=/pay/gateway/filter/**   # 被分拆为: PrefixPath + Path
    
                - Path=/gateway/filter/**              # 断言,为配合PrefixPath测试过滤,暂时注释掉/pay 
              filters:
                - PrefixPath=/pay # http://localhost:9527/pay/gateway/filter
               
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    路径修改

     spring:
      cloud:
       gateway:
          routes:
            - id: pay_routh1 #pay_routh1                #路由的ID(类似mysql主键ID),没有固定规则但要求唯一,建议配合服务名
              #uri: http://localhost:8001                #匹配后提供服务的路由地址
              uri: lb://cloud-payment-service                #匹配后提供服务的路由地址
              predicates:
               #- Path=/pay/gateway/filter/**   # 被分拆为: PrefixPath + Path
    
                - Path=/XYZ/abc/{segment}              # 断言,为配合SetPath测试,{segment}的内容最后被SetPath取代
              filters:
                - SetPath=/pay/gateway/{segment}  # {segment}表示占位符,你写abc也行但要上下一致
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    重定向

    spring:
      cloud:
       gateway:
          routes:
            - id: pay_routh1 #pay_routh1                #路由的ID(类似mysql主键ID),没有固定规则但要求唯一,建议配合服务名
              #uri: http://localhost:8001                #匹配后提供服务的路由地址
              uri: lb://cloud-payment-service                #匹配后提供服务的路由地址
              predicates:
               - Path=/gateway/filter/**              # 断言,为配合PrefixPath测试过滤,暂时注释掉/pay 
              filters:
                - RedirectTo=302, http://www.baidu.com/ # 访问
    
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
  • 相关阅读:
    ADB☀️二、安装与测试一个APK
    [vue] nvm
    MyBatis resultMap元素
    瑞吉外卖 —— 12、项目优化:主从复制、读写分离
    ABAP开发常用的BAPI汇总
    【Spring篇】简述IoC入门案例,DI入门案例
    Cyanine5-N3细胞成像和显微镜CY5-N3/azide星戈瑞
    C高级 脚本相关练习
    ComfyUI 完全入门:AI抠图/去除背景/更换图片背景/更换视频背景
    LeetCode·641.设计循环双端队列·循环双链表
  • 原文地址:https://blog.csdn.net/qq_62646841/article/details/136581046