Spring Cloud Gateway中 Filters的理解
Filters
Filters拦截器的作用是,对请求进行处理
可以进行流量染色
⭐增加请求头
例子
spring:
cloud:
gateway:
routes:
- id: add_request_header_route
uri: http://localhost:8123
predicates:
- Path=/api/**
filters:
- AddRequestHeader=ylx,666
访问 http://localhost:8080/api/name 自动跳转到 http://localhost:8123/api/name
http://localhost:8123/api/name 后端中获取request.getHeader(“ylx”) 中的请求头,打印出来666
说明添加请求头成功
我们可以通过在请求头上添加信息,特定的标识,可以实现对接口的保护,确保只有带有这个请求头的请求才能被下游的服务认可并允许调用。这就是流量染色
⭐增加请求参数
spring:
cloud:
gateway:
routes:
- id: add_request_header_route
uri: http://localhost:8123
predicates:
- Path=/api/**
filters:
- AddRequestHeader=ylx,666
- AddRequestParameter=name,ylx

过滤器AddRequestParameter=name,ylx 添加ylx到参数name中
❓为什么控制器方法参数没有添加注解@RequestParam gateway网关中配置的添加请求参数也能添加到控制器的方法参数中?
💡请求参数默认绑定: 在 GET 请求中,控制器方法的参数如果与请求中的参数名匹配,Spring MVC 会自动将请求参数绑定到方法参数上。这种自动绑定的行为








![[数据集][目标检测]男女性别检测数据集VOC+YOLO格式9769张2类别](https://i-blog.csdnimg.cn/direct/93598bcafc88454a8c6190685463d58e.jpeg)










