Updating Reactor Context in Spring Cloud Gateway

Viewed 39

In my gateway application I have row of filters (applied in specific order). In first of them LogFilter I want to update the Reactor context with userId, so it will be available in all consequent filters.

So, in current code I do:

public class LogFilter implements GlobalFilter, Ordered {

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        return chain.filter(exchange).contextWrite(
                ctx ->  ctx.put("userId", "userIdValue")
        );
    }

    @Override
    public int getOrder() {
        return Ordered.HIGHEST_PRECEDENCE;
    }
}

And sure the Context is not updated immediately in this filter, but only after first heavy call to DB that I do in one of down the flow filters.

What should be the correct way of getting what I need?

0 Answers
Related