1
1
package us .codecraft .webmagic .downloader ;
2
2
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
-
14
3
import org .apache .commons .lang3 .JavaVersion ;
15
4
import org .apache .commons .lang3 .SystemUtils ;
16
5
import org .apache .http .HttpException ;
22
11
import org .apache .http .config .SocketConfig ;
23
12
import org .apache .http .conn .socket .ConnectionSocketFactory ;
24
13
import org .apache .http .conn .socket .PlainConnectionSocketFactory ;
25
- import org .apache .http .conn .ssl .DefaultHostnameVerifier ;
26
14
import 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 .*;
32
16
import org .apache .http .impl .conn .PoolingHttpClientConnectionManager ;
33
17
import org .apache .http .impl .cookie .BasicClientCookie ;
34
18
import org .apache .http .protocol .HttpContext ;
35
19
import org .slf4j .Logger ;
36
20
import org .slf4j .LoggerFactory ;
37
-
38
21
import us .codecraft .webmagic .Site ;
39
22
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
+
40
33
/**
41
34
42
35
* @since 0.4.0
43
36
*/
44
37
public class HttpClientGenerator {
45
38
46
- private transient Logger logger = LoggerFactory .getLogger (getClass ());
39
+ private transient Logger logger = LoggerFactory .getLogger (getClass ());
47
40
48
41
private PoolingHttpClientConnectionManager connectionManager ;
49
42
@@ -61,21 +54,20 @@ private SSLConnectionSocketFactory buildSSLConnectionSocketFactory() {
61
54
SSLContext sslContext = createIgnoreVerifySSL ();
62
55
String [] supportedProtocols ;
63
56
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" };
65
58
} else {
66
- supportedProtocols = new String [] { "SSLv3" , "TLSv1" , "TLSv1.1" , "TLSv1.2" };
59
+ supportedProtocols = new String []{ "SSLv3" , "TLSv1" , "TLSv1.1" , "TLSv1.2" };
67
60
}
68
61
logger .debug ("supportedProtocols: {}" , String .join (", " , supportedProtocols ));
69
62
return new SSLConnectionSocketFactory (sslContext , supportedProtocols ,
70
63
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 ) {
75
67
logger .error ("ssl connection fail" , e );
76
68
}
77
69
return SSLConnectionSocketFactory .getSocketFactory ();
78
- }
70
+ }
79
71
80
72
private SSLContext createIgnoreVerifySSL () throws NoSuchAlgorithmException , KeyManagementException {
81
73
// 实现一个X509TrustManager接口,用于绕过验证,不用修改里面的方法
@@ -97,9 +89,9 @@ public X509Certificate[] getAcceptedIssuers() {
97
89
};
98
90
99
91
SSLContext sc = SSLContext .getInstance ("TLS" );
100
- sc .init (null , new TrustManager [] { trustManager }, null );
92
+ sc .init (null , new TrustManager []{ trustManager }, null );
101
93
return sc ;
102
- }
94
+ }
103
95
104
96
public HttpClientGenerator setPoolSize (int poolSize ) {
105
97
connectionManager .setMaxTotal (poolSize );
0 commit comments