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

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

Introduction

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

Prototype

X509HostnameVerifier ALLOW_ALL_HOSTNAME_VERIFIER

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

Click Source Link

Usage

From source file:org.ow2.proactive.http.HttpClientBuilderTest.java

@Test
public void testUseSystemPropertiesTrue() throws Exception {
    WebProperties.WEB_HTTPS_ALLOW_ANY_CERTIFICATE.updateProperty("true");
    WebProperties.WEB_HTTPS_ALLOW_ANY_HOSTNAME.updateProperty("TRUE");
    WebProperties.storeInSystemProperties();

    httpClientBuilder.useSystemProperties(true);
    httpClientBuilder.build();/*from   w w w .ja  v  a  2s  .  c  o  m*/

    Mockito.verify(internalHttpClientBuilder).build();
    Mockito.verify(internalHttpClientBuilder).useSystemProperties();
    Mockito.verify(internalHttpClientBuilder)
            .setHostnameVerifier(SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
    Mockito.verify(internalHttpClientBuilder).setSslcontext(Mockito.<SSLContext>any());
}

From source file:com.arvato.thoroughly.util.RestTemplateUtil.java

private LayeredConnectionSocketFactory setUpSSL() {
    LayeredConnectionSocketFactory sslSF = null;
    try {//from  ww w . j  a  v  a  2s  .  co m
        KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
        SSLContext sslContext = SSLContexts.custom().useTLS()
                .loadTrustMaterial(trustStore, new AnyTrustStrategy()).build();
        sslSF = new SSLConnectionSocketFactory(sslContext,
                SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

    } catch (Exception e) {
        LOGGER.error(e.getMessage());
    }
    return sslSF;
}

From source file:com.crosstreelabs.cognitio.gumshoe.transport.HttpTransport.java

private void buildHttpClient() {
    requestConfig = RequestConfig.custom().setExpectContinueEnabled(false).setCookieSpec(CookieSpecs.DEFAULT)
            .setRedirectsEnabled(false).setSocketTimeout(5000).setConnectTimeout(5000)
            .setConnectionRequestTimeout(5000).setStaleConnectionCheckEnabled(true).build();

    RegistryBuilder<ConnectionSocketFactory> connRegistryBuilder = RegistryBuilder.create();
    connRegistryBuilder.register("http", PlainConnectionSocketFactory.INSTANCE);
    try { // Fixing: https://code.google.com/p/crawler4j/issues/detail?id=174
          // By always trusting the ssl certificate
        SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(null, new TrustStrategy() {
            @Override/*from   w w  w .  j av  a2  s .  c om*/
            public boolean isTrusted(final X509Certificate[] chain, String authType) {
                return true;
            }
        }).build();
        SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext,
                SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
        connRegistryBuilder.register("https", sslsf);
    } catch (KeyManagementException | KeyStoreException | NoSuchAlgorithmException e) {
        LOGGER.warn("Exception thrown while trying to register https");
        LOGGER.debug("Stacktrace", e);
    }

    Registry<ConnectionSocketFactory> connRegistry = connRegistryBuilder.build();
    connectionManager = new PoolingHttpClientConnectionManager(connRegistry);
    connectionManager.setMaxTotal(5);
    connectionManager.setDefaultMaxPerRoute(5);

    HttpClientBuilder clientBuilder = HttpClientBuilder.create();
    clientBuilder.setDefaultRequestConfig(requestConfig);
    clientBuilder.setConnectionManager(connectionManager);
    clientBuilder.setUserAgent("Cognitio");

    httpClient = clientBuilder.build();
}

From source file:com.tremolosecurity.config.util.UnisonConfigManagerImpl.java

private void initSSL() throws KeyManagementException, UnrecoverableKeyException, NoSuchAlgorithmException,
        KeyStoreException, CertificateException, FileNotFoundException, IOException {
    if (this.getKeyManagerFactory() == null) {
        return;/*from   w  ww  .j  a  va 2 s  .  co  m*/
    }

    KeyStore cacerts = KeyStore.getInstance(KeyStore.getDefaultType());

    String cacertsPath = System.getProperty("javax.net.ssl.trustStore");
    if (cacertsPath == null) {
        cacertsPath = System.getProperty("java.home") + "/lib/security/cacerts";
    }

    cacerts.load(new FileInputStream(cacertsPath), null);

    Enumeration<String> enumer = cacerts.aliases();
    while (enumer.hasMoreElements()) {
        String alias = enumer.nextElement();
        java.security.cert.Certificate cert = cacerts.getCertificate(alias);
        this.ks.setCertificateEntry(alias, cert);
    }

    SSLContext sslctx = SSLContexts.custom().loadTrustMaterial(this.ks)
            .loadKeyMaterial(this.ks, this.cfg.getKeyStorePassword().toCharArray()).build();
    SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslctx,
            SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

    PlainConnectionSocketFactory sf = PlainConnectionSocketFactory.getSocketFactory();
    httpClientRegistry = RegistryBuilder.<ConnectionSocketFactory>create().register("http", sf)
            .register("https", sslsf).build();

    globalHttpClientConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.IGNORE_COOKIES)
            .setRedirectsEnabled(false).setAuthenticationEnabled(false).build();

}

From source file:utils.TestUtils.java

@SuppressWarnings("deprecation") // http api
public static CloseableHttpClient createHttpsClient() {
    try {/*from ww w  .j a v  a  2  s  .c  o m*/
        TrustManager[] trustAllCerts = new TrustManager[] { new NonValidatingX509TrustManager() };
        SSLContext ctx = SSLContext.getInstance("TLS");
        ctx.init(new KeyManager[] {}, trustAllCerts, null);
        SSLSocketFactory socketFactory = new SSLSocketFactory(ctx,
                SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
        Scheme sch = new Scheme("https", 443, socketFactory);
        CloseableHttpClient http = new DefaultHttpClient();
        http.getConnectionManager().getSchemeRegistry().register(sch);
        return http;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:ee.ria.xroad.common.request.ManagementRequestClient.java

private static CloseableHttpClient createHttpClient(KeyManager km, TrustManager tm) throws Exception {
    RegistryBuilder<ConnectionSocketFactory> sfr = RegistryBuilder.<ConnectionSocketFactory>create();

    sfr.register("http", PlainConnectionSocketFactory.INSTANCE);

    SSLContext ctx = SSLContext.getInstance(CryptoUtils.SSL_PROTOCOL);
    ctx.init(km != null ? new KeyManager[] { km } : null, tm != null ? new TrustManager[] { tm } : null,
            new SecureRandom());

    SSLConnectionSocketFactory sf = new SSLConnectionSocketFactory(ctx,
            SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

    sfr.register("https", sf);

    PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(sfr.build());
    cm.setMaxTotal(CLIENT_MAX_TOTAL_CONNECTIONS);
    cm.setDefaultMaxPerRoute(CLIENT_MAX_CONNECTIONS_PER_ROUTE);

    int timeout = SystemProperties.getClientProxyTimeout();
    int socketTimeout = SystemProperties.getClientProxyHttpClientTimeout();

    RequestConfig.Builder rb = RequestConfig.custom();
    rb.setConnectTimeout(timeout);/* www.j  a va2  s. com*/
    rb.setConnectionRequestTimeout(timeout);
    rb.setSocketTimeout(socketTimeout);

    HttpClientBuilder cb = HttpClients.custom();
    cb.setConnectionManager(cm);
    cb.setDefaultRequestConfig(rb.build());

    // Disable request retry
    cb.setRetryHandler(new DefaultHttpRequestRetryHandler(0, false));

    return cb.build();
}

From source file:org.wisdom.framework.vertx.ServerTest.java

/**
 * This methods checks HTTP, HTTPS and HTTPS with Mutual Authentication.
 *//* ww  w . java2s  . c o m*/
@Test
public void testCreationOfThreeServersFromConfiguration()
        throws InterruptedException, IOException, KeyStoreException, CertificateException,
        NoSuchAlgorithmException, KeyManagementException, UnrecoverableKeyException {
    FakeConfiguration s1 = new FakeConfiguration(ImmutableMap.<String, Object>builder().put("port", 0)
            .put("ssl", false).put("authentication", false).build());

    FakeConfiguration s2 = new FakeConfiguration(ImmutableMap.<String, Object>builder().put("port", 0)
            .put("ssl", true).put("authentication", false).build());

    FakeConfiguration s3 = new FakeConfiguration(ImmutableMap.<String, Object>builder().put("port", 0)
            .put("ssl", true).put("authentication", true).build());

    // Server HTTPS
    File root = new File("");
    final File serverKeyStore = new File(
            root.getAbsolutePath() + "/src/test/resources/keystore/server/server.jks");
    assertThat(serverKeyStore).isFile();
    when(application.get("https.keyStore")).thenReturn(serverKeyStore.getAbsolutePath());
    when(application.get("https.trustStore"))
            .thenReturn(new File(root.getAbsolutePath() + "/src/test/resources/keystore/server/server.jks")
                    .getAbsolutePath());
    when(application.getWithDefault("https.keyStoreType", "JKS")).thenReturn("JKS");
    when(application.getWithDefault("https.trustStoreType", "JKS")).thenReturn("JKS");
    when(application.getWithDefault("https.keyStorePassword", "")).thenReturn("wisdom");
    when(application.getWithDefault("https.trustStorePassword", "")).thenReturn("wisdom");

    when(application.getWithDefault("https.keyStoreAlgorithm", KeyManagerFactory.getDefaultAlgorithm()))
            .thenReturn(KeyManagerFactory.getDefaultAlgorithm());
    when(application.getWithDefault("https.trustStoreAlgorithm", KeyManagerFactory.getDefaultAlgorithm()))
            .thenReturn(KeyManagerFactory.getDefaultAlgorithm());
    when(application.getConfiguration("vertx.servers"))
            .thenReturn(new FakeConfiguration(ImmutableMap.<String, Object>of("s1", s1, "s2", s2, "s3", s3)));

    Controller controller = new DefaultController() {
        @SuppressWarnings("unused")
        public Result index() {
            return ok("Alright");
        }
    };
    Route route = new RouteBuilder().route(HttpMethod.GET).on("/").to(controller, "index");
    when(router.getRouteFor(anyString(), anyString(), any(Request.class))).thenReturn(route);

    wisdom.start();
    waitForStart(wisdom);
    waitForHttpsStart(wisdom);

    assertThat(wisdom.servers).hasSize(3);

    // Check rendering
    for (Server server : wisdom.servers) {
        String r;
        KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
        FileInputStream instream = new FileInputStream("src/test/resources/keystore/client/client1.jks");
        trustStore.load(instream, "wisdom".toCharArray());

        // Trust own CA and all self-signed certs
        SSLContext sslcontext = SSLContexts.custom()
                .loadTrustMaterial(trustStore, new TrustSelfSignedStrategy())
                .loadKeyMaterial(trustStore, "wisdom".toCharArray()).build();

        SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext,
                new String[] { "TLSv1", "SSLv3" }, null,
                SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

        CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build();

        if (server.ssl()) {
            HttpGet httpget = new HttpGet("https://localhost:" + server.port());
            final CloseableHttpResponse response = httpclient.execute(httpget);
            r = EntityUtils.toString(response.getEntity());
        } else {
            r = org.apache.http.client.fluent.Request.Get("http://localhost:" + server.port()).execute()
                    .returnContent().asString();
        }

        assertThat(r).isEqualToIgnoringCase("Alright");
    }
}

From source file:org.ow2.proactive.http.HttpClientBuilderTest.java

@Test
public void testUseSystemPropertiesFalse() throws Exception {
    WebProperties.WEB_HTTPS_ALLOW_ANY_CERTIFICATE.updateProperty("false");
    WebProperties.WEB_HTTPS_ALLOW_ANY_HOSTNAME.updateProperty("FALSE");
    WebProperties.storeInSystemProperties();

    httpClientBuilder.useSystemProperties(false);
    httpClientBuilder.build();//from w  w w  .j ava 2  s .  c o  m

    Mockito.verify(internalHttpClientBuilder).build();
    Mockito.verify(internalHttpClientBuilder, never()).useSystemProperties();
    Mockito.verify(internalHttpClientBuilder, never())
            .setHostnameVerifier(SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
    Mockito.verify(internalHttpClientBuilder, never()).setSslcontext(Mockito.<SSLContext>any());
}