Rest client with HTTPS support

A naive implementation of HTTPS REST Client for SpringBoot
server to fetch data from Corda
This commit is contained in:
djmil 2023-08-15 22:10:48 +02:00
parent d8c2382883
commit c14a5ea92c
2 changed files with 67 additions and 0 deletions

View File

@ -19,6 +19,7 @@ dependencies {
//implementation 'org.springframework.boot:spring-boot-starter-security' //implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-webflux' 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 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'io.projectreactor:reactor-test' testImplementation 'io.projectreactor:reactor-test'
//testImplementation 'org.springframework.security:spring-security-test' //testImplementation 'org.springframework.security:spring-security-test'

View File

@ -1,7 +1,32 @@
package djmil.cordacheckers; 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.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest; 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 @SpringBootTest
class CordacheckersApplicationTests { class CordacheckersApplicationTests {
@ -10,4 +35,45 @@ class CordacheckersApplicationTests {
void contextLoads() { 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<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory> 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<String> request = new HttpEntity<>(headers);
final HttpComponentsClientHttpRequestFactory requestFactory =
new HttpComponentsClientHttpRequestFactory(httpClient);
final ResponseEntity<String> 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());
}
} }