目录
GatewayFilter 工厂
网关过滤器工厂GatewayFilterFactory 允许以某种方式修改传入的HTTP 请求或返回的HTTP 响应。其作用域是某些特定路由。SpringCloudGateway 包括很多种内置的网关过滤器工厂。下面会学习较常用的几种。
AddRequestHeader
AddRequestHeader
GatewayFilter
工厂需要一个 name 和 value 参数。下面的例子配置了一个 AddRequestHeader
GatewayFilter
。
添加请求头
spring:
application:
name: depart-consumer # 微服务名称
cloud:
gateway:
routes:
- id: add_request_header_route
uri: http://localhost:8080
predicates:
- Path=/**
filters:
- AddRequestHeader=X-Request-Color, blue
新建一个服务,端口8080,获取请求头
package com.zjw.controller;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Enumeration;
/**
* @since 2023/11/28 13:41
*/
@RestController
@RequestMapping("/info")
public class ShowInfoController {
@GetMapping("/header")
public String getHeader(HttpServletRequest request){
Enumeration<String> headers = request.getHeaders("X-Request-Color");
StringBuilder sb = new StringBuilder();
while (headers.hasMoreElements()) {
sb.append(headers.nextElement()).append(" ");
}
return "X-Request-Color: " + sb.toString();
}
}
通过api实现增加请求头。
@Configuration
public class GatewayConfig {
/**
* 增加请求头
*/
@Bean
public RouteLocator addRequestHeader(RouteLocatorBuilder builder) {
return builder.routes()
.route("add_request_header_route",
r -> r.path("/**")
.filters(fs -> fs
.addRequestHeader("X-Request-Color", "blue")
.addRequestHeader("X-Request-Color", "red"))
.uri("http://localhost:8080"))
.build();
}
}
AddRequestHeadersIfNotPresent
AddRequestHeadersIfNotPresent
GatewayFilter
工厂接受一个由冒号分隔的 name 和 value 键值对的集合。下面的例子配置了一个AddRequestHeadersIfNotPresent
GatewayFilter
。
spring:
application:
name: depart-consumer # 微服务名称
cloud:
gateway:
routes:
- id: add_request_header_route
uri: http://localhost:8080
predicates:
- Path=/**
filters:
- AddRequestHeader=X-Request-Color, blue
- AddRequestHeadersIfNotPresent=X-Request-Color-1:blue,X-Request-Color-2:green
这个列表为所有匹配的请求在下游请求的header信息中添加了两个header信息 X-Request-Color-1:blue
和 X-Request-Color-2:green
。这类似于 AddRequestHeader
的工作方式,但与 AddRequestHeader
不同的是,它只在header 信息不存在的情况下才会这样做。否则,客户端请求中的原始值将被发送。
java写法
@Configuration
public class GatewayConfig {
/**
* addRequestHeader增加请求头
*/
@Bean
public RouteLocator addRequestHeadersIfNotPresent(RouteLocatorBuilder builder) {
return builder.routes()
.route("add_request_header_route",
r -> r.path("/**")
.filters(fs -> fs
.addRequestHeadersIfNotPresent("X-Request-Color:blue"))
.uri("http://localhost:8080"))
.build();
}
}
AddRequestParameter
AddRequestParameter
GatewayFilter
Factory需要一个 name 和 value 参数。下面的例子配置了一个AddRequestParameter
GatewayFilter
。
spring:
application:
name: depart-consumer # 微服务名称
cloud:
gateway:
routes:
- id: add_request_parameter_route
uri: http://localhost:8080
predicates:
- Path=/**
filters:
- AddRequestParameter=color, blue
- AddRequestParameter=color, red
java实现
@Configuration
public class GatewayConfig {
/**
* addRequestHeader增加请求头
*/
@Bean
public RouteLocator addRequestParameter(RouteLocatorBuilder builder) {
return builder.routes()
.route("add_request_parameter_route",
r -> r.path("/**")
.filters(fs -> fs
.addRequestParameter("color","red"))
.uri("http://localhost:8080"))
.build();
}
}
AddResponseHeader
AddResponseHeader GatewayFilter 工厂需要一个 name 和 value 参数。下面的例子配置了一个 AddResponseHeader GatewayFilter。
spring:
application:
name: depart-consumer # 微服务名称
cloud:
gateway:
routes:
- id: add_response_header_route
uri: http://localhost:8080
predicates:
- Path=/**
filters:
- AddResponseHeader=X-Response-Color, Blue
java实现
@Configuration
public class GatewayConfig {
/**
* addResponseHeader增加响应头
*/
@Bean
public RouteLocator addResponseHeader(RouteLocatorBuilder builder) {
return builder.routes()
.route("add_response_header_route",
r -> r.path("/**")
.filters(fs -> fs
.addResponseHeader("color","red"))
.uri("http://localhost:8080"))
.build();
}
}
标签:网关,GatewayFilter,Color,SpringCloud,route,Request,header,GatewayFilterFactory,pub
From: https://www.cnblogs.com/zjw-blog/p/17862084.html