Skip to content

Instantly share code, notes, and snippets.

@dongfg
Created February 26, 2020 07:59
Show Gist options
  • Save dongfg/8f27cfbcb16db3d18f2419e24a36bebd to your computer and use it in GitHub Desktop.
Save dongfg/8f27cfbcb16db3d18f2419e24a36bebd to your computer and use it in GitHub Desktop.
kotlin spring restTemplate okHttp ignore ssl
@Configuration
class RestTemplateConfig {
@Bean
fun restTemplate(builder: RestTemplateBuilder): RestTemplate {
return builder.requestFactory {
val trustAllCerts: Array<TrustManager> = arrayOf(object : X509TrustManager {
override fun checkClientTrusted(chain: Array<out X509Certificate>?, authType: String?) {
}
override fun checkServerTrusted(chain: Array<out X509Certificate>?, authType: String?) {
}
override fun getAcceptedIssuers(): Array<X509Certificate> {
return arrayOf()
}
})
val sslContext = SSLContext.getInstance("SSL")
sslContext.init(null, trustAllCerts, SecureRandom())
val okHttpBuilder = OkHttpClient.Builder()
okHttpBuilder.sslSocketFactory(sslContext.socketFactory, trustAllCerts[0] as X509TrustManager)
okHttpBuilder.hostnameVerifier { _, _ -> true }
OkHttp3ClientHttpRequestFactory(okHttpBuilder.build())
}.build()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment