springcloud 分布式 Spring-Cloud-Gateway Filter详细配置说明 - predicates:

📅 2026-11-30 👤 拼多多助力营销技巧 📂 拼多多助力网站 👁️ 554 次阅读

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/ # 访问

精彩内容,springcloud

标签: 拼多多助力网站平台 赚钱项目 拼多多助力网 首码项目 手赚APP
拼多多助力营销技巧