use of org.apache.http.conn.ssl.SSLConnectionSocketFactory in project rdf4j by eclipse.
the class HttpClientBuilders method getSSLTrustAllHttpClientBuilder.
/**
* Return an {@link HttpClientBuilder} that can be used to build an {@link HttpClient} which trusts all
* certificates (particularly including self-signed certificates).
*
* @return a {@link HttpClientBuilder} for <i>SSL trust all</i>
*/
public static HttpClientBuilder getSSLTrustAllHttpClientBuilder() {
try {
SSLContextBuilder builder = new SSLContextBuilder();
builder.loadTrustMaterial(null, new TrustStrategy() {
@Override
public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
return true;
}
});
HostnameVerifier hostNameVerifier = new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
return true;
}
};
SSLConnectionSocketFactory sslSF = new SSLConnectionSocketFactory(builder.build(), hostNameVerifier);
return HttpClients.custom().setSSLSocketFactory(sslSF).useSystemProperties();
} catch (Exception e) {
// key management exception, etc.
throw new RuntimeException(e);
}
}
use of org.apache.http.conn.ssl.SSLConnectionSocketFactory in project canal by alibaba.
the class HttpHelper method getIgnoreCerf.
private static String getIgnoreCerf(String url, CookieStore cookieStore, Map<String, String> params, int timeout) {
long start = System.currentTimeMillis();
HttpClientBuilder builder = HttpClientBuilder.create();
builder.setMaxConnPerRoute(50);
builder.setMaxConnTotal(100);
HttpGet httpGet = null;
CloseableHttpResponse response = null;
try {
// 创建支持忽略证书的https
final SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, (x509Certificates, s) -> true).build();
CloseableHttpClient httpClient = HttpClientBuilder.create().setSSLContext(sslContext).setConnectionManager(new PoolingHttpClientConnectionManager(RegistryBuilder.<ConnectionSocketFactory>create().register("http", PlainConnectionSocketFactory.INSTANCE).register("https", new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE)).build())).build();
// ---------------- 创建支持https 的client成功---------
URI uri = new URIBuilder(url).build();
RequestConfig config = custom().setConnectTimeout(timeout).setConnectionRequestTimeout(timeout).setSocketTimeout(timeout).build();
httpGet = new HttpGet(uri);
HttpClientContext context = HttpClientContext.create();
context.setRequestConfig(config);
response = httpClient.execute(httpGet, context);
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == HttpStatus.SC_OK) {
return EntityUtils.toString(response.getEntity());
} else {
String errorMsg = EntityUtils.toString(response.getEntity());
throw new RuntimeException("requestGet remote error, url=" + uri.toString() + ", code=" + statusCode + ", error msg=" + errorMsg);
}
} catch (Throwable t) {
long end = System.currentTimeMillis();
long cost = end - start;
String curlRequest = getCurlRequest(url, cookieStore, params, cost);
throw new RuntimeException("requestPost(Https) remote error, request : " + curlRequest, t);
} finally {
long end = System.currentTimeMillis();
long cost = end - start;
printCurlRequest(url, null, null, cost);
if (response != null) {
try {
response.close();
} catch (IOException e) {
}
}
if (httpGet != null) {
httpGet.releaseConnection();
}
}
}
use of org.apache.http.conn.ssl.SSLConnectionSocketFactory in project canal by alibaba.
the class AbstractRequest method executeHttpRequest.
/**
* 执行http请求
*
* @param getMethod
* @return
* @throws IOException
*/
@SuppressWarnings("deprecation")
private final HttpResponse executeHttpRequest(HttpGet getMethod, String host) throws Exception {
SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(null, (TrustStrategy) (arg0, arg1) -> true).build();
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, new String[] { "TLSv1" }, null, SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
Registry registry = RegistryBuilder.create().register("http", PlainConnectionSocketFactory.INSTANCE).register("https", sslsf).build();
HttpClientConnectionManager httpClientConnectionManager = new PoolingHttpClientConnectionManager(registry);
CloseableHttpClient httpClient = HttpClientBuilder.create().setMaxConnPerRoute(50).setMaxConnTotal(100).setConnectionManager(httpClientConnectionManager).build();
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(timeout).setConnectionRequestTimeout(timeout).setSocketTimeout(timeout).build();
getMethod.setConfig(requestConfig);
HttpResponse response = httpClient.execute(getMethod);
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode != HttpResponseStatus.OK.code() && statusCode != HttpResponseStatus.PARTIAL_CONTENT.code()) {
String result = EntityUtils.toString(response.getEntity());
throw new RuntimeException("return error !" + response.getStatusLine().getReasonPhrase() + ", " + result);
}
return response;
}
use of org.apache.http.conn.ssl.SSLConnectionSocketFactory in project java-chassis by ServiceComb.
the class HttpTransportFactory method getPoolingHttpClientConnectionManager.
private static PoolingHttpClientConnectionManager getPoolingHttpClientConnectionManager(SSLProperties sslProperties) {
// register http/https socket factory
RegistryBuilder<ConnectionSocketFactory> builder = RegistryBuilder.<ConnectionSocketFactory>create();
builder.register("http", PlainConnectionSocketFactory.INSTANCE);
if (sslProperties.isEnabled()) {
builder.register("https", new SSLConnectionSocketFactory(SSLManager.createSSLContext(sslProperties.getSslOption(), sslProperties.getSslCustom()), NoopHostnameVerifier.INSTANCE));
}
Registry<ConnectionSocketFactory> connectionSocketFactoryRegistry = builder.build();
// connection pool management
PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(connectionSocketFactoryRegistry);
connectionManager.setMaxTotal(MAX_TOTAL);
connectionManager.setDefaultMaxPerRoute(DEFAULT_MAX_PER_ROUTE);
return connectionManager;
}
use of org.apache.http.conn.ssl.SSLConnectionSocketFactory in project spring-framework by spring-projects.
the class ServerHttpsRequestIntegrationTests method startServer.
@BeforeEach
void startServer() throws Exception {
this.server.setHandler(new CheckRequestHandler());
this.server.afterPropertiesSet();
this.server.start();
// Set dynamically chosen port
this.port = this.server.getPort();
SSLContextBuilder builder = new SSLContextBuilder();
builder.loadTrustMaterial(new TrustSelfSignedStrategy());
SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(builder.build(), NoopHostnameVerifier.INSTANCE);
CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(socketFactory).build();
HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(httpclient);
this.restTemplate = new RestTemplate(requestFactory);
}
Aggregations