为 WebTestClient 禁用 TLS 证书检查

Disable TLS certificate check for WebTestClient

我在集成测试中使用 Spring WebFlux WebTestClient。我使用 WebTestClient.bindToServer() 创建客户端。经过测试的服务器使用 HTTPS 提供资源。我有以下代码:

WebTestClient webTestClient = WebTestClient.bindToServer()
    .baseUrl("https://secured.example.com")
    .build();
webTestClient.get().uri("/index.html")
    .exchange()
    .expectStatus().isOk();

测试中不需要使用有效证书。如何配置 WebTestClient 以信任所有证书?

P.S。我可以将 WebClient 配置为信任所有人。但是WebTestClient更适合写测试。

您可以使用可以提供给 WebTestClient 的自定义 ClientHttpConnector 配置它。

// create a custom connector and customize TLS configuration there.
ReactorClientHttpConnector connector = new ReactorClientHttpConnector(options -> options...);
WebTestClient client = WebTestClient.bindToServer(connector);

最近已在 SPR-16168 中修复此问题。