Created
February 26, 2020 07:59
-
-
Save dongfg/8f27cfbcb16db3d18f2419e24a36bebd to your computer and use it in GitHub Desktop.
kotlin spring restTemplate okHttp ignore ssl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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