2

I am trying to add the dynamic value for all the request header before passing the request to downstream.

I have created a user defined class that implements org.springframework.cloud.gateway.filter.GlobalFilter

In this class, I have to call other services (2 reactive redis and 1 api call) to retrieve the dynamic value which needs to be set in header.

Here is the problem I am facing right now,

@Override
  public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
    return chain.filter(exchange);
  }

GatewayFilterChain.filter(exchange) subscription got completed before other mono subscriptions (2 redis and 1 service) got completed. Hence before I mutate the exchange, the request without adding the header passes to the downstream.

exchange.getRequest().mutate()
        .header(HttpHeaders.AUTHORIZATION, "Bearer ".concat(token))
        .build()

To avoid this issue, I have added the delay in mono subscription

chain.filter(exchange).delaySubscription(Duration.ofMillis(300L))

But I don't want to introduce manual delay in system, can you please guide/suggestion me the better solution so that GatewayFilterChain should not get subscribed before other mono subscription completes.

0 Answers0