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:
parent
d8c2382883
commit
c14a5ea92c
@ -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'
|
||||
|
@ -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<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());
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user