SprinBoot: Secure HTTPS Clinet with POJO
This commit is contained in:
parent
c14a5ea92c
commit
a8985e6a2b
@ -0,0 +1,6 @@
|
||||
package djmil.cordacheckers.pojo;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
public record holdingIdentity(String x500Name, String shortHash) { }
|
@ -0,0 +1,6 @@
|
||||
package djmil.cordacheckers.pojo;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
public record virtualNodes(holdingIdentity holdingIdentity) { }
|
@ -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) { }
|
@ -1 +1,3 @@
|
||||
|
||||
trust.store=classpath:keystore/truststore.p12
|
||||
trust.store.password=test123
|
BIN
backend/src/main/resources/keystore/truststore.p12
Normal file
BIN
backend/src/main/resources/keystore/truststore.p12
Normal file
Binary file not shown.
@ -1,5 +1,6 @@
|
||||
package djmil.cordacheckers;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.security.GeneralSecurityException;
|
||||
|
||||
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.RegistryBuilder;
|
||||
import org.apache.hc.core5.ssl.SSLContexts;
|
||||
import org.apache.hc.core5.ssl.TrustStrategy;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.http.HttpEntity;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
@ -25,22 +27,29 @@ import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import djmil.cordacheckers.pojo.holdingIdentity;
|
||||
import djmil.cordacheckers.pojo.virtualnode;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
|
||||
@SpringBootTest
|
||||
class CordacheckersApplicationTests {
|
||||
|
||||
@Value("${trust.store}")
|
||||
private Resource trustStore;
|
||||
|
||||
@Value("${trust.store.password}")
|
||||
private String trustStorePassword;
|
||||
|
||||
@Test
|
||||
void contextLoads() {
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenAcceptingAllCertificates_whenHttpsUrlIsConsumed_thenOk() throws GeneralSecurityException {
|
||||
|
||||
final TrustStrategy acceptingTrustStrategy = (cert, authType) -> true;
|
||||
void givenAcceptOnlyCACertificates_whenHttpsUrlIsConsumed_thenOk() throws GeneralSecurityException, IOException {
|
||||
final SSLContext sslContext = SSLContexts.custom()
|
||||
.loadTrustMaterial(null, acceptingTrustStrategy)
|
||||
.loadTrustMaterial(trustStore.getURL(), trustStorePassword.toCharArray())
|
||||
.build();
|
||||
final SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE);
|
||||
final Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory> create()
|
||||
@ -54,26 +63,34 @@ class CordacheckersApplicationTests {
|
||||
.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);
|
||||
//requestFactory.setReadTimeout(readTimeout);
|
||||
//requestFactory.setConnectTimeout(connectTimeout);
|
||||
|
||||
|
||||
// Request authorization header
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.setBasicAuth("admin", "admin");
|
||||
|
||||
// String authStr = "username:password";
|
||||
// String base64Creds = Base64.getEncoder().encodeToString(authStr.getBytes());
|
||||
// headers.add("Authorization", "Basic " + base64Creds);
|
||||
|
||||
// Request
|
||||
final HttpEntity<String> request = new HttpEntity<>(headers);
|
||||
|
||||
final ResponseEntity<virtualnode> response = new RestTemplate(requestFactory)
|
||||
.exchange("https://localhost:8888/api/v1/virtualnode", HttpMethod.GET, request, virtualnode.class);
|
||||
|
||||
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"));
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user