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?