use of org.apache.http.conn.ssl.SSLConnectionSocketFactory in project csb-sdk by aliyun.
the class HttpClientFactory method createConnManager.
/**
* Create a connection pool which supports http and https socket
* @return
* @throws HttpCallerException
*/
public static PoolingHttpClientConnectionManager createConnManager() throws HttpCallerException {
try {
// ignore SSL certificate info with the below two setting:
// 1. trust https server certificate always.
SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
@Override
public boolean isTrusted(java.security.cert.X509Certificate[] chain, String authType) throws java.security.cert.CertificateException {
return true;
}
}).build();
// 2. hostname verifier pass
HostnameVerifier hostnameVerifier = NoopHostnameVerifier.INSTANCE;
SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslContext, hostnameVerifier);
Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create().register("http", PlainConnectionSocketFactory.getSocketFactory()).register("https", sslSocketFactory).build();
return new PoolingHttpClientConnectionManager(socketFactoryRegistry);
} catch (Exception e) {
throw new HttpCallerException("Failed to create httpclient: " + e.getMessage(), e);
}
}
use of org.apache.http.conn.ssl.SSLConnectionSocketFactory in project tephra by heisedebaise.
the class HttpImpl method onContextRefreshed.
@Override
public void onContextRefreshed() {
try {
SSLContext sslContext = SSLContext.getInstance("SSLv3");
sslContext.init(null, new TrustManager[] { new X509TrustManager() {
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}
} }, null);
SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE);
Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create().register("http", PlainConnectionSocketFactory.INSTANCE).register("https", sslConnectionSocketFactory).build();
manager = new PoolingHttpClientConnectionManager(registry);
manager.setMaxTotal(max);
manager.setDefaultMaxPerRoute(max >> 3);
} catch (Exception e) {
logger.warn(e, "初始化HTTP/S客户端时发生异常!");
}
}
use of org.apache.http.conn.ssl.SSLConnectionSocketFactory in project tutorials by eugenp.
the class HttpsClientSslLiveTest method givenHttpClientAfter4_3_whenAcceptingAllCertificates_thenCanConsumeHttpsUriWithSelfSignedCertificate.
@Test
public final void givenHttpClientAfter4_3_whenAcceptingAllCertificates_thenCanConsumeHttpsUriWithSelfSignedCertificate() throws IOException, GeneralSecurityException {
final TrustStrategy acceptingTrustStrategy = (certificate, authType) -> true;
final SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(null, acceptingTrustStrategy).build();
final SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
final CloseableHttpClient httpClient = HttpClients.custom().setHostnameVerifier(SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER).setSSLSocketFactory(sslsf).build();
final HttpGet getMethod = new HttpGet(HOST_WITH_SSL);
final HttpResponse response = httpClient.execute(getMethod);
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
httpClient.close();
}
use of org.apache.http.conn.ssl.SSLConnectionSocketFactory in project fabric-sdk-java by hyperledger.
the class HFCAClient method setUpSSL.
private void setUpSSL() throws InvalidArgumentException {
if (cryptoPrimitives == null) {
try {
cryptoPrimitives = new CryptoPrimitives();
cryptoPrimitives.init();
} catch (Exception e) {
throw new InvalidArgumentException(e);
}
}
if (isSSL && null == registry) {
if (properties.containsKey("pemBytes") && properties.containsKey("pemFile")) {
throw new InvalidArgumentException("Properties can not have both \"pemBytes\" and \"pemFile\" specified. ");
}
try {
if (properties.containsKey("pemBytes")) {
byte[] pemBytes = (byte[]) properties.get("pemBytes");
cryptoPrimitives.addCACertificateToTrustStore(pemBytes, pemBytes.toString());
} else {
String pemFile = properties.getProperty("pemFile");
if (pemFile != null) {
cryptoPrimitives.addCACertificateToTrustStore(new File(pemFile), pemFile);
}
}
SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(cryptoPrimitives.getTrustStore(), null).build();
ConnectionSocketFactory sf;
if (null != properties && "true".equals(properties.getProperty("allowAllHostNames"))) {
AllHostsSSLSocketFactory msf = new AllHostsSSLSocketFactory(cryptoPrimitives.getTrustStore());
msf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
sf = msf;
} else {
sf = new SSLConnectionSocketFactory(sslContext);
}
registry = RegistryBuilder.<ConnectionSocketFactory>create().register("https", sf).register("http", new PlainConnectionSocketFactory()).build();
} catch (Exception e) {
logger.error(e);
throw new InvalidArgumentException(e);
}
}
}
use of org.apache.http.conn.ssl.SSLConnectionSocketFactory in project cosmic by MissionCriticalCloud.
the class HttpClientHelper method createSocketFactoryConfigration.
private static Registry<ConnectionSocketFactory> createSocketFactoryConfigration() throws KeyManagementException, NoSuchAlgorithmException, KeyStoreException {
final Registry<ConnectionSocketFactory> socketFactoryRegistry;
final SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(new TrustSelfSignedStrategy()).build();
final SSLConnectionSocketFactory cnnectionSocketFactory = new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE);
socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create().register(HTTPS, cnnectionSocketFactory).build();
return socketFactoryRegistry;
}
Aggregations