Spring Webflux - 如何在 WebFilter 中调用反应式端点

jgv*_*115 2 spring spring-boot spring-webflux

我想实现一个 WebFilter,它读取传入请求的特定标头,使用此标头的值将 GET 请求调用到另一个反应式 REST 端点,然后使用 GET 响应的值改变原始请求。

我想在 WebFilter 中实现它,因为我不想将此函数调用添加到我的@RestController.

目前我有这个:

@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()
}

Run Code Online (Sandbox Code Playgroud)

这显然现在行不通。我的 WebClient 正在返回 Mono,所以我不能直接在我的mutate()调用中使用它,因为这需要一个字符串。由于显而易见的原因,我也无法真正让 WebClient 调用阻塞操作。

有谁知道我如何解决这个问题?

123*_*123 6

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);
}
Run Code Online (Sandbox Code Playgroud)

您面临的问题是由于您尝试以同步方式执行此操作,而您需要在收到来自 WebClient 的响应后映射标头。