|
9 | 9 | import org.jetbrains.annotations.NotNull; |
10 | 10 | import org.jetbrains.annotations.Nullable; |
11 | 11 |
|
| 12 | +import javax.net.ssl.HostnameVerifier; |
| 13 | +import javax.net.ssl.SSLContext; |
| 14 | +import javax.net.ssl.SSLSession; |
| 15 | +import javax.net.ssl.SSLSocketFactory; |
| 16 | +import javax.net.ssl.TrustManager; |
| 17 | +import javax.net.ssl.X509TrustManager; |
12 | 18 | import java.io.IOException; |
13 | 19 | import java.net.SocketTimeoutException; |
| 20 | +import java.security.KeyManagementException; |
| 21 | +import java.security.NoSuchAlgorithmException; |
| 22 | +import java.security.cert.CertificateException; |
| 23 | +import java.security.cert.X509Certificate; |
14 | 24 | import java.util.ArrayList; |
15 | 25 | import java.util.HashMap; |
16 | 26 | import java.util.List; |
@@ -120,12 +130,15 @@ public WebClient(String scheme, int port, boolean followRedirects) { |
120 | 130 | try { |
121 | 131 | this.scheme = scheme; |
122 | 132 | this.port = port; |
123 | | - client = new OkHttpClient.Builder() |
| 133 | + OkHttpClient.Builder builder = new OkHttpClient.Builder() |
124 | 134 | .connectTimeout(5, TimeUnit.MINUTES) |
125 | 135 | .writeTimeout(5, TimeUnit.MINUTES) |
126 | 136 | .readTimeout(5, TimeUnit.MINUTES) |
127 | | - .followRedirects(followRedirects) |
128 | | - .build(); |
| 137 | + .followRedirects(followRedirects); |
| 138 | + if (scheme.equalsIgnoreCase("https")) { |
| 139 | + configureSelfSigned(builder); |
| 140 | + } |
| 141 | + this.client = builder.build(); |
129 | 142 | header("Accept", |
130 | 143 | "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"); |
131 | 144 | } catch (Exception x) { |
@@ -261,4 +274,30 @@ public void close() { |
261 | 274 | client.dispatcher().executorService().shutdown(); |
262 | 275 | client.connectionPool().evictAll(); |
263 | 276 | } |
| 277 | + |
| 278 | + private static void configureSelfSigned(OkHttpClient.Builder builder) |
| 279 | + throws NoSuchAlgorithmException, KeyManagementException { |
| 280 | + X509TrustManager trustManager = new X509TrustManager() { |
| 281 | + @Override |
| 282 | + public X509Certificate[] getAcceptedIssuers() { |
| 283 | + return new X509Certificate[0]; |
| 284 | + } |
| 285 | + |
| 286 | + @Override |
| 287 | + public void checkServerTrusted(final X509Certificate[] chain, |
| 288 | + final String authType) throws CertificateException { |
| 289 | + } |
| 290 | + |
| 291 | + @Override |
| 292 | + public void checkClientTrusted(final X509Certificate[] chain, |
| 293 | + final String authType) throws CertificateException { |
| 294 | + } |
| 295 | + }; |
| 296 | + |
| 297 | + SSLContext sslContext = SSLContext.getInstance("SSL"); |
| 298 | + |
| 299 | + sslContext.init(null, new TrustManager[]{trustManager}, new java.security.SecureRandom()); |
| 300 | + builder.sslSocketFactory(sslContext.getSocketFactory(), trustManager); |
| 301 | + builder.hostnameVerifier((hostname, session) -> true); |
| 302 | + } |
264 | 303 | } |
0 commit comments