11package us .codecraft .webmagic .downloader ;
22
3- import java .io .IOException ;
4- import java .security .KeyManagementException ;
5- import java .security .NoSuchAlgorithmException ;
6- import java .security .cert .CertificateException ;
7- import java .security .cert .X509Certificate ;
8- import java .util .Map ;
9-
10- import javax .net .ssl .SSLContext ;
11- import javax .net .ssl .TrustManager ;
12- import javax .net .ssl .X509TrustManager ;
13-
143import org .apache .commons .lang3 .JavaVersion ;
154import org .apache .commons .lang3 .SystemUtils ;
165import org .apache .http .HttpException ;
2211import org .apache .http .config .SocketConfig ;
2312import org .apache .http .conn .socket .ConnectionSocketFactory ;
2413import org .apache .http .conn .socket .PlainConnectionSocketFactory ;
25- import org .apache .http .conn .ssl .DefaultHostnameVerifier ;
2614import org .apache .http .conn .ssl .SSLConnectionSocketFactory ;
27- import org .apache .http .impl .client .BasicCookieStore ;
28- import org .apache .http .impl .client .CloseableHttpClient ;
29- import org .apache .http .impl .client .DefaultHttpRequestRetryHandler ;
30- import org .apache .http .impl .client .HttpClientBuilder ;
31- import org .apache .http .impl .client .HttpClients ;
15+ import org .apache .http .impl .client .*;
3216import org .apache .http .impl .conn .PoolingHttpClientConnectionManager ;
3317import org .apache .http .impl .cookie .BasicClientCookie ;
3418import org .apache .http .protocol .HttpContext ;
3519import org .slf4j .Logger ;
3620import org .slf4j .LoggerFactory ;
37-
3821import us .codecraft .webmagic .Site ;
3922
23+ import javax .net .ssl .SSLContext ;
24+ import javax .net .ssl .TrustManager ;
25+ import javax .net .ssl .X509TrustManager ;
26+ import java .io .IOException ;
27+ import java .security .KeyManagementException ;
28+ import java .security .NoSuchAlgorithmException ;
29+ import java .security .cert .CertificateException ;
30+ import java .security .cert .X509Certificate ;
31+ import java .util .Map ;
32+
4033/**
4134 * @author code4crafter@gmail.com <br>
4235 * @since 0.4.0
4336 */
4437public class HttpClientGenerator {
4538
46- private transient Logger logger = LoggerFactory .getLogger (getClass ());
39+ private transient Logger logger = LoggerFactory .getLogger (getClass ());
4740
4841 private PoolingHttpClientConnectionManager connectionManager ;
4942
@@ -61,21 +54,20 @@ private SSLConnectionSocketFactory buildSSLConnectionSocketFactory() {
6154 SSLContext sslContext = createIgnoreVerifySSL ();
6255 String [] supportedProtocols ;
6356 if (SystemUtils .isJavaVersionAtLeast (JavaVersion .JAVA_11 )) {
64- supportedProtocols = new String [] { "SSLv3" , "TLSv1" , "TLSv1.1" , "TLSv1.2" , "TLSv1.3" };
57+ supportedProtocols = new String []{ "SSLv3" , "TLSv1" , "TLSv1.1" , "TLSv1.2" , "TLSv1.3" };
6558 } else {
66- supportedProtocols = new String [] { "SSLv3" , "TLSv1" , "TLSv1.1" , "TLSv1.2" };
59+ supportedProtocols = new String []{ "SSLv3" , "TLSv1" , "TLSv1.1" , "TLSv1.2" };
6760 }
6861 logger .debug ("supportedProtocols: {}" , String .join (", " , supportedProtocols ));
6962 return new SSLConnectionSocketFactory (sslContext , supportedProtocols ,
7063 null ,
71- new DefaultHostnameVerifier ()); // 优先绕过安全证书
72- } catch (KeyManagementException e ) {
73- logger .error ("ssl connection fail" , e );
74- } catch (NoSuchAlgorithmException e ) {
64+ //不进行主机校验
65+ (host , sslSession ) -> true ); // 优先绕过安全证书
66+ } catch (KeyManagementException | NoSuchAlgorithmException e ) {
7567 logger .error ("ssl connection fail" , e );
7668 }
7769 return SSLConnectionSocketFactory .getSocketFactory ();
78- }
70+ }
7971
8072 private SSLContext createIgnoreVerifySSL () throws NoSuchAlgorithmException , KeyManagementException {
8173 // 实现一个X509TrustManager接口,用于绕过验证,不用修改里面的方法
@@ -97,9 +89,9 @@ public X509Certificate[] getAcceptedIssuers() {
9789 };
9890
9991 SSLContext sc = SSLContext .getInstance ("TLS" );
100- sc .init (null , new TrustManager [] { trustManager }, null );
92+ sc .init (null , new TrustManager []{ trustManager }, null );
10193 return sc ;
102- }
94+ }
10395
10496 public HttpClientGenerator setPoolSize (int poolSize ) {
10597 connectionManager .setMaxTotal (poolSize );
0 commit comments