SprinBoot: Secure HTTPS Clinet with POJO

This commit is contained in:
djmil 2023-08-17 21:12:59 +02:00
parent c14a5ea92c
commit a8985e6a2b
6 changed files with 60 additions and 21 deletions

View File

@ -0,0 +1,6 @@
package djmil.cordacheckers.pojo;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@JsonIgnoreProperties(ignoreUnknown = true)
public record holdingIdentity(String x500Name, String shortHash) { }

View File

@ -0,0 +1,6 @@
package djmil.cordacheckers.pojo;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@JsonIgnoreProperties(ignoreUnknown = true)
public record virtualNodes(holdingIdentity holdingIdentity) { }

View File

@ -0,0 +1,8 @@
package djmil.cordacheckers.pojo;
import java.util.List;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@JsonIgnoreProperties(ignoreUnknown = true)
public record virtualnode(List<virtualNodes> virtualNodes) { }

View File

@ -1 +1,3 @@
trust.store=classpath:keystore/truststore.p12
trust.store.password=test123

Binary file not shown.

View File

@ -1,5 +1,6 @@
package djmil.cordacheckers; package djmil.cordacheckers;
import java.io.IOException;
import java.security.GeneralSecurityException; import java.security.GeneralSecurityException;
import javax.net.ssl.SSLContext; import javax.net.ssl.SSLContext;
@ -14,9 +15,10 @@ import org.apache.hc.client5.http.ssl.SSLConnectionSocketFactory;
import org.apache.hc.core5.http.config.Registry; import org.apache.hc.core5.http.config.Registry;
import org.apache.hc.core5.http.config.RegistryBuilder; import org.apache.hc.core5.http.config.RegistryBuilder;
import org.apache.hc.core5.ssl.SSLContexts; 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.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpEntity; import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod; import org.springframework.http.HttpMethod;
@ -25,22 +27,29 @@ import org.springframework.http.ResponseEntity;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
import djmil.cordacheckers.pojo.holdingIdentity;
import djmil.cordacheckers.pojo.virtualnode;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
@SpringBootTest @SpringBootTest
class CordacheckersApplicationTests { class CordacheckersApplicationTests {
@Value("${trust.store}")
private Resource trustStore;
@Value("${trust.store.password}")
private String trustStorePassword;
@Test @Test
void contextLoads() { void contextLoads() {
} }
@Test @Test
void givenAcceptingAllCertificates_whenHttpsUrlIsConsumed_thenOk() throws GeneralSecurityException { void givenAcceptOnlyCACertificates_whenHttpsUrlIsConsumed_thenOk() throws GeneralSecurityException, IOException {
final TrustStrategy acceptingTrustStrategy = (cert, authType) -> true;
final SSLContext sslContext = SSLContexts.custom() final SSLContext sslContext = SSLContexts.custom()
.loadTrustMaterial(null, acceptingTrustStrategy) .loadTrustMaterial(trustStore.getURL(), trustStorePassword.toCharArray())
.build(); .build();
final SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE); final SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE);
final Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory> create() final Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory> create()
@ -54,8 +63,13 @@ class CordacheckersApplicationTests {
.setConnectionManager(connectionManager) .setConnectionManager(connectionManager)
.build(); .build();
final HttpComponentsClientHttpRequestFactory requestFactory =
new HttpComponentsClientHttpRequestFactory(httpClient);
//requestFactory.setReadTimeout(readTimeout);
//requestFactory.setConnectTimeout(connectTimeout);
// create headers
// Request authorization header
HttpHeaders headers = new HttpHeaders(); HttpHeaders headers = new HttpHeaders();
headers.setBasicAuth("admin", "admin"); headers.setBasicAuth("admin", "admin");
@ -63,17 +77,20 @@ class CordacheckersApplicationTests {
// String base64Creds = Base64.getEncoder().encodeToString(authStr.getBytes()); // String base64Creds = Base64.getEncoder().encodeToString(authStr.getBytes());
// headers.add("Authorization", "Basic " + base64Creds); // headers.add("Authorization", "Basic " + base64Creds);
// create request // Request
final HttpEntity<String> request = new HttpEntity<>(headers); final HttpEntity<String> request = new HttpEntity<>(headers);
final ResponseEntity<virtualnode> response = new RestTemplate(requestFactory)
final HttpComponentsClientHttpRequestFactory requestFactory = .exchange("https://localhost:8888/api/v1/virtualnode", HttpMethod.GET, request, virtualnode.class);
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); assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(response.hasBody());
System.out.println("UNSWER: " + response.getBody()); virtualnode vNode = response.getBody();
assertThat(vNode != null);
assertThat(vNode.virtualNodes().size() == 5);
holdingIdentity identity = vNode.virtualNodes().get(0).holdingIdentity();
assertThat(identity.x500Name().contains("NotaryRep1"));
} }
} }