2

我正在迁移RestTemplateto的用法WebClient,以下是我现有的RestTemplate代码,

val restTemplate: RestTemplate
@Throws(KeyStoreException::class, NoSuchAlgorithmException::class, KeyManagementException::class)
get() {
    System.setProperty("https.protocols", "TLSv1,TLSv1.1,TLSv1.2")
    val sslContext = SSLContexts.custom()
        .loadTrustMaterial(null, InsecureTrustStrategy())
        .build()
    val socketFactory = SSLConnectionSocketFactory(sslContext)
    val httpClient = HttpClients.custom()
        .setSSLSocketFactory(socketFactory)
        .build()
    val requestFactory = HttpComponentsClientHttpRequestFactory(httpClient)

    return RestTemplate(requestFactory)
}

private class InsecureTrustStrategy: TrustStrategy {

    override fun isTrusted(chain: Array<out X509Certificate>?, authType: String?): Boolean = true
}

以下是我的WebClient代码

val webClient: WebClient
@Throws(KeyStoreException::class, NoSuchAlgorithmException::class, KeyManagementException::class)
get() {
    val sslContext = SslContextBuilder.forClient()
        .protocols(
            "TLSv1",
            "TLSv1.1",
            "TLSv1.2",
        )
        .trustManager(InsecureTrustManagerFactory.INSTANCE)
        .build()
    val httpClient = HttpClient.create()
        .secure {
            it.sslContext(sslContext)
        }
    val connector = ReactorClientHttpConnector(httpClient)

    return WebClient.builder()
        .clientConnector(connector)
        .build()
}

我的转换是否正确?我错过了什么吗?

4

0 回答 0