Spring Webflux - How do I call an reactive endpoint within a WebFilter

Viewed 3214

I want to implement a WebFilter that reads a specific header of the incoming request, calls a GET request to another reactive REST endpoint with the value of this header, then mutates the original request with the value of the GET response.

I want to implement this in a WebFilter because I don't want to have to add this function call to every function in my @RestController.

Currently I have this:

@Component
class ExampleWebFilter(val webClients: WebClients) : WebFilter {
    override fun filter(exchange: ServerWebExchange, chain: WebFilterChain): Mono<Void> {
        println(exchange.request.headers)
        println(exchange.request.path)
        println(exchange.response)

        val test = webClients.internalAuthServiceClient.get()
                .uri("/api/authorisation/v1/test")
                .header("authToken", "authToken123")
                .retrieve().bodyToMono(String::class.java)

        println(test)

        exchange.mutate().request(
                exchange.request.mutate().header("newheader", test).build()
        )
        return chain.filter(exchange)
    }
}

@Component
class WebClients() {
    val internalAuthServiceClient = WebClient.builder()
            .baseUrl("lb://auth-service")
            .build()
}

This obviously doesn't work right now. My WebClient is returning Mono so I can't use this directly in my mutate() call because that requires a String. I also can't really make the WebClient call a blocking operation for obvious reasons.

Does anyone know how I could solve this problem?

1 Answers

I don't use kotlin so you will have to convert but this is how you would do it in java. I'd imagine it will be pretty much the same though.

@Override
public Mono<Void> filter(ServerWebExchange serverWebExchange,
                         WebFilterChain webFilterChain) {
    return webClients
            .internalAuthServiceClient
            .get()
            .uri("/api/authorisation/v1/test")
            .retrieve()
            .bodyToMono(String.class)
            //Gonna assume you tested the above and all works
            //If get bad response or any error really
            // then print error + return empty mono
            .onErrorResume(e -> {
                e.printStackTrace();
                return Mono.empty();
            })
            //Map the header AFTER a response
            //only maps if Mono is not empty
            .map(header -> {
                serverWebExchange
                        .getRequest()
                        .mutate()
                        .header("web-filter", header);
                return serverWebExchange;
            })
            //return unchanged serverWebExchange if empty Mono
            .defaultIfEmpty(serverWebExchange)
            //Flatmap since filter returns Mono to prevent returning Mono<Mono<void>>
            .flatMap(webFilterChain::filter);
}

The issue you are facing is due to you trying to do it in a synchronous way, whereas you need to map the header after you have received the response from the WebClient.

Related