Package org.apache.http.conn.ssl

Examples of org.apache.http.conn.ssl.SSLContextBuilder


    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();
View Full Code Here


    this.container = factory.getEmbeddedServletContainer();
    this.container.start();

    SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(
        new SSLContextBuilder().loadTrustMaterial(null,
            new TrustSelfSignedStrategy()).build());

    HttpClient httpClient = HttpClients.custom().setSSLSocketFactory(socketFactory)
        .build();
View Full Code Here

    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();
View Full Code Here

    this.container = factory.getEmbeddedServletContainer();
    this.container.start();

    SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(
        new SSLContextBuilder().loadTrustMaterial(null,
            new TrustSelfSignedStrategy()).build());

    HttpClient httpClient = HttpClients.custom().setSSLSocketFactory(socketFactory)
        .build();
View Full Code Here

  private CloseableHttpClient prepareAuthenticatingClient() throws Exception {
    // install host name verifier that always approves host names
    AllowAllHostnameVerifier hostnameVerifier = new AllowAllHostnameVerifier();
    // for SSL requests we should accept self-signed host certificates
    KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
    SSLContextBuilder sslContextBuilder = SSLContexts.custom()
        .loadTrustMaterial(trustStore, new TrustSelfSignedStrategy());

    // first attempt to prepare a https client with certificate credentials
    if (this.certificateCredentials != null) {
      String keystorePath = this.certificateCredentials.getKeystorePath();
      String keystorePassword = this.certificateCredentials
          .getKeystorePassword();
      // fall back to keystore password if key password is missing
      String keyPassword = this.certificateCredentials.getKeyPassword()
          .or(keystorePassword);
      KeyStore keyStore = KeyStore.getInstance(KEYSTORE_TYPE);
      keyStore.load(new FileInputStream(keystorePath),
          keystorePassword.toCharArray());
      sslContextBuilder.loadKeyMaterial(keyStore,
          keyPassword.toCharArray());
    }

    CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    if (this.basicCredentials != null) {
      String username = this.basicCredentials.getUsername();
      String password = this.basicCredentials.getPassword();
      credentialsProvider.setCredentials(AuthScope.ANY,
          new UsernamePasswordCredentials(username, password));
    }
    CloseableHttpClient httpclient = HttpClients.custom()
        .setDefaultCredentialsProvider(credentialsProvider)
        .setSslcontext(sslContextBuilder.build())
        .setHostnameVerifier(hostnameVerifier).build();
    return httpclient;
  }
View Full Code Here

   
    final HttpClientBuilder builder = HttpClientBuilder.create();
   
    // Allow self-signed SSL certificates:
    try {
      final SSLContextBuilder sslbuilder = new SSLContextBuilder();
      sslbuilder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
      final SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
          sslbuilder.build(),
          SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
     
      builder.setSSLSocketFactory(sslsf);
    } catch (final Exception e) {
      LOG.log(Level.WARNING, "Couldn't init SSL strategy", e);
View Full Code Here

    }

    private static CloseableHttpClient createHttpsClient() {
        CloseableHttpClient chc = null;
        try {
            final SSLContextBuilder builder = new SSLContextBuilder();
            builder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
            chc = HttpClients.custom().setSSLSocketFactory(
                    new SSLConnectionSocketFactory(builder.build(),
                            SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER)).build();
        } catch (KeyManagementException ex) {
        } catch (NoSuchAlgorithmException ex) {
        } catch (KeyStoreException ex) {
        }
View Full Code Here

        assertThat(response.getStatusCode().value(), equalTo(200));
    }

    @Test
    public final void givenHttpClientPost4_3_whenAcceptingAllCertificates_thenCanConsumeHttpsUriWithSelfSignedCertificate() throws IOException, GeneralSecurityException {
        final SSLContextBuilder builder = new SSLContextBuilder();
        builder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
        final SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(builder.build());
        final CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(sslsf).build();

        // new

        final String urlOverHttps = "https://localhost:8443/spring-security-rest-basic-auth/api/bars/1";
View Full Code Here

            acceptSelfSignedCerts = Boolean.parseBoolean(System.getenv(PROXY_ACCEPT_SELF_SIGNED_CERTS_ENV));
        }

        if (acceptSelfSignedCerts) {
            try {
                SSLContextBuilder builder = new SSLContextBuilder();
                builder.loadTrustMaterial(null, new TrustStrategy() {
                    @Override
                    public boolean isTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
                        return true;
                    }
                });
                SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
                        builder.build(), SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
                httpClientBuilder.setSSLSocketFactory(sslsf);
            } catch (NoSuchAlgorithmException e) {
                throw new ServletException(e);
            } catch (KeyStoreException e) {
                throw new ServletException(e);
View Full Code Here

        }

        if (isTrusted(method.getURI().getAuthority())) {
            // creating a special configuration that allows connections to non-trusted HTTPS hosts
            try {
                SSLContextBuilder sslContextBuilder = new SSLContextBuilder();
                sslContextBuilder.loadTrustMaterial(null, new TrustSelfSignedStrategy() {
                    @Override
                    public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
                        return true;
                    }
                });
                SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory(
                        sslContextBuilder.build(), SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

                Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
                        .register("https", sslConnectionSocketFactory)
                        .build();
View Full Code Here

TOP

Related Classes of org.apache.http.conn.ssl.SSLContextBuilder

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.