Spring Cloud Gateway inject header
Asked Answered
S

4

18

In the GatewayFilter, I am trying to inject a header into the request like below.

@Override public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) { //DO SOME AUTHORIZATION OPERATIONS String jwt = "xxxxx" exchange.getRequest().getHeaders().add("headerkey", jwt); return chain.filter(exchange); }

But I am getting the below error:

java.lang.UnsupportedOperationException: null
at java.util.Collections$UnmodifiableMap.computeIfAbsent(Collections.java:1535) ~[na:1.8.0_111]
at org.springframework.http.HttpHeaders.add(HttpHeaders.java:1425) ~[spring-web-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at com.trimble.sample.springcloudgateway.filter.AuthGatewayFilter.filter(AuthGatewayFilter.java:30) ~[classes/:na]
at org.springframework.cloud.gateway.filter.OrderedGatewayFilter.filter(OrderedGatewayFilter.java:44) ~[spring-cloud-gateway-core-2.0.0.BUILD-SNAPSHOT.jar:2.0.0.BUILD-SNAPSHOT]
at org.springframework.cloud.gateway.handler.FilteringWebHandler$DefaultGatewayFilterChain.filter(FilteringWebHandler.java:103) ~[spring-cloud-gateway-core-2.0.0.BUILD-SNAPSHOT.jar:2.0.0.BUILD-SNAPSHOT]
at org.springframework.cloud.gateway.filter.NettyWriteResponseFilter.filter(NettyWriteResponseFilter.java:62) ~[spring-cloud-gateway-core-2.0.0.BUILD-SNAPSHOT.jar:2.0.0.BUILD-SNAPSHOT]
at org.springframework.cloud.gateway.handler.FilteringWebHandler$GatewayFilterAdapter.filter(FilteringWebHandler.java:121) ~[spring-cloud-gateway-core-2.0.0.BUILD-SNAPSHOT.jar:2.0.0.BUILD-SNAPSHOT]
at org.springframework.cloud.gateway.filter.OrderedGatewayFilter.filter(OrderedGatewayFilter.java:44) ~[spring-cloud-gateway-core-2.0.0.BUILD-SNAPSHOT.jar:2.0.0.BUILD-SNAPSHOT]
at org.springframework.cloud.gateway.handler.FilteringWebHandler$DefaultGatewayFilterChain.filter(FilteringWebHandler.java:103) ~[spring-cloud-gateway-core-2.0.0.BUILD-SNAPSHOT.jar:2.0.0.BUILD-SNAPSHOT]
Subtract answered 15/4, 2018 at 18:21 Comment(3)
You have to call mutate on the request and then call mutate on the exchangeGrounder
Called the mutate methods as below: ServerHttpRequest request = exchange.getRequest() .mutate() .header("headerkey", jwt) .build(); exchange.mutate().request(request).build(); return chain.filter(exchange); However, the header is not injected to the backend api. Am I doing it wrong?Subtract
I don't know enough information to sayGrounder
S
20

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);
Subtract answered 16/4, 2018 at 11:33 Comment(0)
M
15

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());
        };
    }
}
Maki answered 3/8, 2018 at 6:40 Comment(0)
B
0
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
    exchange.getRequest()
            .mutate()
            .headers(httpHeaders -> httpHeaders.setBearerAuth("token"));
    return chain.filter(exchange);
}
Byron answered 8/11, 2022 at 10:47 Comment(3)
Please explain why your answer worksPosehn
Because headers() with mutate method is usedByron
The explanation is not for me. The intention is that you will make your answer better by explaining it. So please shift your explanation to your answer.Posehn
Z
0

To add header, only mutating the request is enough, We dont need to mutate the exchange

exchange
  .getRequest()
  .mutate()
  .header("NEW_HEADER_KEY", "HEADER_VAL")
  .build();

return exchange;
Zymometer answered 19/1, 2024 at 8:13 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.