Skip to content

Commit ca815da

Browse files
author
eugenp
committed
self signed certificates with new api
1 parent c5ddb0b commit ca815da

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

spring-security-rest-basic-auth/src/test/java/org/baeldung/client/RawClientLiveTest.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,15 @@
1818
import org.apache.http.conn.ClientConnectionManager;
1919
import org.apache.http.conn.scheme.Scheme;
2020
import org.apache.http.conn.scheme.SchemeRegistry;
21+
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
22+
import org.apache.http.conn.ssl.SSLContextBuilder;
2123
import org.apache.http.conn.ssl.SSLSocketFactory;
24+
import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
2225
import org.apache.http.conn.ssl.TrustStrategy;
2326
import org.apache.http.impl.client.CloseableHttpClient;
2427
import org.apache.http.impl.client.DefaultHttpClient;
2528
import org.apache.http.impl.client.HttpClientBuilder;
29+
import org.apache.http.impl.client.HttpClients;
2630
import org.apache.http.impl.conn.PoolingClientConnectionManager;
2731
import org.junit.Ignore;
2832
import org.junit.Test;
@@ -67,7 +71,7 @@ public final void whenHttpsUrlIsConsumed_thenException() throws ClientProtocolEx
6771
}
6872

6973
@Test
70-
public final void givenAcceptingAllCertificates_whenHttpsUrlIsConsumed_thenException() throws IOException, GeneralSecurityException {
74+
public final void givenHttpClientPre4_3_whenAcceptingAllCertificates_thenCanConsumeHttpsUriWithSelfSignedCertificate() throws IOException, GeneralSecurityException {
7175
final TrustStrategy acceptingTrustStrategy = new TrustStrategy() {
7276
@Override
7377
public final boolean isTrusted(final X509Certificate[] certificate, final String authType) {
@@ -87,4 +91,19 @@ public final boolean isTrusted(final X509Certificate[] certificate, final String
8791
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
8892
}
8993

94+
@Test
95+
public final void givenHttpClientPost4_3_whenAcceptingAllCertificates_thenCanConsumeHttpsUriWithSelfSignedCertificate() throws IOException, GeneralSecurityException {
96+
final SSLContextBuilder builder = new SSLContextBuilder();
97+
builder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
98+
final SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(builder.build());
99+
final CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
100+
101+
// new
102+
103+
final String urlOverHttps = "https://localhost:8443/spring-security-rest-basic-auth/api/bars/1";
104+
final HttpGet getMethod = new HttpGet(urlOverHttps);
105+
final HttpResponse response = httpClient.execute(getMethod);
106+
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
107+
}
108+
90109
}

0 commit comments

Comments
 (0)