Example usage for org.apache.http.ssl SSLContextBuilder SSLContextBuilder

List of usage examples for org.apache.http.ssl SSLContextBuilder SSLContextBuilder

Introduction

In this page you can find the example usage for org.apache.http.ssl SSLContextBuilder SSLContextBuilder.

Prototype

public SSLContextBuilder() 

Source Link

Usage

From source file:org.springframework.boot.web.servlet.server.AbstractServletWebServerFactoryTests.java

@Test
public void sslNeedsClientAuthenticationSucceedsWithClientCertificate() throws Exception {
    AbstractServletWebServerFactory factory = getFactory();
    addTestTxtFile(factory);//from   ww w.  ja va 2s.  c o m
    factory.setSsl(getSsl(ClientAuth.NEED, "password", "classpath:test.jks", "classpath:test.jks", null, null));
    this.webServer = factory.getWebServer();
    this.webServer.start();
    KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
    keyStore.load(new FileInputStream(new File("src/test/resources/test.jks")), "secret".toCharArray());
    SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(
            new SSLContextBuilder().loadTrustMaterial(null, new TrustSelfSignedStrategy())
                    .loadKeyMaterial(keyStore, "password".toCharArray()).build());
    HttpClient httpClient = HttpClients.custom().setSSLSocketFactory(socketFactory).build();
    HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(
            httpClient);
    assertThat(getResponse(getLocalUrl("https", "/test.txt"), requestFactory)).isEqualTo("test");
}

From source file:org.springframework.boot.web.servlet.server.AbstractServletWebServerFactoryTests.java

@Test(expected = IOException.class)
public void sslNeedsClientAuthenticationFailsWithoutClientCertificate() throws Exception {
    AbstractServletWebServerFactory factory = getFactory();
    addTestTxtFile(factory);/*from www  .j av  a2s. c o  m*/
    factory.setSsl(getSsl(ClientAuth.NEED, "password", "classpath:test.jks"));
    this.webServer = factory.getWebServer();
    this.webServer.start();
    SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(
            new SSLContextBuilder().loadTrustMaterial(null, new TrustSelfSignedStrategy()).build());
    HttpClient httpClient = HttpClients.custom().setSSLSocketFactory(socketFactory).build();
    HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(
            httpClient);
    getResponse(getLocalUrl("https", "/test.txt"), requestFactory);
}

From source file:com.clustercontrol.http.util.GetHttpResponse.java

private CloseableHttpClient getHttpClient()
        throws KeyManagementException, NoSuchAlgorithmException, KeyStoreException {
    if (m_client == null) {
        List<Header> headers = new ArrayList<>();

        HttpClientBuilder builder = HttpClients.custom().setDefaultCredentialsProvider(m_cledentialProvider)
                .setDefaultHeaders(headers);

        if (!this.m_needAuthSSLCert) {
            // SSL ??
            TrustStrategy trustStrategy = new TrustStrategy() {
                @Override//ww w .  j  av a  2 s.co m
                public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
                    return true;
                }
            };
            builder.setSSLSocketFactory(new SSLConnectionSocketFactory(
                    new SSLContextBuilder().loadTrustMaterial(null, trustStrategy).build(),
                    new NoopHostnameVerifier()));
        }
        RequestConfig requestConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.DEFAULT)
                .setConnectTimeout(m_connectTimeout).setSocketTimeout(m_requestTimeout).build();
        builder.setDefaultRequestConfig(requestConfig);

        if (m_proxyHost != null) {
            HttpHost proxy = new HttpHost(m_proxyHost, m_proxyPort,
                    m_proxyScheme == null ? "https" : m_proxyScheme);
            if (m_proxyUser != null && m_proxyPassword != null) {
                m_cledentialProvider.setCredentials(new AuthScope(proxy.getHostName(), proxy.getPort()),
                        new UsernamePasswordCredentials(m_proxyUser, m_proxyPassword));
            }
            builder.setProxy(proxy);
        }

        if (m_userAgent != null) {
            headers.add(new BasicHeader(HTTP.USER_AGENT, m_userAgent));
        }

        if (m_cancelProxyCache) {
            // ?
            // https://www.ipa.go.jp/security/awareness/vendor/programmingv2/contents/405.html
            headers.add(new BasicHeader("Cache-Control", "no-cache"));
            headers.add(new BasicHeader("Pragma", "no-cache"));
        }

        if (keepAlive) {
            headers.add(new BasicHeader(HTTP.CONN_DIRECTIVE, HTTP.CONN_KEEP_ALIVE));
        } else {
            headers.add(new BasicHeader(HTTP.CONN_DIRECTIVE, HTTP.CONN_CLOSE));
        }
        m_client = builder.build();
    }

    return m_client;
}

From source file:org.springframework.boot.web.servlet.server.AbstractServletWebServerFactoryTests.java

@Test
public void sslWantsClientAuthenticationSucceedsWithClientCertificate() throws Exception {
    AbstractServletWebServerFactory factory = getFactory();
    addTestTxtFile(factory);// ww  w .j a v a 2 s  .c o m
    factory.setSsl(getSsl(ClientAuth.WANT, "password", "classpath:test.jks"));
    this.webServer = factory.getWebServer();
    this.webServer.start();
    KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
    keyStore.load(new FileInputStream(new File("src/test/resources/test.jks")), "secret".toCharArray());
    SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(
            new SSLContextBuilder().loadTrustMaterial(null, new TrustSelfSignedStrategy())
                    .loadKeyMaterial(keyStore, "password".toCharArray()).build());
    HttpClient httpClient = HttpClients.custom().setSSLSocketFactory(socketFactory).build();
    HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(
            httpClient);
    assertThat(getResponse(getLocalUrl("https", "/test.txt"), requestFactory)).isEqualTo("test");
}

From source file:org.elasticsearch.xpack.core.ssl.SSLConfigurationReloaderTests.java

private static MockWebServer getSslServer(Path keyStorePath, String keyStorePass)
        throws KeyStoreException, CertificateException, NoSuchAlgorithmException, IOException,
        KeyManagementException, UnrecoverableKeyException {
    KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
    try (InputStream is = Files.newInputStream(keyStorePath)) {
        keyStore.load(is, keyStorePass.toCharArray());
    }//from www . j a v  a 2 s.  c  o m
    final SSLContext sslContext = new SSLContextBuilder().loadKeyMaterial(keyStore, keyStorePass.toCharArray())
            .build();
    MockWebServer server = new MockWebServer(sslContext, false);
    server.enqueue(new MockResponse().setResponseCode(200).setBody("body"));
    server.start();
    return server;
}

From source file:com.lehman.ic9.net.httpClient.java

/**
 * Build client method is used initialize the HTTP client and is 
 * called from perform request./* w  ww. j a  v  a 2 s  .c o m*/
 * @param httpGet is a HttpRequest object with the request.
 * @throws NoSuchAlgorithmException Exception
 * @throws KeyStoreException Exception
 * @throws KeyManagementException Exception
 * @throws AuthenticationException Exception
 */
private void buildClient(HttpRequest httpGet)
        throws NoSuchAlgorithmException, KeyStoreException, KeyManagementException, AuthenticationException {
    this.hcb = HttpClients.custom();
    this.hcb.setDefaultCookieStore(this.cs);
    this.hcb.setDefaultCredentialsProvider(this.cp);
    this.hcb.setDefaultRequestConfig(this.rcb.build());

    if (this.allowSelfSigned) {
        SSLContextBuilder sslBuilder = new SSLContextBuilder();
        sslBuilder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
        SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslBuilder.build(),
                SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
        this.hcb.setSSLSocketFactory(sslsf);
    }

    this.buildAuth(httpGet);

    if (this.tcpNoDelay) {
        SocketConfig socketConfig = SocketConfig.custom().setTcpNoDelay(true).build();
        this.hcb.setDefaultSocketConfig(socketConfig);
    }

    this.cli = hcb.build();
}

From source file:org.springframework.boot.web.servlet.server.AbstractServletWebServerFactoryTests.java

@Test
public void sslWantsClientAuthenticationSucceedsWithoutClientCertificate() throws Exception {
    AbstractServletWebServerFactory factory = getFactory();
    addTestTxtFile(factory);//  w  ww  .  j  a va2s  .  c  o  m
    factory.setSsl(getSsl(ClientAuth.WANT, "password", "classpath:test.jks"));
    this.webServer = factory.getWebServer();
    this.webServer.start();
    SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(
            new SSLContextBuilder().loadTrustMaterial(null, new TrustSelfSignedStrategy()).build());
    HttpClient httpClient = HttpClients.custom().setSSLSocketFactory(socketFactory).build();
    HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(
            httpClient);
    assertThat(getResponse(getLocalUrl("https", "/test.txt"), requestFactory)).isEqualTo("test");
}

From source file:org.elasticsearch.xpack.core.ssl.SSLConfigurationReloaderTests.java

private static CloseableHttpClient getSSLClient(Path trustStorePath, String trustStorePass)
        throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException, IOException,
        CertificateException {/*from  w w w . j av a  2 s  .co m*/
    KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
    try (InputStream is = Files.newInputStream(trustStorePath)) {
        trustStore.load(is, trustStorePass.toCharArray());
    }
    final SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(trustStore, null).build();
    return HttpClients.custom().setSSLContext(sslContext).build();
}

From source file:org.springframework.boot.web.servlet.server.AbstractServletWebServerFactoryTests.java

@Test
public void sslWithCustomSslStoreProvider() throws Exception {
    AbstractServletWebServerFactory factory = getFactory();
    addTestTxtFile(factory);// w w  w.j  a v a2 s. com
    Ssl ssl = new Ssl();
    ssl.setClientAuth(ClientAuth.NEED);
    ssl.setKeyPassword("password");
    factory.setSsl(ssl);
    SslStoreProvider sslStoreProvider = mock(SslStoreProvider.class);
    given(sslStoreProvider.getKeyStore()).willReturn(loadStore());
    given(sslStoreProvider.getTrustStore()).willReturn(loadStore());
    factory.setSslStoreProvider(sslStoreProvider);
    this.webServer = factory.getWebServer();
    this.webServer.start();
    KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
    keyStore.load(new FileInputStream(new File("src/test/resources/test.jks")), "secret".toCharArray());
    SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(
            new SSLContextBuilder().loadTrustMaterial(null, new TrustSelfSignedStrategy())
                    .loadKeyMaterial(keyStore, "password".toCharArray()).build());
    HttpClient httpClient = HttpClients.custom().setSSLSocketFactory(socketFactory).build();
    HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(
            httpClient);
    assertThat(getResponse(getLocalUrl("https", "/test.txt"), requestFactory)).isEqualTo("test");
    verify(sslStoreProvider).getKeyStore();
    verify(sslStoreProvider).getTrustStore();
}

From source file:com.vmware.identity.openidconnect.sample.RelyingPartyController.java

private void populateSSLCertificates(String domainControllerFQDN, int domainControllerPort) throws Exception {
    AfdClient afdClient = new AfdClient(domainControllerFQDN, domainControllerPort,
            NoopHostnameVerifier.INSTANCE, new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
                @Override//from ww  w.java  2s  . c  o m
                public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
                    return true;
                }
            }).build());

    List<CertificateDTO> certs = afdClient.vecs().getSSLCertificates();
    int index = 1;
    for (CertificateDTO cert : certs) {
        this.keyStore.setCertificateEntry(String.format("VecsSSLCert%d", index), cert.getX509Certificate());
        index++;
    }
}