Example usage for org.apache.http.ssl SSLContexts custom

List of usage examples for org.apache.http.ssl SSLContexts custom

Introduction

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

Prototype

public static SSLContextBuilder custom() 

Source Link

Document

Creates custom SSL context.

Usage

From source file:org.nmdp.b12s.mac.client.http.X509Config.java

public static SSLContext defaultSslContext() {

    try {//from   w w w  . ja  v a 2s. c  om
        URL trustKeyStoreUrl = ConfigProperty.getFileAsUrl("TRUST_JKS_URL", X509Config.class, "/trusted.jks");
        char[] trustPassword = ConfigProperty.getPropertyPassword("TRUST_JKS_PWD", "changeit");
        URL clientKeyStoreUrl = ConfigProperty.getFileAsUrl("CLIENT_JKS_FILE", X509Config.class,
                "/test-client.jks");
        char[] clientPassword = ConfigProperty.getPropertyPassword("CLIENT_JKS_URL", "changeit");
        char[] clientKeyPassword = ConfigProperty.getPropertyPassword("CLIENT_KEY_PWD", clientPassword);
        SSLContext sslContext = SSLContexts.custom()
                // Configure trusted certs
                .loadTrustMaterial(trustKeyStoreUrl, trustPassword)
                // Configure client certificate
                .loadKeyMaterial(clientKeyStoreUrl, clientPassword, clientKeyPassword).build();
        return sslContext;
    } catch (KeyManagementException | UnrecoverableKeyException | NoSuchAlgorithmException | KeyStoreException
            | CertificateException | IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.wildfly.swarm.https.test.HttpsTest.java

@Test
@RunAsClient/*from w w  w  .j  a  va2 s  .  c om*/
public void hello() throws IOException, GeneralSecurityException {
    SSLContext sslContext = SSLContexts.custom().loadTrustMaterial((TrustStrategy) (chain, authType) -> true)
            .build();
    try (CloseableHttpClient httpClient = HttpClients.custom().setSSLContext(sslContext).build()) {

        String response = Executor.newInstance(httpClient).execute(Request.Get("https://localhost:8443/"))
                .returnContent().asString();
        assertThat(response).contains("Hello on port 8443, secure: true");
    }
}

From source file:nl.architolk.ldt.processors.HttpClientProperties.java

private static void initialize() throws Exception {
    notInitialized = false;// w w  w .ja  v a 2s. co  m

    //Fetch property-values
    PropertySet props = Properties.instance().getPropertySet();
    String proxyHost = props.getString("oxf.http.proxy.host");
    Integer proxyPort = props.getInteger("oxf.http.proxy.port");
    proxyExclude = props.getString("oxf.http.proxy.exclude");
    String sslKeystoreURI = props.getStringOrURIAsString("oxf.http.ssl.keystore.uri", false);
    String sslKeystorePassword = props.getString("oxf.http.ssl.keystore.password");

    //Create custom scheme if needed
    if (sslKeystoreURI != null && sslKeystorePassword != null) {
        SSLContext sslcontext = SSLContexts.custom()
                .loadTrustMaterial(new URL(sslKeystoreURI), sslKeystorePassword.toCharArray()).build();
        sslsf = new SSLConnectionSocketFactory(sslcontext);
    }

    //Create requestConfig proxy if needed
    if (proxyHost != null && proxyPort != null) {
        requestConfig = RequestConfig.custom().setProxy(new HttpHost(proxyHost, proxyPort, "http")).build();
    }
}

From source file:io.confluent.support.metrics.utils.WebClientProxyTest.java

@BeforeClass
public static void startProxy() throws Exception {

    int port = PortFactory.findFreePort();
    clientAndProxy = startClientAndProxy(port);
    proxy = new HttpHost("localhost", port);
    // load truststore with MockServer CA
    sslContext = SSLContexts.custom()
            .loadTrustMaterial(new File("src/test/resources/truststore.jks"), "changeit".toCharArray()).build();
    httpClientBuilder = HttpClients.custom().setSSLContext(sslContext);
    httpclient = httpClientBuilder.build();

    serverPort = PortFactory.findFreePort();
    clientAndServer = ClientAndServer.startClientAndServer(serverPort);
    clientAndServer.when(new HttpRequest().withMethod("GET")).respond(HttpResponse.response("OK"));
}

From source file:org.ulyssis.ipp.publisher.HttpOutput.java

private SSLContext createSslCustomContext() {
    try {/*from   ww w. j  a  v  a 2s . c  om*/
        SSLContextBuilder builder = SSLContexts.custom();
        if (options.getKeystore().isPresent()) {
            KeyStore cks = KeyStore.getInstance(KeyStore.getDefaultType());
            cks.load(new FileInputStream(options.getKeystore().get().toFile()),
                    options.getKeystorePass().toCharArray());
            builder.loadKeyMaterial(cks, options.getKeystorePass().toCharArray());
        }

        if (options.getTruststore().isPresent()) {
            KeyStore tks = KeyStore.getInstance(KeyStore.getDefaultType());
            tks.load(new FileInputStream(options.getTruststore().get().toFile()),
                    options.getTruststorePass().toCharArray());
            builder.loadTrustMaterial(tks, new TrustSelfSignedStrategy());
        }

        if (!options.getKeystore().isPresent() && !options.getKeystore().isPresent()) {
            return SSLContext.getDefault();
        }

        return builder.build();
    } catch (Exception e) {
        // TODO: DO SOMETHING WITH THE EXCEPTION!
        LOG.error("Exception", e);
    }
    return null;
}

From source file:com.cloud.utils.rest.HttpClientHelper.java

private static Registry<ConnectionSocketFactory> createSocketFactoryConfigration()
        throws KeyManagementException, NoSuchAlgorithmException, KeyStoreException {
    Registry<ConnectionSocketFactory> socketFactoryRegistry;
    final SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(new TrustSelfSignedStrategy()).build();
    final SSLConnectionSocketFactory cnnectionSocketFactory = new SSLConnectionSocketFactory(sslContext,
            NoopHostnameVerifier.INSTANCE);
    socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
            .register(HTTPS, cnnectionSocketFactory).build();

    return socketFactoryRegistry;
}

From source file:ch.sourcepond.maven.plugin.jenkins.it.utils.HttpsServerStartupBarrier.java

@Override
protected CloseableHttpClient createClient() throws KeyManagementException, NoSuchAlgorithmException,
        KeyStoreException, CertificateException, IOException, URISyntaxException {
    final URL url = getClass().getResource(KEYSTORE_NAME);
    // Trust own CA and all self-signed certs
    final SSLContext sslcontext = SSLContexts.custom().loadTrustMaterial(new File(url.toURI()),
            TEST_PASSWORD.toCharArray(), new TrustSelfSignedStrategy()).build();
    // Allow TLSv1 protocol only
    final SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext,
            SSLConnectionSocketFactory.getDefaultHostnameVerifier());
    final CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build();

    return httpclient;
}

From source file:de.hska.ld.content.client.PDFGenClient.java

private CloseableHttpClient createHttpsClient() throws IOException {
    SSLContext sslContext = null;
    try {// w  ww  .j  ava2  s.  co  m
        sslContext = SSLContexts.custom().loadTrustMaterial(null, new TrustStrategy() {

            @Override
            public boolean isTrusted(final X509Certificate[] chain, final String authType)
                    throws CertificateException {
                return true;
            }
        }).useProtocol("TLSv1.2").build();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (KeyStoreException e) {
        e.printStackTrace();
    } catch (KeyManagementException e) {
        e.printStackTrace();
    }
    return HttpClients.custom().setSSLContext(sslContext).build();
}

From source file:com.github.restdriver.clientdriver.integration.SecureClientDriverTest.java

@Test
public void testConnectionSucceedsWithGivenTrustMaterial() throws Exception {

    // Arrange/*ww  w.j  a  va 2 s.  com*/
    KeyStore keyStore = getKeystore();
    SecureClientDriver driver = new SecureClientDriver(
            new DefaultClientDriverJettyHandler(new DefaultRequestMatcher()), 1111, keyStore, "password",
            "certificate");
    driver.addExpectation(onRequestTo("/test"), giveEmptyResponse());

    // set the test certificate as trusted
    SSLContext context = SSLContexts.custom().loadTrustMaterial(keyStore, TrustSelfSignedStrategy.INSTANCE)
            .build();
    HttpClient client = HttpClients.custom().setSSLHostnameVerifier(new NoopHostnameVerifier())
            .setSSLContext(context).build();
    HttpGet getter = new HttpGet(driver.getBaseUrl() + "/test");

    // Act
    HttpResponse response = client.execute(getter);

    // Assert
    assertEquals(204, response.getStatusLine().getStatusCode());
    driver.verify();
}

From source file:io.apicurio.studio.fe.servlet.servlets.DownloadServlet.java

@PostConstruct
protected void postConstruct() {
    try {//  ww  w  .j av  a  2s.co  m
        if (uiConfig.isDisableHubApiTrustManager()) {
            SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(null, new TrustSelfSignedStrategy())
                    .build();
            SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext,
                    NoopHostnameVerifier.INSTANCE);
            httpClient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
        } else {
            httpClient = HttpClients.createSystem();
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}