From c14a5ea92ce134163d4150424f58658db5f41cf9 Mon Sep 17 00:00:00 2001 From: djmil Date: Tue, 15 Aug 2023 22:10:48 +0200 Subject: [PATCH] Rest client with HTTPS support A naive implementation of HTTPS REST Client for SpringBoot server to fetch data from Corda --- backend/build.gradle | 1 + .../CordacheckersApplicationTests.java | 66 +++++++++++++++++++ 2 files changed, 67 insertions(+) diff --git a/backend/build.gradle b/backend/build.gradle index 99b7994..08a0fb9 100644 --- a/backend/build.gradle +++ b/backend/build.gradle @@ -19,6 +19,7 @@ dependencies { //implementation 'org.springframework.boot:spring-boot-starter-security' implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.springframework.boot:spring-boot-starter-webflux' + testImplementation 'org.apache.httpcomponents.client5:httpclient5:5.2.1' testImplementation 'org.springframework.boot:spring-boot-starter-test' testImplementation 'io.projectreactor:reactor-test' //testImplementation 'org.springframework.security:spring-security-test' diff --git a/backend/src/test/java/djmil/cordacheckers/CordacheckersApplicationTests.java b/backend/src/test/java/djmil/cordacheckers/CordacheckersApplicationTests.java index afb8c33..f434f23 100644 --- a/backend/src/test/java/djmil/cordacheckers/CordacheckersApplicationTests.java +++ b/backend/src/test/java/djmil/cordacheckers/CordacheckersApplicationTests.java @@ -1,7 +1,32 @@ package djmil.cordacheckers; +import java.security.GeneralSecurityException; + +import javax.net.ssl.SSLContext; + +import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; +import org.apache.hc.client5.http.impl.classic.HttpClients; +import org.apache.hc.client5.http.impl.io.BasicHttpClientConnectionManager; +import org.apache.hc.client5.http.socket.ConnectionSocketFactory; +import org.apache.hc.client5.http.socket.PlainConnectionSocketFactory; +import org.apache.hc.client5.http.ssl.NoopHostnameVerifier; +import org.apache.hc.client5.http.ssl.SSLConnectionSocketFactory; +import org.apache.hc.core5.http.config.Registry; +import org.apache.hc.core5.http.config.RegistryBuilder; +import org.apache.hc.core5.ssl.SSLContexts; +import org.apache.hc.core5.ssl.TrustStrategy; import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpMethod; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; +import org.springframework.web.client.RestTemplate; + +import static org.assertj.core.api.Assertions.assertThat; + @SpringBootTest class CordacheckersApplicationTests { @@ -10,4 +35,45 @@ class CordacheckersApplicationTests { void contextLoads() { } + @Test + void givenAcceptingAllCertificates_whenHttpsUrlIsConsumed_thenOk() throws GeneralSecurityException { + + final TrustStrategy acceptingTrustStrategy = (cert, authType) -> true; + final SSLContext sslContext = SSLContexts.custom() + .loadTrustMaterial(null, acceptingTrustStrategy) + .build(); + final SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE); + final Registry socketFactoryRegistry = RegistryBuilder. create() + .register("https", sslsf) + .register("http", new PlainConnectionSocketFactory()) + .build(); + + final BasicHttpClientConnectionManager connectionManager = + new BasicHttpClientConnectionManager(socketFactoryRegistry); + final CloseableHttpClient httpClient = HttpClients.custom() + .setConnectionManager(connectionManager) + .build(); + + + // create headers + HttpHeaders headers = new HttpHeaders(); + headers.setBasicAuth("admin", "admin"); + + // String authStr = "username:password"; + // String base64Creds = Base64.getEncoder().encodeToString(authStr.getBytes()); + // headers.add("Authorization", "Basic " + base64Creds); + + // create request + final HttpEntity request = new HttpEntity<>(headers); + + + final HttpComponentsClientHttpRequestFactory requestFactory = + new HttpComponentsClientHttpRequestFactory(httpClient); + final ResponseEntity response = new RestTemplate(requestFactory) + .exchange("https://localhost:8888/api/v1/virtualnode", HttpMethod.GET, request, String.class); + + assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); + + System.out.println("UNSWER: " + response.getBody()); + } }