Example usage for org.apache.http.conn.ssl SSLConnectionSocketFactory STRICT_HOSTNAME_VERIFIER

List of usage examples for org.apache.http.conn.ssl SSLConnectionSocketFactory STRICT_HOSTNAME_VERIFIER

Introduction

In this page you can find the example usage for org.apache.http.conn.ssl SSLConnectionSocketFactory STRICT_HOSTNAME_VERIFIER.

Prototype

X509HostnameVerifier STRICT_HOSTNAME_VERIFIER

To view the source code for org.apache.http.conn.ssl SSLConnectionSocketFactory STRICT_HOSTNAME_VERIFIER.

Click Source Link

Usage

From source file:com.floragunn.searchguard.httpclient.HttpClient.java

private final CloseableHttpClient createHTTPClient()
        throws NoSuchAlgorithmException, KeyStoreException, CertificateException, FileNotFoundException,
        IOException, UnrecoverableKeyException, KeyManagementException {

    // basic auth
    // pki auth//from w w  w.  j  ava  2 s  .  c o m
    // kerberos auth

    final org.apache.http.impl.client.HttpClientBuilder hcb = HttpClients.custom();

    if (ssl) {

        final SSLContextBuilder sslContextbBuilder = SSLContexts.custom().useTLS();

        if (log.isTraceEnabled()) {
            log.trace("Configure HTTP client with SSL");
        }

        if (trustStore != null) {
            final KeyStore myTrustStore = KeyStore
                    .getInstance(trustStore.getName().endsWith("jks") ? "JKS" : "PKCS12");
            myTrustStore.load(new FileInputStream(trustStore),
                    truststorePassword == null || truststorePassword.isEmpty() ? null
                            : truststorePassword.toCharArray());
            sslContextbBuilder.loadTrustMaterial(myTrustStore);
        }

        if (keystore != null) {
            final KeyStore keyStore = KeyStore
                    .getInstance(keystore.getName().endsWith("jks") ? "JKS" : "PKCS12");
            keyStore.load(new FileInputStream(keystore),
                    keystorePassword == null || keystorePassword.isEmpty() ? null
                            : keystorePassword.toCharArray());
            sslContextbBuilder.loadKeyMaterial(keyStore,
                    keystorePassword == null || keystorePassword.isEmpty() ? null
                            : keystorePassword.toCharArray());
        }

        final SSLContext sslContext = sslContextbBuilder.build();
        final SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext,
                new String[] { "TLSv1.1", "TLSv1.2" }, null,
                verifyHostnames ? SSLConnectionSocketFactory.STRICT_HOSTNAME_VERIFIER
                        : SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

        hcb.setSSLSocketFactory(sslsf);
    }

    /*if (keytab != null) {
            
    //System.setProperty("java.security.auth.login.config", "login.conf");
    //System.setProperty("java.security.krb5.conf", "krb5.conf");
            
            
    final CredentialsProvider credsProvider = new BasicCredentialsProvider();
    //SPNEGO/Kerberos setup
    log.debug("SPNEGO activated");
    final AuthSchemeProvider nsf = new LoginSPNegoSchemeFactory(true);
    final Credentials jaasCreds = new JaasCredentials();
    credsProvider.setCredentials(new AuthScope(null, -1, null, AuthSchemes.SPNEGO), jaasCreds);
    credsProvider.setCredentials(new AuthScope(null, -1, null, AuthSchemes.NTLM), new NTCredentials("Guest", "Guest", "Guest",
            "Guest"));
    final Registry<AuthSchemeProvider> authSchemeRegistry = RegistryBuilder.<AuthSchemeProvider> create()
            .register(AuthSchemes.SPNEGO, nsf).register(AuthSchemes.NTLM, new NTLMSchemeFactory()).build();
            
    hcb.setDefaultAuthSchemeRegistry(authSchemeRegistry);
    hcb.setDefaultCredentialsProvider(credsProvider);
    }*/

    if (basicCredentials != null) {
        hcb.setDefaultHeaders(
                Lists.newArrayList(new BasicHeader(HttpHeaders.AUTHORIZATION, "Basic " + basicCredentials)));
    }

    return hcb.build();
}

From source file:com.mirth.connect.client.core.ConnectServiceUtil.java

private static CloseableHttpClient getClient(String[] protocols, String[] cipherSuites) {
    RegistryBuilder<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder
            .<ConnectionSocketFactory>create();
    String[] enabledProtocols = MirthSSLUtil.getEnabledHttpsProtocols(protocols);
    String[] enabledCipherSuites = MirthSSLUtil.getEnabledHttpsCipherSuites(cipherSuites);
    SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory(
            SSLContexts.createSystemDefault(), enabledProtocols, enabledCipherSuites,
            SSLConnectionSocketFactory.STRICT_HOSTNAME_VERIFIER);
    socketFactoryRegistry.register("https", sslConnectionSocketFactory);

    BasicHttpClientConnectionManager httpClientConnectionManager = new BasicHttpClientConnectionManager(
            socketFactoryRegistry.build());
    httpClientConnectionManager.setSocketConfig(SocketConfig.custom().setSoTimeout(TIMEOUT).build());
    return HttpClients.custom().setConnectionManager(httpClientConnectionManager).build();
}

From source file:com.baidubce.http.BceHttpClient.java

/**
 * Create connection manager for http client.
 *
 * @return The connection manager for http client.
 */// w  w w .j a va  2 s  .c om
private HttpClientConnectionManager createHttpClientConnectionManager() {
    ConnectionSocketFactory socketFactory = PlainConnectionSocketFactory.getSocketFactory();
    LayeredConnectionSocketFactory sslSocketFactory;
    try {
        sslSocketFactory = new SSLConnectionSocketFactory(SSLContext.getDefault(),
                SSLConnectionSocketFactory.STRICT_HOSTNAME_VERIFIER);
    } catch (NoSuchAlgorithmException e) {
        throw new BceClientException("Fail to create SSLConnectionSocketFactory", e);
    }
    Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
            .register(Protocol.HTTP.toString(), socketFactory)
            .register(Protocol.HTTPS.toString(), sslSocketFactory).build();
    PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(registry);
    connectionManager.setDefaultMaxPerRoute(this.config.getMaxConnections());
    connectionManager.setDefaultSocketConfig(SocketConfig.custom()
            .setSoTimeout(this.config.getSocketTimeoutInMillis()).setTcpNoDelay(true).build());
    connectionManager.setMaxTotal(this.config.getMaxConnections());
    return connectionManager;
}

From source file:org.bonitasoft.connectors.rest.RESTConnector.java

/**
 * Set the request builder based on the request
 * //from   w w w .ja v a2  s. c o m
 * @param ssl The request SSL options
 * @param httpClientBuilder The request builder
 * @throws Exception
 */
private void setSSL(final SSL ssl, final HttpClientBuilder httpClientBuilder) throws Exception {
    if (ssl != null) {
        final SSLContextBuilder sslContextBuilder = new SSLContextBuilder();

        if (ssl.getTrustStore() != null) {
            final KeyStore trustStore = ssl.getTrustStore().generateKeyStore();
            if (ssl.isUseSelfSignedCertificate()) {
                sslContextBuilder.loadTrustMaterial(trustStore, new TrustSelfSignedStrategy());
            } else {
                sslContextBuilder.loadTrustMaterial(trustStore);
            }
        }

        if (ssl.getKeyStore() != null) {
            final KeyStore keyStore = ssl.getKeyStore().generateKeyStore();
            final String keyStorePassword = ssl.getKeyStore().getPassword();
            sslContextBuilder.loadKeyMaterial(keyStore, keyStorePassword.toCharArray());
        }

        sslContextBuilder.setSecureRandom(null);

        if (ssl.isUseTLS()) {
            sslContextBuilder.useTLS();
        } else {
            sslContextBuilder.useSSL();
        }

        final SSLVerifier verifier = ssl.getSslVerifier();
        X509HostnameVerifier hostnameVerifier = SSLConnectionSocketFactory.STRICT_HOSTNAME_VERIFIER;
        switch (verifier) {
        case BROWSER:
            hostnameVerifier = SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER;
            break;
        case ALLOW:
            hostnameVerifier = SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;
            break;
        case STRICT:
            hostnameVerifier = SSLConnectionSocketFactory.STRICT_HOSTNAME_VERIFIER;
            break;
        default:
            hostnameVerifier = SSLConnectionSocketFactory.STRICT_HOSTNAME_VERIFIER;
            break;
        }

        final SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(
                sslContextBuilder.build(), hostnameVerifier);
        httpClientBuilder.setSSLSocketFactory(socketFactory);
    }
}

From source file:org.finra.herd.dao.helper.HttpClientHelper.java

/**
 * Creates a new HTTP client./*from w  w w  .j a  va 2s .c om*/
 *
 * @param trustSelfSignedCertificate specifies whether to trust a self-signed certificate
 * @param disableHostnameVerification specifies whether to turn off hostname verification
 *
 * @return the HTTP client
 * @throws KeyStoreException if a key store exception occurs
 * @throws NoSuchAlgorithmException if a no such algorithm exception occurs
 * @throws KeyManagementException if key management exception
 */
public CloseableHttpClient createHttpClient(Boolean trustSelfSignedCertificate,
        Boolean disableHostnameVerification)
        throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException {
    // Create an HTTP client builder.
    HttpClientBuilder httpClientBuilder = HttpClients.custom();

    // Create an SSL context builder.
    SSLContextBuilder sslContextBuilder = SSLContexts.custom();

    // If specified, setup a trust strategy that allows all certificates.
    if (BooleanUtils.isTrue(trustSelfSignedCertificate)) {
        sslContextBuilder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
    }

    // If specified, turn hostname verification off.
    HostnameVerifier hostnameVerifier = BooleanUtils.isTrue(disableHostnameVerification)
            ? SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER
            : SSLConnectionSocketFactory.STRICT_HOSTNAME_VERIFIER;

    // Create and assign an SSL connection socket factory.
    SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory(
            sslContextBuilder.build(), hostnameVerifier);
    httpClientBuilder.setSSLSocketFactory(sslConnectionSocketFactory);

    // Build and return an HTTP client.
    return httpClientBuilder.build();
}