2

I'm migrating usage of RestTemplate to WebClient, following is my existing RestTemplate code,

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
}

Following is my WebClient code

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

Is my conversion is correct? Have I missed anything?

Vignesh
  • 3,571
  • 6
  • 28
  • 44

0 Answers0