Search in sources :

Example 96 with SSLConnectionSocketFactory

use of org.apache.http.conn.ssl.SSLConnectionSocketFactory in project nutch by apache.

the class ElasticRestIndexWriter method open.

@Override
public void open(Configuration conf, String name) throws IOException {
    hosts = conf.getStrings(ElasticRestConstants.HOST);
    port = conf.getInt(ElasticRestConstants.PORT, 9200);
    user = conf.get(ElasticRestConstants.USER);
    password = conf.get(ElasticRestConstants.PASSWORD);
    https = conf.getBoolean(ElasticRestConstants.HTTPS, false);
    trustAllHostnames = conf.getBoolean(ElasticRestConstants.HOSTNAME_TRUST, false);
    languages = conf.getStrings(ElasticRestConstants.LANGUAGES);
    separator = conf.get(ElasticRestConstants.SEPARATOR, DEFAULT_SEPARATOR);
    sink = conf.get(ElasticRestConstants.SINK, DEFAULT_SINK);
    // trust ALL certificates
    SSLContext sslContext = null;
    try {
        sslContext = new SSLContextBuilder().loadTrustMaterial(new TrustStrategy() {

            public boolean isTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
                return true;
            }
        }).build();
    } catch (NoSuchAlgorithmException | KeyManagementException | KeyStoreException e) {
        LOG.error("Failed to instantiate sslcontext object: \n{}", ExceptionUtils.getStackTrace(e));
        throw new SecurityException();
    }
    // skip hostname checks
    HostnameVerifier hostnameVerifier = null;
    if (trustAllHostnames) {
        hostnameVerifier = NoopHostnameVerifier.INSTANCE;
    } else {
        hostnameVerifier = new DefaultHostnameVerifier();
    }
    SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslContext);
    SchemeIOSessionStrategy httpsIOSessionStrategy = new SSLIOSessionStrategy(sslContext, hostnameVerifier);
    JestClientFactory jestClientFactory = new JestClientFactory();
    if (hosts == null || hosts.length == 0 || port <= 1) {
        throw new IllegalStateException("No hosts or port specified. Please set the host and port in nutch-site.xml");
    }
    List<String> urlsOfElasticsearchNodes = new ArrayList<String>();
    for (String host : hosts) {
        urlsOfElasticsearchNodes.add(new URL(https ? "https" : "http", host, port, "").toString());
    }
    HttpClientConfig.Builder builder = new HttpClientConfig.Builder(urlsOfElasticsearchNodes).multiThreaded(true).connTimeout(300000).readTimeout(300000);
    if (https) {
        if (user != null && password != null) {
            builder.defaultCredentials(user, password);
        }
        builder.defaultSchemeForDiscoveredNodes("https").sslSocketFactory(// this only affects sync calls
        sslSocketFactory).httpsIOSessionStrategy(// this only affects async calls
        httpsIOSessionStrategy);
    }
    jestClientFactory.setHttpClientConfig(builder.build());
    client = jestClientFactory.getObject();
    defaultIndex = conf.get(ElasticRestConstants.INDEX, "nutch");
    defaultType = conf.get(ElasticRestConstants.TYPE, "doc");
    maxBulkDocs = conf.getInt(ElasticRestConstants.MAX_BULK_DOCS, DEFAULT_MAX_BULK_DOCS);
    maxBulkLength = conf.getInt(ElasticRestConstants.MAX_BULK_LENGTH, DEFAULT_MAX_BULK_LENGTH);
    bulkBuilder = new Bulk.Builder().defaultIndex(defaultIndex).defaultType(defaultType);
}
Also used : TrustStrategy(org.apache.http.ssl.TrustStrategy) ArrayList(java.util.ArrayList) CertificateException(java.security.cert.CertificateException) SSLIOSessionStrategy(org.apache.http.nio.conn.ssl.SSLIOSessionStrategy) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) KeyManagementException(java.security.KeyManagementException) URL(java.net.URL) DefaultHostnameVerifier(org.apache.http.conn.ssl.DefaultHostnameVerifier) SSLContextBuilder(org.apache.http.ssl.SSLContextBuilder) HttpClientConfig(io.searchbox.client.config.HttpClientConfig) SchemeIOSessionStrategy(org.apache.http.nio.conn.SchemeIOSessionStrategy) SSLContext(javax.net.ssl.SSLContext) KeyStoreException(java.security.KeyStoreException) JestClientFactory(io.searchbox.client.JestClientFactory) Bulk(io.searchbox.core.Bulk) X509Certificate(java.security.cert.X509Certificate) NoopHostnameVerifier(org.apache.http.conn.ssl.NoopHostnameVerifier) HostnameVerifier(javax.net.ssl.HostnameVerifier) DefaultHostnameVerifier(org.apache.http.conn.ssl.DefaultHostnameVerifier)

Example 97 with SSLConnectionSocketFactory

use of org.apache.http.conn.ssl.SSLConnectionSocketFactory in project syndesis-qe by syndesisio.

the class RestUtils method createAllTrustingClient.

// Required in order to skip certificate validation
private static HttpClient createAllTrustingClient() throws RestClientException {
    HttpClient httpclient = null;
    try {
        final SSLContextBuilder builder = new SSLContextBuilder();
        builder.loadTrustMaterial((TrustStrategy) (X509Certificate[] chain, String authType) -> true);
        final SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(builder.build());
        httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).setMaxConnTotal(1000).setMaxConnPerRoute(1000).build();
    } catch (NoSuchAlgorithmException | KeyStoreException | KeyManagementException e) {
        throw new RestClientException("Cannot create all SSL certificates trusting client", e);
    }
    return httpclient;
}
Also used : HttpClient(org.apache.http.client.HttpClient) RestClientException(io.syndesis.qe.exceptions.RestClientException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyStoreException(java.security.KeyStoreException) SSLContextBuilder(org.apache.http.ssl.SSLContextBuilder) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) X509Certificate(java.security.cert.X509Certificate) KeyManagementException(java.security.KeyManagementException)

Example 98 with SSLConnectionSocketFactory

use of org.apache.http.conn.ssl.SSLConnectionSocketFactory in project coprhd-controller by CoprHD.

the class WinRMTarget method createClientConnectionManager.

private HttpClientConnectionManager createClientConnectionManager() throws Exception {
    SSLContextBuilder contextBuilder = SSLContexts.custom();
    try {
        contextBuilder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
        SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(SSLContexts.custom().loadTrustMaterial(null, new TrustSelfSignedStrategy()).build(), SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
        Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create().register("http", PlainConnectionSocketFactory.INSTANCE).register("https", socketFactory).build();
        return (new PoolingHttpClientConnectionManager(registry));
    } catch (Exception e) {
        throw new HttpException(e.getMessage());
    }
}
Also used : SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) ConnectionSocketFactory(org.apache.http.conn.socket.ConnectionSocketFactory) PlainConnectionSocketFactory(org.apache.http.conn.socket.PlainConnectionSocketFactory) HttpException(org.apache.http.HttpException) SSLContextBuilder(org.apache.http.conn.ssl.SSLContextBuilder) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) TrustSelfSignedStrategy(org.apache.http.conn.ssl.TrustSelfSignedStrategy) HttpException(org.apache.http.HttpException) MalformedURLException(java.net.MalformedURLException) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager)

Example 99 with SSLConnectionSocketFactory

use of org.apache.http.conn.ssl.SSLConnectionSocketFactory in project swift by luastar.

the class HttpClientUtils method createHttpClient.

/**
 * 创建自定义重定向策略,支持https的调用
 *
 * @param url
 * @return
 */
private static CloseableHttpClient createHttpClient(String url, RedirectStrategy redirectStrategy) {
    try {
        HttpClientBuilder httpClientBuilder = HttpClients.custom();
        // 重定向策略
        if (redirectStrategy != null) {
            httpClientBuilder.setRedirectStrategy(redirectStrategy);
        }
        // https支持
        if (StringUtils.isNotEmpty(url) && url.startsWith("https://")) {
            SSLContext sslcontext = SSLContexts.custom().loadTrustMaterial(null, new TrustStrategy() {

                @Override
                public boolean isTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
                    return true;
                }
            }).build();
            SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext);
            httpClientBuilder.setSSLSocketFactory(sslsf);
        }
        return httpClientBuilder.build();
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
    }
    return HttpClients.createDefault();
}
Also used : TrustStrategy(org.apache.http.conn.ssl.TrustStrategy) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) SSLContext(javax.net.ssl.SSLContext) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) CertificateException(java.security.cert.CertificateException)

Example 100 with SSLConnectionSocketFactory

use of org.apache.http.conn.ssl.SSLConnectionSocketFactory in project SEPA by arces-wot.

the class SSLSecurityManager method getSSLHttpClient.

/**
 * Gets the SSL http client.
 *
 * @return the SSL http client
 * @throws KeyManagementException the key management exception
 * @throws NoSuchAlgorithmException the no such algorithm exception
 * @throws KeyStoreException the key store exception
 * @throws CertificateException the certificate exception
 * @throws IOException Signals that an I/O exception has occurred.
 */
public CloseableHttpClient getSSLHttpClient() throws KeyManagementException, NoSuchAlgorithmException, KeyStoreException, CertificateException, IOException {
    // Trust own CA and all self-signed certificates
    SSLContext sslcontext = null;
    sslcontext = SSLContexts.custom().loadTrustMaterial(new File(storename), password.toCharArray(), new TrustSelfSignedStrategy()).build();
    // Allow TLSv1 protocol only
    SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, new String[] { protocol }, null, this);
    return HttpClients.custom().setSSLSocketFactory(sslsf).build();
}
Also used : SSLContext(javax.net.ssl.SSLContext) File(java.io.File) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) TrustSelfSignedStrategy(org.apache.http.conn.ssl.TrustSelfSignedStrategy)

Aggregations

SSLConnectionSocketFactory (org.apache.http.conn.ssl.SSLConnectionSocketFactory)157 SSLContext (javax.net.ssl.SSLContext)99 ConnectionSocketFactory (org.apache.http.conn.socket.ConnectionSocketFactory)63 PlainConnectionSocketFactory (org.apache.http.conn.socket.PlainConnectionSocketFactory)54 SSLContextBuilder (org.apache.http.ssl.SSLContextBuilder)52 PoolingHttpClientConnectionManager (org.apache.http.impl.conn.PoolingHttpClientConnectionManager)49 IOException (java.io.IOException)42 TrustSelfSignedStrategy (org.apache.http.conn.ssl.TrustSelfSignedStrategy)42 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)42 HttpClientBuilder (org.apache.http.impl.client.HttpClientBuilder)36 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)31 KeyManagementException (java.security.KeyManagementException)30 RequestConfig (org.apache.http.client.config.RequestConfig)25 NoopHostnameVerifier (org.apache.http.conn.ssl.NoopHostnameVerifier)25 KeyStoreException (java.security.KeyStoreException)24 HttpClient (org.apache.http.client.HttpClient)24 HttpComponentsClientHttpRequestFactory (org.springframework.http.client.HttpComponentsClientHttpRequestFactory)24 KeyStore (java.security.KeyStore)22 CertificateException (java.security.cert.CertificateException)21 Test (org.junit.Test)21