14
14
import org .apache .http .conn .socket .ConnectionSocketFactory ;
15
15
import org .apache .http .conn .socket .PlainConnectionSocketFactory ;
16
16
import org .apache .http .conn .ssl .SSLConnectionSocketFactory ;
17
+ import org .apache .http .conn .ssl .TrustStrategy ;
17
18
import org .apache .http .impl .client .BasicCredentialsProvider ;
18
19
import org .apache .http .impl .client .CloseableHttpClient ;
19
20
import org .apache .http .impl .client .HttpClientBuilder ;
20
21
import org .apache .http .impl .client .HttpClients ;
21
22
import org .apache .http .impl .conn .PoolingHttpClientConnectionManager ;
22
23
import org .apache .http .protocol .HttpContext ;
24
+ import org .apache .http .ssl .SSLContexts ;
23
25
import org .slf4j .Logger ;
24
26
import org .slf4j .LoggerFactory ;
25
27
28
+ import javax .net .ssl .SSLContext ;
26
29
import java .io .IOException ;
30
+ import java .security .KeyManagementException ;
31
+ import java .security .KeyStoreException ;
32
+ import java .security .NoSuchAlgorithmException ;
33
+ import java .security .cert .CertificateException ;
34
+ import java .security .cert .X509Certificate ;
27
35
import java .util .concurrent .TimeUnit ;
28
36
import java .util .concurrent .atomic .AtomicBoolean ;
29
37
@@ -214,6 +222,7 @@ private synchronized void prepare() {
214
222
this .httpClientBuilder = HttpClients .custom ()
215
223
.setConnectionManager (connectionManager )
216
224
.setConnectionManagerShared (true )
225
+ .setSSLSocketFactory (this .buildSSLConnectionSocketFactory ())
217
226
.setDefaultRequestConfig (
218
227
RequestConfig .custom ()
219
228
.setSocketTimeout (this .soTimeout )
@@ -240,6 +249,29 @@ private synchronized void prepare() {
240
249
prepared .set (true );
241
250
}
242
251
252
+ private SSLConnectionSocketFactory buildSSLConnectionSocketFactory () {
253
+ try {
254
+ SSLContext sslcontext = SSLContexts .custom ()
255
+ //忽略掉对服务器端证书的校验
256
+ .loadTrustMaterial (new TrustStrategy () {
257
+ @ Override
258
+ public boolean isTrusted (X509Certificate [] chain , String authType ) throws CertificateException {
259
+ return true ;
260
+ }
261
+ }).build ();
262
+
263
+ return new SSLConnectionSocketFactory (
264
+ sslcontext ,
265
+ new String []{"TLSv1" },
266
+ null ,
267
+ SSLConnectionSocketFactory .getDefaultHostnameVerifier ());
268
+ } catch (NoSuchAlgorithmException | KeyManagementException | KeyStoreException e ) {
269
+ this .log .error (e .getMessage (), e );
270
+ }
271
+
272
+ return null ;
273
+ }
274
+
243
275
@ Override
244
276
public CloseableHttpClient build () {
245
277
if (!prepared .get ()) {
0 commit comments