|
35 | 35 | * */ |
36 | 36 | public class HttpsClientSslLiveTest { |
37 | 37 |
|
38 | | - // "https://localhost:8443/spring-security-rest-basic-auth/api/bars/1" // local |
39 | | - // "https://mms.nw.ru/" // hosted |
40 | | - private static final String HOST_WITH_SSL = "https://mms.nw.ru/"; |
41 | | - |
42 | | - // tests |
43 | | - |
44 | | - @Test(expected = SSLException.class) |
45 | | - public final void whenHttpsUrlIsConsumed_thenException() throws ClientProtocolException, IOException { |
46 | | - final CloseableHttpClient httpClient = HttpClientBuilder.create().build(); |
47 | | - |
48 | | - final HttpGet getMethod = new HttpGet(HOST_WITH_SSL); |
49 | | - final HttpResponse response = httpClient.execute(getMethod); |
50 | | - assertThat(response.getStatusLine().getStatusCode(), equalTo(200)); |
51 | | - } |
52 | | - |
53 | | - @SuppressWarnings("deprecation") |
54 | | - @Test |
55 | | - public final void givenHttpClientPre4_3_whenAcceptingAllCertificates_thenCanConsumeHttpsUriWithSelfSignedCertificate() throws IOException, GeneralSecurityException { |
56 | | - final TrustStrategy acceptingTrustStrategy = new TrustStrategy() { |
57 | | - @Override |
58 | | - public final boolean isTrusted(final X509Certificate[] certificate, final String authType) { |
59 | | - return true; |
60 | | - } |
61 | | - }; |
62 | | - final SSLSocketFactory sf = new SSLSocketFactory(acceptingTrustStrategy, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); |
63 | | - final SchemeRegistry registry = new SchemeRegistry(); |
64 | | - registry.register(new Scheme("https", 443, sf)); |
65 | | - final ClientConnectionManager ccm = new PoolingClientConnectionManager(registry); |
66 | | - |
67 | | - final CloseableHttpClient httpClient = new DefaultHttpClient(ccm); |
68 | | - |
69 | | - final HttpGet getMethod = new HttpGet(HOST_WITH_SSL); |
70 | | - final HttpResponse response = httpClient.execute(getMethod); |
71 | | - assertThat(response.getStatusLine().getStatusCode(), equalTo(200)); |
72 | | - |
73 | | - httpClient.close(); |
74 | | - } |
75 | | - |
76 | | - @Test |
77 | | - public final void givenHttpClientAfter4_3_whenAcceptingAllCertificates_thenCanConsumeHttpsUriWithSelfSignedCertificate() throws IOException, GeneralSecurityException { |
78 | | - final TrustStrategy acceptingTrustStrategy = new TrustStrategy() { |
79 | | - @Override |
80 | | - public final boolean isTrusted(final X509Certificate[] certificate, final String authType) { |
81 | | - return true; |
82 | | - } |
83 | | - }; |
84 | | - final SSLContext sslContext = SSLContexts.custom() |
85 | | - .loadTrustMaterial(null, acceptingTrustStrategy).build(); |
86 | | - |
87 | | - final SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory( |
88 | | - sslContext, |
89 | | - SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); |
90 | | - |
91 | | - final CloseableHttpClient httpClient = HttpClients |
92 | | - .custom() |
93 | | - .setHostnameVerifier( |
94 | | - SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER) |
95 | | - .setSSLSocketFactory(sslsf).build(); |
96 | | - |
97 | | - |
98 | | - final HttpGet getMethod = new HttpGet(HOST_WITH_SSL); |
99 | | - final HttpResponse response = httpClient.execute(getMethod); |
100 | | - assertThat(response.getStatusLine().getStatusCode(), equalTo(200)); |
101 | | - |
102 | | - httpClient.close(); |
103 | | - } |
104 | | - |
105 | | - @Test |
106 | | - public final void givenHttpClientPost4_3_whenAcceptingAllCertificates_thenCanConsumeHttpsUriWithSelfSignedCertificate() throws IOException, GeneralSecurityException { |
107 | | - final SSLContextBuilder builder = new SSLContextBuilder(); |
108 | | - builder.loadTrustMaterial(null, new TrustSelfSignedStrategy()); |
109 | | - final SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(builder.build()); |
110 | | - final CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(sslsf).build(); |
111 | | - |
112 | | - // new |
113 | | - |
114 | | - final HttpGet getMethod = new HttpGet(HOST_WITH_SSL); |
115 | | - final HttpResponse response = httpClient.execute(getMethod); |
116 | | - assertThat(response.getStatusLine().getStatusCode(), equalTo(200)); |
117 | | - } |
| 38 | + // "https://localhost:8443/spring-security-rest-basic-auth/api/bars/1" // local |
| 39 | + // "https://mms.nw.ru/" // hosted |
| 40 | + private static final String HOST_WITH_SSL = "https://mms.nw.ru/"; |
| 41 | + |
| 42 | + // tests |
| 43 | + |
| 44 | + @Test(expected = SSLException.class) |
| 45 | + public final void whenHttpsUrlIsConsumed_thenException() throws ClientProtocolException, IOException { |
| 46 | + final CloseableHttpClient httpClient = HttpClientBuilder.create().build(); |
| 47 | + |
| 48 | + final HttpGet getMethod = new HttpGet(HOST_WITH_SSL); |
| 49 | + final HttpResponse response = httpClient.execute(getMethod); |
| 50 | + assertThat(response.getStatusLine().getStatusCode(), equalTo(200)); |
| 51 | + } |
| 52 | + |
| 53 | + @SuppressWarnings("deprecation") |
| 54 | + @Test |
| 55 | + public final void givenHttpClientPre4_3_whenAcceptingAllCertificates_thenCanConsumeHttpsUriWithSelfSignedCertificate() throws IOException, GeneralSecurityException { |
| 56 | + final TrustStrategy acceptingTrustStrategy = new TrustStrategy() { |
| 57 | + @Override |
| 58 | + public final boolean isTrusted(final X509Certificate[] certificate, final String authType) { |
| 59 | + return true; |
| 60 | + } |
| 61 | + }; |
| 62 | + final SSLSocketFactory sf = new SSLSocketFactory(acceptingTrustStrategy, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); |
| 63 | + final SchemeRegistry registry = new SchemeRegistry(); |
| 64 | + registry.register(new Scheme("https", 443, sf)); |
| 65 | + final ClientConnectionManager ccm = new PoolingClientConnectionManager(registry); |
| 66 | + |
| 67 | + final CloseableHttpClient httpClient = new DefaultHttpClient(ccm); |
| 68 | + |
| 69 | + final HttpGet getMethod = new HttpGet(HOST_WITH_SSL); |
| 70 | + final HttpResponse response = httpClient.execute(getMethod); |
| 71 | + assertThat(response.getStatusLine().getStatusCode(), equalTo(200)); |
| 72 | + |
| 73 | + httpClient.close(); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + public final void givenHttpClientAfter4_3_whenAcceptingAllCertificates_thenCanConsumeHttpsUriWithSelfSignedCertificate() throws IOException, GeneralSecurityException { |
| 78 | + final TrustStrategy acceptingTrustStrategy = new TrustStrategy() { |
| 79 | + @Override |
| 80 | + public final boolean isTrusted(final X509Certificate[] certificate, final String authType) { |
| 81 | + return true; |
| 82 | + } |
| 83 | + }; |
| 84 | + final SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(null, acceptingTrustStrategy).build(); |
| 85 | + |
| 86 | + final SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); |
| 87 | + |
| 88 | + final CloseableHttpClient httpClient = HttpClients.custom().setHostnameVerifier(SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER).setSSLSocketFactory(sslsf).build(); |
| 89 | + |
| 90 | + final HttpGet getMethod = new HttpGet(HOST_WITH_SSL); |
| 91 | + final HttpResponse response = httpClient.execute(getMethod); |
| 92 | + assertThat(response.getStatusLine().getStatusCode(), equalTo(200)); |
| 93 | + |
| 94 | + httpClient.close(); |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + public final void givenHttpClientPost4_3_whenAcceptingAllCertificates_thenCanConsumeHttpsUriWithSelfSignedCertificate() throws IOException, GeneralSecurityException { |
| 99 | + final SSLContextBuilder builder = new SSLContextBuilder(); |
| 100 | + builder.loadTrustMaterial(null, new TrustSelfSignedStrategy()); |
| 101 | + final SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(builder.build()); |
| 102 | + final CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(sslsf).build(); |
| 103 | + |
| 104 | + // new |
| 105 | + |
| 106 | + final HttpGet getMethod = new HttpGet(HOST_WITH_SSL); |
| 107 | + final HttpResponse response = httpClient.execute(getMethod); |
| 108 | + assertThat(response.getStatusLine().getStatusCode(), equalTo(200)); |
| 109 | + } |
118 | 110 |
|
119 | 111 | } |
0 commit comments