|
4 | 4 | import org.junit.jupiter.api.Test; |
5 | 5 |
|
6 | 6 | import java.io.IOException; |
| 7 | +import java.net.Socket; |
7 | 8 | import java.net.URI; |
| 9 | +import java.net.URISyntaxException; |
8 | 10 | import java.net.http.HttpClient; |
9 | 11 | import java.net.http.HttpRequest; |
10 | 12 | import java.net.http.HttpResponse; |
| 13 | +import java.security.KeyManagementException; |
| 14 | +import java.security.NoSuchAlgorithmException; |
| 15 | +import java.security.SecureRandom; |
| 16 | +import java.security.cert.X509Certificate; |
11 | 17 | import java.util.Properties; |
12 | 18 |
|
| 19 | +import javax.net.ssl.SSLContext; |
| 20 | +import javax.net.ssl.SSLEngine; |
| 21 | +import javax.net.ssl.TrustManager; |
| 22 | +import javax.net.ssl.X509ExtendedTrustManager; |
| 23 | + |
13 | 24 | public class HttpClientSSLBypassUnitTest { |
14 | 25 |
|
15 | 26 | @Test |
16 | | - public void whenHttpsRequest_thenCorrect() throws IOException, InterruptedException { |
| 27 | + public void givenDisableUsingJVMProperty_whenByPassCertificationVerification_thenSuccessHttpResponse() throws IOException, InterruptedException { |
17 | 28 | final Properties props = System.getProperties(); |
18 | 29 | props.setProperty("jdk.internal.httpclient.disableHostnameVerification", Boolean.TRUE.toString()); |
19 | 30 |
|
20 | 31 | HttpClient httpClient = HttpClient.newBuilder() |
21 | | - .build(); |
| 32 | + .build(); |
22 | 33 |
|
23 | 34 | HttpRequest request = HttpRequest.newBuilder() |
24 | | - .uri(URI.create("https://wrong.host.badssl.com/")) |
25 | | - .build(); |
| 35 | + .uri(URI.create("https://wrong.host.badssl.com/")) |
| 36 | + .build(); |
26 | 37 |
|
27 | 38 | HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString()); |
28 | 39 | props.setProperty("jdk.internal.httpclient.disableHostnameVerification", Boolean.FALSE.toString()); |
29 | 40 |
|
30 | 41 | Assertions.assertEquals(200, response.statusCode()); |
31 | 42 | } |
| 43 | + |
| 44 | + @Test |
| 45 | + public void givenMockTrustManager_whenByPassCertificateVerification_thenSuccessHttpResponse() throws IOException, InterruptedException, NoSuchAlgorithmException, KeyManagementException, URISyntaxException { |
| 46 | + SSLContext sslContext = SSLContext.getInstance("SSL"); // OR TLS |
| 47 | + sslContext.init(null, new TrustManager[]{ MOCK_TRUST_MANAGER }, new SecureRandom()); |
| 48 | + HttpClient httpClient = HttpClient.newBuilder().sslContext(sslContext).build(); |
| 49 | + HttpRequest request = HttpRequest.newBuilder() |
| 50 | + .uri(new URI("https://wrong.host.badssl.com/")) |
| 51 | + .build(); |
| 52 | + HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString()); |
| 53 | + Assertions.assertEquals(200, response.statusCode()); |
| 54 | + } |
| 55 | + |
| 56 | + |
| 57 | + private static final TrustManager MOCK_TRUST_MANAGER = new X509ExtendedTrustManager() { |
| 58 | + @Override |
| 59 | + public java.security.cert.X509Certificate[] getAcceptedIssuers() { |
| 60 | + return new java.security.cert.X509Certificate[0]; |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) { |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) { |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public void checkServerTrusted(X509Certificate[] chain, String authType, SSLEngine engine) { |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public void checkServerTrusted(X509Certificate[] chain, String authType, Socket socket) { |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + public void checkClientTrusted(X509Certificate[] chain, String authType, SSLEngine engine) { |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public void checkClientTrusted(X509Certificate[] chain, String authType, Socket socket) { |
| 85 | + } |
| 86 | + }; |
32 | 87 | } |
0 commit comments