11package org .baeldung .client ;
22
3+ import static org .apache .http .conn .ssl .SSLSocketFactory .ALLOW_ALL_HOSTNAME_VERIFIER ;
34import static org .hamcrest .Matchers .equalTo ;
45import static org .junit .Assert .assertThat ;
56
67import java .io .IOException ;
78import java .security .GeneralSecurityException ;
89import java .security .cert .X509Certificate ;
9- import java .util .Timer ;
10- import java .util .TimerTask ;
1110
1211import javax .net .ssl .SSLPeerUnverifiedException ;
1312
1413import org .apache .http .HttpResponse ;
1514import org .apache .http .client .ClientProtocolException ;
16- import org .apache .http .client .config .RequestConfig ;
1715import org .apache .http .client .methods .HttpGet ;
1816import org .apache .http .conn .ClientConnectionManager ;
1917import org .apache .http .conn .scheme .Scheme ;
3028import org .apache .http .impl .conn .PoolingClientConnectionManager ;
3129import org .junit .Ignore ;
3230import org .junit .Test ;
31+ import org .springframework .http .HttpMethod ;
32+ import org .springframework .http .ResponseEntity ;
33+ import org .springframework .http .client .HttpComponentsClientHttpRequestFactory ;
34+ import org .springframework .web .client .RestTemplate ;
3335
3436/**
3537 * This test requires a localhost server over HTTPS <br>
@@ -39,30 +41,6 @@ public class HttpsClientLiveManualTest {
3941
4042 // tests
4143
42- @ Test
43- public final void whenSecuredRestApiIsConsumed_then200OK () throws ClientProtocolException , IOException {
44- final CloseableHttpClient httpClient = HttpClientBuilder .create ().build ();
45-
46- final int timeout = 30 ; // seconds
47- final RequestConfig requestConfig = RequestConfig .custom ().setConnectionRequestTimeout (timeout ).setConnectTimeout (timeout ).setSocketTimeout (timeout ).build ();
48- final HttpGet getMethod = new HttpGet ("http://localhost:8080/spring-security-rest-basic-auth/api/bars/1" );
49- getMethod .setConfig (requestConfig );
50-
51- final int hardTimeout = 10 ; // seconds
52- final TimerTask task = new TimerTask () {
53- @ Override
54- public void run () {
55- if (getMethod != null ) {
56- getMethod .abort ();
57- }
58- }
59- };
60- new Timer (true ).schedule (task , hardTimeout * 1000 );
61-
62- final HttpResponse response = httpClient .execute (getMethod );
63- System .out .println ("HTTP Status of response: " + response .getStatusLine ().getStatusCode ());
64- }
65-
6644 @ Test (expected = SSLPeerUnverifiedException .class )
6745 @ Ignore ("Only for a server that has HTTPS enabled (on 8443)" )
6846 public final void whenHttpsUrlIsConsumed_thenException () throws ClientProtocolException , IOException {
@@ -98,6 +76,25 @@ public final boolean isTrusted(final X509Certificate[] certificate, final String
9876 httpClient .close ();
9977 }
10078
79+ @ Test
80+ public final void givenAcceptingAllCertificates_whenHttpsUrlIsConsumed_thenException () throws GeneralSecurityException {
81+ final HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory ();
82+ final CloseableHttpClient httpClient = (CloseableHttpClient ) requestFactory .getHttpClient ();
83+
84+ final TrustStrategy acceptingTrustStrategy = new TrustStrategy () {
85+ @ Override
86+ public final boolean isTrusted (final X509Certificate [] certificate , final String authType ) {
87+ return true ;
88+ }
89+ };
90+ final SSLSocketFactory sf = new SSLSocketFactory (acceptingTrustStrategy , ALLOW_ALL_HOSTNAME_VERIFIER );
91+ httpClient .getConnectionManager ().getSchemeRegistry ().register (new Scheme ("https" , 8443 , sf ));
92+
93+ final String urlOverHttps = "https://localhost:8443/spring-security-rest-basic-auth/api/bars/1" ;
94+ final ResponseEntity <String > response = new RestTemplate (requestFactory ).exchange (urlOverHttps , HttpMethod .GET , null , String .class );
95+ assertThat (response .getStatusCode ().value (), equalTo (200 ));
96+ }
97+
10198 @ Test
10299 public final void givenHttpClientPost4_3_whenAcceptingAllCertificates_thenCanConsumeHttpsUriWithSelfSignedCertificate () throws IOException , GeneralSecurityException {
103100 final SSLContextBuilder builder = new SSLContextBuilder ();
0 commit comments