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回应属性删除
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
           

路径修改

 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也行但要上下一致

重定向

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. Spring-Cloud-Gateway Filter详细配置说明

    2024-03-10 16:54:01       48 阅读
  2. Spring Cloud配置中心详细介绍及使用

    2024-03-10 16:54:01       29 阅读
  3. Spring——配置说明

    2024-03-10 16:54:01       25 阅读
  4. Spring Cloud原理详解

    2024-03-10 16:54:01       35 阅读
  5. Spring Cloud原理详解

    2024-03-10 16:54:01       40 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-10 16:54:01       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-10 16:54:01       106 阅读
  3. 在Django里面运行非项目文件

    2024-03-10 16:54:01       87 阅读
  4. Python语言-面向对象

    2024-03-10 16:54:01       96 阅读

热门阅读

  1. 53. 最大子数组和(力扣LeetCode)

    2024-03-10 16:54:01       50 阅读
  2. 阿里巴巴商家爬虫工具 1688采集软件使用教程

    2024-03-10 16:54:01       40 阅读
  3. hadoop 总结

    2024-03-10 16:54:01       45 阅读
  4. 解决:Glide 在回调中再次加载图片报错

    2024-03-10 16:54:01       47 阅读
  5. sql返回数据怎么添加索引

    2024-03-10 16:54:01       40 阅读
  6. 速盾网络:cdn加速技术和云计算的区别

    2024-03-10 16:54:01       43 阅读
  7. adb shell pm 查询设备应用

    2024-03-10 16:54:01       46 阅读
  8. springcloud学习过程错误

    2024-03-10 16:54:01       51 阅读
  9. spring三种配置方式总结

    2024-03-10 16:54:01       40 阅读
  10. 学习笔记 反悔贪心

    2024-03-10 16:54:01       33 阅读