作者:G眯眼猫2850927647Ona | 来源:互联网 | 2023-09-03 17:53
切勿在生产中使用此TrustManagerFactory。它仅用于测试目的,因此非常不安全。
@Bean
public WebClient createWebClient() throws SSLException {
SslContext sslCOntext= SslContextBuilder
.forClient()
.trustManager(InsecureTrustManagerFactory.INSTANCE)
.build();
ClientHttpConnector httpCOnnector= HttpClient.create().secure { t -> t.sslContext(sslContext) }
return WebClient.builder().clientConnector(httpConnector).build();
}
像Spring 5.1.1长相(spring启动2.1.0)移除HttpClientOptions
从ReactorClientHttpConnector
,所以你不能配置选项在创建实例ReactorClientHttpConnector
现在有效的一种选择是:
val sslCOntext= SslContextBuilder
.forClient()
.trustManager(InsecureTrustManagerFactory.INSTANCE)
.build()
val httpClient = HttpClient.create().secure { t -> t.sslContext(sslContext) }
val webClient = WebClient.builder().clientConnector(ReactorClientHttpConnector(httpClient)).build()
基本上,在创建HttpClient时,我们将配置不安全的 sslContext,然后将该httpClient传递给ReactorClientHttpConnector
全局使用。
另一个选项是TcpClient
使用不安全的sslContext 进行配置并使用它来创建HttpClient实例,如下所示:
val sslCOntext= SslContextBuilder
.forClient()
.trustManager(InsecureTrustManagerFactory.INSTANCE)
.build()
val tcpClient = TcpClient.create().secure { sslProviderBuilder -> sslProviderBuilder.sslContext(sslContext) }
val httpClient = HttpClient.from(tcpClient)
val webClient = WebClient.builder().clientConnector(ReactorClientHttpConnector(httpClient)).build()