|
|
|
@ -34,9 +34,9 @@ import java.util.Map;
|
|
|
|
|
* @since 0.4.0
|
|
|
|
|
*/
|
|
|
|
|
public class HttpClientGenerator {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private transient Logger logger = LoggerFactory.getLogger(getClass());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private PoolingHttpClientConnectionManager connectionManager;
|
|
|
|
|
|
|
|
|
|
public HttpClientGenerator() {
|
|
|
|
@ -48,43 +48,43 @@ public class HttpClientGenerator {
|
|
|
|
|
connectionManager.setDefaultMaxPerRoute(100);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private SSLConnectionSocketFactory buildSSLConnectionSocketFactory() {
|
|
|
|
|
try {
|
|
|
|
|
private SSLConnectionSocketFactory buildSSLConnectionSocketFactory() {
|
|
|
|
|
try {
|
|
|
|
|
return new SSLConnectionSocketFactory(createIgnoreVerifySSL(), new String[]{"SSLv3", "TLSv1", "TLSv1.1", "TLSv1.2", "TLSv1.3"},
|
|
|
|
|
null,
|
|
|
|
|
new DefaultHostnameVerifier()); // 优先绕过安全证书
|
|
|
|
|
} catch (KeyManagementException e) {
|
|
|
|
|
} catch (KeyManagementException e) {
|
|
|
|
|
logger.error("ssl connection fail", e);
|
|
|
|
|
} catch (NoSuchAlgorithmException e) {
|
|
|
|
|
logger.error("ssl connection fail", e);
|
|
|
|
|
}
|
|
|
|
|
return SSLConnectionSocketFactory.getSocketFactory();
|
|
|
|
|
return SSLConnectionSocketFactory.getSocketFactory();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private SSLContext createIgnoreVerifySSL() throws NoSuchAlgorithmException, KeyManagementException {
|
|
|
|
|
// 实现一个X509TrustManager接口,用于绕过验证,不用修改里面的方法
|
|
|
|
|
X509TrustManager trustManager = new X509TrustManager() {
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public X509Certificate[] getAcceptedIssuers() {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
SSLContext sc = SSLContext.getInstance("SSLv3");
|
|
|
|
|
sc.init(null, new TrustManager[] { trustManager }, null);
|
|
|
|
|
return sc;
|
|
|
|
|
private SSLContext createIgnoreVerifySSL() throws NoSuchAlgorithmException, KeyManagementException {
|
|
|
|
|
// 实现一个X509TrustManager接口,用于绕过验证,不用修改里面的方法
|
|
|
|
|
X509TrustManager trustManager = new X509TrustManager() {
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public X509Certificate[] getAcceptedIssuers() {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
SSLContext sc = SSLContext.getInstance("SSLv3");
|
|
|
|
|
sc.init(null, new TrustManager[] { trustManager }, null);
|
|
|
|
|
return sc;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public HttpClientGenerator setPoolSize(int poolSize) {
|
|
|
|
|
connectionManager.setMaxTotal(poolSize);
|
|
|
|
|
return this;
|
|
|
|
@ -96,7 +96,7 @@ public class HttpClientGenerator {
|
|
|
|
|
|
|
|
|
|
private CloseableHttpClient generateClient(Site site) {
|
|
|
|
|
HttpClientBuilder httpClientBuilder = HttpClients.custom();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
httpClientBuilder.setConnectionManager(connectionManager);
|
|
|
|
|
if (site.getUserAgent() != null) {
|
|
|
|
|
httpClientBuilder.setUserAgent(site.getUserAgent());
|
|
|
|
|