From 949c5d737c966e7b7cc89cbc3b45b8f437866e1f Mon Sep 17 00:00:00 2001 From: Michael Lifshits Date: Mon, 1 Nov 2021 09:34:40 -0400 Subject: [PATCH] ignore SSL cert validation --- .../capsa/it/runner/HttpRequestBuilder.kt | 26 ++++++++++++++----- 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/capsa-it/src/main/kotlin/digital/capsa/it/runner/HttpRequestBuilder.kt b/capsa-it/src/main/kotlin/digital/capsa/it/runner/HttpRequestBuilder.kt index c432d6d..5eab906 100644 --- a/capsa-it/src/main/kotlin/digital/capsa/it/runner/HttpRequestBuilder.kt +++ b/capsa-it/src/main/kotlin/digital/capsa/it/runner/HttpRequestBuilder.kt @@ -3,12 +3,12 @@ package digital.capsa.it.runner import com.fasterxml.jackson.databind.ObjectMapper import com.fasterxml.jackson.databind.node.TextNode import digital.capsa.it.json.JsonPathModifier -import java.io.BufferedReader -import java.io.InputStreamReader -import java.net.URI -import java.util.stream.Collectors import org.apache.http.HttpHost +import org.apache.http.conn.ssl.NoopHostnameVerifier +import org.apache.http.conn.ssl.SSLConnectionSocketFactory import org.apache.http.impl.client.HttpClientBuilder +import org.apache.http.impl.client.HttpClients +import org.apache.http.ssl.SSLContexts import org.springframework.http.HttpEntity import org.springframework.http.HttpHeaders import org.springframework.http.ResponseEntity @@ -17,6 +17,10 @@ import org.springframework.http.client.HttpComponentsClientHttpRequestFactory import org.springframework.stereotype.Component import org.springframework.web.client.ResponseErrorHandler import org.springframework.web.client.RestTemplate +import java.io.BufferedReader +import java.io.InputStreamReader +import java.net.URI +import java.util.stream.Collectors @Component class HttpRequestBuilder(private val objectMapper: ObjectMapper, private val requestFile: String) { @@ -75,7 +79,7 @@ class HttpRequestBuilder(private val objectMapper: ObjectMapper, private val req val response = restTemplate.exchange( URI(httpRequest.schema, null, httpRequest.host, httpRequest.port, (httpRequest.basePath?.let { "${httpRequest.basePath}" } ?: "") - + httpRequest.path, httpRequest.queryParams, null).toString(), + + httpRequest.path, httpRequest.queryParams, null).toString(), httpRequest.method, requestEntity, String::class.java) if (block != null) { @@ -91,7 +95,7 @@ class HttpRequestBuilder(private val objectMapper: ObjectMapper, private val req proxyHost: String?, proxyPort: String? ): - HttpComponentsClientHttpRequestFactory { + HttpComponentsClientHttpRequestFactory { val clientHttpRequestFactory = HttpComponentsClientHttpRequestFactory( HttpClientBuilder.create() .setProxy(proxyHost?.let { HttpHost(it, proxyPort!!.toInt(), "http") }) @@ -99,6 +103,16 @@ class HttpRequestBuilder(private val objectMapper: ObjectMapper, private val req ) clientHttpRequestFactory.setConnectTimeout(connectTimeout) clientHttpRequestFactory.setReadTimeout(readTimeout) + clientHttpRequestFactory.httpClient = HttpClients.custom() + .setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE) + .setSSLSocketFactory( + SSLConnectionSocketFactory( + SSLContexts.custom() + .loadTrustMaterial(null) { _, _ -> true } + .build(), null, null, + NoopHostnameVerifier() + ) + ).build() return clientHttpRequestFactory } }