Spring Cloud Gateway inject header
The below code works:
ServerHttpRequest request = exchange.getRequest()
.mutate()
.header("x-jwt-assertion", jwt)
.build();
ServerWebExchange exchange1 = exchange.mutate().request(request).build();
return chain.filter(exchange1);
You can take a look at
org.springframework.cloud.gateway.filter.factory.*GatewayFilterFactory
There are many useful gateway filter implementations.
For your question,
org.springframework.cloud.gateway.filter.factory.AddRequestHeaderGatewayFilterFactory
has a standard implementation.
public class AddRequestHeaderGatewayFilterFactory extends AbstractNameValueGatewayFilterFactory {
@Override
public GatewayFilter apply(NameValueConfig config) {
return (exchange, chain) -> {
ServerHttpRequest request = exchange.getRequest().mutate()
.header(config.getName(), config.getValue())
.build();
return chain.filter(exchange.mutate().request(request).build());
};
}
}