Search in sources :

Example 11 with ConnectionSocketFactory

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();
}
Also used : SSLTestConfig(org.apache.solr.util.SSLTestConfig) PlainConnectionSocketFactory(org.apache.http.conn.socket.PlainConnectionSocketFactory) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) ConnectionSocketFactory(org.apache.http.conn.socket.ConnectionSocketFactory) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager)

Example 12 with ConnectionSocketFactory

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;
    }
}
Also used : SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) ConnectionSocketFactory(org.apache.http.conn.socket.ConnectionSocketFactory) NoopHostnameVerifier(org.apache.http.conn.ssl.NoopHostnameVerifier) X509TrustManager(javax.net.ssl.X509TrustManager) SSLContext(javax.net.ssl.SSLContext) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) HttpClientConnectionManager(org.apache.http.conn.HttpClientConnectionManager) BasicHttpClientConnectionManager(org.apache.http.impl.conn.BasicHttpClientConnectionManager) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) X509Certificate(java.security.cert.X509Certificate) BasicHttpClientConnectionManager(org.apache.http.impl.conn.BasicHttpClientConnectionManager) MalformedCookieException(org.apache.http.cookie.MalformedCookieException) CertificateException(java.security.cert.CertificateException)

Example 13 with ConnectionSocketFactory

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();
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) TrustStrategy(org.apache.http.ssl.TrustStrategy) SSLContext(javax.net.ssl.SSLContext) RegistryBuilder(org.apache.http.config.RegistryBuilder) HttpOptions(org.apache.http.client.methods.HttpOptions) IOException(java.io.IOException) KeyManagementException(java.security.KeyManagementException) KeyStoreException(java.security.KeyStoreException) SSLContextBuilder(org.apache.http.ssl.SSLContextBuilder) HttpPut(org.apache.http.client.methods.HttpPut) Registry(org.apache.http.config.Registry) HttpGet(org.apache.http.client.methods.HttpGet) ConnectionSocketFactory(org.apache.http.conn.socket.ConnectionSocketFactory) PlainConnectionSocketFactory(org.apache.http.conn.socket.PlainConnectionSocketFactory) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager) NoopHostnameVerifier(org.apache.http.conn.ssl.NoopHostnameVerifier) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) URI(java.net.URI) HostnameVerifier(javax.net.ssl.HostnameVerifier) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) TrustStrategy(org.apache.http.ssl.TrustStrategy) ConnectionSocketFactory(org.apache.http.conn.socket.ConnectionSocketFactory) PlainConnectionSocketFactory(org.apache.http.conn.socket.PlainConnectionSocketFactory) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) NoopHostnameVerifier(org.apache.http.conn.ssl.NoopHostnameVerifier) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) SSLContext(javax.net.ssl.SSLContext) SSLContextBuilder(org.apache.http.ssl.SSLContextBuilder) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) NoopHostnameVerifier(org.apache.http.conn.ssl.NoopHostnameVerifier) HostnameVerifier(javax.net.ssl.HostnameVerifier) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager)

Example 14 with ConnectionSocketFactory

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);
    }
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) InetSocketAddress(java.net.InetSocketAddress) HttpGet(org.apache.http.client.methods.HttpGet) HttpClientContext(org.apache.http.client.protocol.HttpClientContext) IOException(java.io.IOException) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager) ConnectionSocketFactory(org.apache.http.conn.socket.ConnectionSocketFactory) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse)

Example 15 with ConnectionSocketFactory

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);
}
Also used : Field(java.lang.reflect.Field) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) ConnectionSocketFactory(org.apache.http.conn.socket.ConnectionSocketFactory) PlainConnectionSocketFactory(org.apache.http.conn.socket.PlainConnectionSocketFactory) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) Test(org.junit.Test)

Aggregations

ConnectionSocketFactory (org.apache.http.conn.socket.ConnectionSocketFactory)30 SSLConnectionSocketFactory (org.apache.http.conn.ssl.SSLConnectionSocketFactory)28 PlainConnectionSocketFactory (org.apache.http.conn.socket.PlainConnectionSocketFactory)21 PoolingHttpClientConnectionManager (org.apache.http.impl.conn.PoolingHttpClientConnectionManager)19 SSLContext (javax.net.ssl.SSLContext)15 HttpClientBuilder (org.apache.http.impl.client.HttpClientBuilder)10 IOException (java.io.IOException)8 HostnameVerifier (javax.net.ssl.HostnameVerifier)8 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)7 URI (java.net.URI)6 HttpResponse (org.apache.http.HttpResponse)6 RequestConfig (org.apache.http.client.config.RequestConfig)6 HttpGet (org.apache.http.client.methods.HttpGet)6 Test (org.junit.Test)6 X509Certificate (java.security.cert.X509Certificate)5 BasicHttpClientConnectionManager (org.apache.http.impl.conn.BasicHttpClientConnectionManager)5 Field (java.lang.reflect.Field)4 CertificateException (java.security.cert.CertificateException)4 HashMap (java.util.HashMap)4 SSLContextBuilder (org.apache.http.conn.ssl.SSLContextBuilder)4