1818import org .apache .http .conn .ClientConnectionManager ;
1919import org .apache .http .conn .scheme .Scheme ;
2020import org .apache .http .conn .scheme .SchemeRegistry ;
21+ import org .apache .http .conn .ssl .SSLConnectionSocketFactory ;
22+ import org .apache .http .conn .ssl .SSLContextBuilder ;
2123import org .apache .http .conn .ssl .SSLSocketFactory ;
24+ import org .apache .http .conn .ssl .TrustSelfSignedStrategy ;
2225import org .apache .http .conn .ssl .TrustStrategy ;
2326import org .apache .http .impl .client .CloseableHttpClient ;
2427import org .apache .http .impl .client .DefaultHttpClient ;
2528import org .apache .http .impl .client .HttpClientBuilder ;
29+ import org .apache .http .impl .client .HttpClients ;
2630import org .apache .http .impl .conn .PoolingClientConnectionManager ;
2731import org .junit .Ignore ;
2832import 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