use of org.apache.http.conn.socket.ConnectionSocketFactory in project lucene-solr by apache.
the class TestMiniSolrCloudClusterSSL method getSslAwareClientWithNoClientCerts.
/**
* Returns a new HttpClient that supports both HTTP and HTTPS (with the default test truststore), but
* has no keystore -- so servers requiring client authentication should fail.
*/
private static CloseableHttpClient getSslAwareClientWithNoClientCerts() throws Exception {
// NOTE: This method explicitly does *NOT* use HttpClientUtil code because that
// will muck with the global static HttpClientBuilder / SchemeRegistryProvider
// and we can't do that and still test the entire purpose of what we are trying to test here.
final SSLTestConfig clientConfig = new SSLTestConfig(true, false);
final SSLConnectionSocketFactory sslFactory = clientConfig.buildClientSSLConnectionSocketFactory();
assert null != sslFactory;
final Registry<ConnectionSocketFactory> socketFactoryReg = RegistryBuilder.<ConnectionSocketFactory>create().register("https", sslFactory).register("http", PlainConnectionSocketFactory.INSTANCE).build();
final HttpClientBuilder builder = HttpClientBuilder.create();
builder.setConnectionManager(new PoolingHttpClientConnectionManager(socketFactoryReg));
return builder.build();
}
use of org.apache.http.conn.socket.ConnectionSocketFactory in project wildfly by wildfly.
the class TestHttpClientUtils method getHttpsClient.
/**
*@param credentialsProvider optional cred provider
* @return client that doesn't verify https connections
*/
public static CloseableHttpClient getHttpsClient(CredentialsProvider credentialsProvider) {
try {
SSLContext ctx = SSLContext.getInstance("TLS");
X509TrustManager tm = new X509TrustManager() {
public void checkClientTrusted(X509Certificate[] xcs, String string) throws CertificateException {
}
public void checkServerTrusted(X509Certificate[] xcs, String string) throws CertificateException {
}
public X509Certificate[] getAcceptedIssuers() {
return null;
}
};
ctx.init(null, new TrustManager[] { tm }, null);
ctx.init(null, new TrustManager[] { tm }, null);
SSLConnectionSocketFactory sslConnectionFactory = new SSLConnectionSocketFactory(ctx, new NoopHostnameVerifier());
Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create().register("https", sslConnectionFactory).build();
HttpClientConnectionManager ccm = new BasicHttpClientConnectionManager(registry);
HttpClientBuilder builder = HttpClientBuilder.create().setSSLSocketFactory(sslConnectionFactory).setSSLHostnameVerifier(new NoopHostnameVerifier()).setConnectionManager(ccm);
if (credentialsProvider != null) {
builder.setDefaultCredentialsProvider(credentialsProvider);
}
return builder.build();
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
}
use of org.apache.http.conn.socket.ConnectionSocketFactory in project pact-jvm by DiUS.
the class InsecureHttpsRequest method setupInsecureSSL.
private void setupInsecureSSL() throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException {
HttpClientBuilder b = HttpClientBuilder.create();
// setup a Trust Strategy that allows all certificates.
//
TrustStrategy trustStrategy = (chain, authType) -> true;
SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, trustStrategy).build();
b.setSSLContext(sslContext);
// don't check Hostnames, either.
// -- use SSLConnectionSocketFactory.getDefaultHostnameVerifier(), if you don't want to weaken
HostnameVerifier hostnameVerifier = new NoopHostnameVerifier();
// here's the special part:
// -- need to create an SSL Socket Factory, to use our weakened "trust strategy";
// -- and create a Registry, to register it.
//
SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslContext, hostnameVerifier);
Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create().register("http", PlainConnectionSocketFactory.getSocketFactory()).register("https", sslSocketFactory).build();
// now, we create connection-manager using our Registry.
// -- allows multi-threaded use
PoolingHttpClientConnectionManager connMgr = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
b.setConnectionManager(connMgr);
// finally, build the HttpClient;
// -- done!
this.httpclient = b.build();
}
use of org.apache.http.conn.socket.ConnectionSocketFactory in project bitsquare by bitsquare.
the class HttpClient method requestWithGETProxy.
/**
* Make an HTTP Get request routed over socks5 proxy.
*/
private String requestWithGETProxy(String param, Socks5Proxy socks5Proxy, @Nullable String headerKey, @Nullable String headerValue) throws IOException, HttpException {
log.debug("requestWithGETProxy param=" + param);
// This code is adapted from:
// http://stackoverflow.com/a/25203021/5616248
// Register our own SocketFactories to override createSocket() and connectSocket().
// connectSocket does NOT resolve hostname before passing it to proxy.
Registry<ConnectionSocketFactory> reg = RegistryBuilder.<ConnectionSocketFactory>create().register("http", new SocksConnectionSocketFactory()).register("https", new SocksSSLConnectionSocketFactory(SSLContexts.createSystemDefault())).build();
// Use FakeDNSResolver if not resolving DNS locally.
// This prevents a local DNS lookup (which would be ignored anyway)
PoolingHttpClientConnectionManager cm = socks5Proxy.resolveAddrLocally() ? new PoolingHttpClientConnectionManager(reg) : new PoolingHttpClientConnectionManager(reg, new FakeDnsResolver());
try (CloseableHttpClient httpclient = HttpClients.custom().setConnectionManager(cm).build()) {
InetSocketAddress socksaddr = new InetSocketAddress(socks5Proxy.getInetAddress(), socks5Proxy.getPort());
// remove me: Use this to test with system-wide Tor proxy, or change port for another proxy.
// InetSocketAddress socksaddr = new InetSocketAddress("127.0.0.1", 9050);
HttpClientContext context = HttpClientContext.create();
context.setAttribute("socks.address", socksaddr);
HttpGet request = new HttpGet(baseUrl + param);
if (headerKey != null && headerValue != null)
request.setHeader(headerKey, headerValue);
log.debug("Executing request " + request + " proxy: " + socksaddr);
try (CloseableHttpResponse response = httpclient.execute(request, context)) {
return convertInputStreamToString(response.getEntity().getContent());
}
} catch (Throwable t) {
log.debug("Error at requestWithGETProxy: " + t.getMessage());
throw new IOException(t);
}
}
use of org.apache.http.conn.socket.ConnectionSocketFactory in project dropwizard by dropwizard.
the class HttpClientBuilderTest method canUseASystemHostnameVerifierByDefaultWhenTlsConfigurationNotSpecified.
@Test
public void canUseASystemHostnameVerifierByDefaultWhenTlsConfigurationNotSpecified() throws Exception {
final Registry<ConnectionSocketFactory> configuredRegistry;
configuredRegistry = builder.createConfiguredRegistry();
assertThat(configuredRegistry).isNotNull();
final SSLConnectionSocketFactory socketFactory = (SSLConnectionSocketFactory) configuredRegistry.lookup("https");
assertThat(socketFactory).isNotNull();
final Field hostnameVerifierField = FieldUtils.getField(SSLConnectionSocketFactory.class, "hostnameVerifier", true);
assertThat(hostnameVerifierField.get(socketFactory)).isInstanceOf(HostnameVerifier.class);
}
Aggregations