Example usage for javax.net.ssl X509TrustManager X509TrustManager

List of usage examples for javax.net.ssl X509TrustManager X509TrustManager

Introduction

In this page you can find the example usage for javax.net.ssl X509TrustManager X509TrustManager.

Prototype

X509TrustManager

Source Link

Usage

From source file:www.ht.com.app.tools.OtherUtils.java

public static void trustAllHttpsURLConnection() {
    // Create a trust manager that does not validate certificate chains
    if (sslSocketFactory == null) {
        TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
            @Override//from  w w w  .  j a v a  2  s  . co  m
            public X509Certificate[] getAcceptedIssuers() {
                return null;
            }

            @Override
            public void checkClientTrusted(X509Certificate[] certs, String authType) {
            }

            @Override
            public void checkServerTrusted(X509Certificate[] certs, String authType) {
            }
        } };
        try {
            SSLContext sslContext = SSLContext.getInstance("TLS");
            sslContext.init(null, trustAllCerts, null);
            sslSocketFactory = sslContext.getSocketFactory();
        } catch (Throwable e) {
            Logger.e(e.getMessage(), e);
        }
    }

    if (sslSocketFactory != null) {
        HttpsURLConnection.setDefaultSSLSocketFactory(sslSocketFactory);
        HttpsURLConnection.setDefaultHostnameVerifier(
                org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
    }
}

From source file:com.addbean.autils.tools.OtherUtils.java

public static void trustAllHttpsURLConnection() {
    // Create a trust manager that does not validate certificate chains
    if (sslSocketFactory == null) {
        TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
            @Override/*from w  ww  . ja va2 s.c  om*/
            public X509Certificate[] getAcceptedIssuers() {
                return null;
            }

            @Override
            public void checkClientTrusted(X509Certificate[] certs, String authType) {
            }

            @Override
            public void checkServerTrusted(X509Certificate[] certs, String authType) {
            }
        } };
        try {
            SSLContext sslContext = SSLContext.getInstance("TLS");
            sslContext.init(null, trustAllCerts, null);
            sslSocketFactory = sslContext.getSocketFactory();
        } catch (Throwable e) {
            Log.e("OtherUtils", e.getMessage());
        }
    }

    if (sslSocketFactory != null) {
        HttpsURLConnection.setDefaultSSLSocketFactory(sslSocketFactory);
        HttpsURLConnection.setDefaultHostnameVerifier(
                org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
    }
}

From source file:org.esxx.js.protocol.HTTPHandler.java

private static synchronized ClientConnectionManager getConnectionManager() {
    if (connectionManager == null) {
        SchemeRegistry sr = new SchemeRegistry();
        sr.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
        //      sr.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));

        try {/*from w  w w.  j a v a2s.co m*/
            SSLContext sslcontext = SSLContext.getInstance(SSLSocketFactory.TLS);
            sslcontext.init(null, new TrustManager[] { new X509TrustManager() {
                @Override
                public void checkServerTrusted(X509Certificate[] chain, String auth) {
                }

                @Override
                public X509Certificate[] getAcceptedIssuers() {
                    return new X509Certificate[0];
                }

                @Override
                public void checkClientTrusted(X509Certificate[] certs, String auth) {
                }
            } }, new java.security.SecureRandom());

            SSLSocketFactory ssf = new SSLSocketFactory(sslcontext, null);
            ssf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
            sr.register(new Scheme("https", ssf, 443));
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        connectionManager = new ThreadSafeClientConnManager(getHttpParams(), sr);
    }

    return connectionManager;
}

From source file:net.tatans.rhea.network.view.OtherUtils.java

public static void trustAllHttpsURLConnection() {
    // Create a trust manager that does not validate certificate chains
    if (sslSocketFactory == null) {
        TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
            @Override/*from  w  ww  .  java 2  s  . c o  m*/
            public X509Certificate[] getAcceptedIssuers() {
                return null;
            }

            @Override
            public void checkClientTrusted(X509Certificate[] certs, String authType) {
            }

            @Override
            public void checkServerTrusted(X509Certificate[] certs, String authType) {
            }
        } };
        try {
            SSLContext sslContext = SSLContext.getInstance("TLS");
            sslContext.init(null, trustAllCerts, null);
            sslSocketFactory = sslContext.getSocketFactory();
        } catch (Throwable e) {
            TatansLogUtils.e(e.getMessage(), e);
        }
    }

    if (sslSocketFactory != null) {
        HttpsURLConnection.setDefaultSSLSocketFactory(sslSocketFactory);
        HttpsURLConnection.setDefaultHostnameVerifier(
                org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
    }
}

From source file:wsattacker.plugin.dos.dosExtension.requestSender.RequestSenderImpl.java

private String sendRequestHttpClient(RequestObject requestObject) {

    // get Post Request
    HttpPost post = this.createHttpPostMethod(requestObject);

    // set afterReceive to default value to handle missing responses
    afterReceive = 0;/*from   w w  w  .java 2 s . c  o m*/

    // Get HTTP client and execute request
    try {
        URL url = new URL(requestObject.getEndpoint());
        String protocol = url.getProtocol();

        HttpClient httpClient;
        if (protocol.equalsIgnoreCase("https")) {
            SSLContext ctx = SSLContext.getInstance("TLS");
            X509TrustManager tm = new X509TrustManager() {
                @Override
                public void checkClientTrusted(X509Certificate[] xcs, String string)
                        throws CertificateException {
                }

                @Override
                public void checkServerTrusted(X509Certificate[] xcs, String string)
                        throws CertificateException {
                }

                @Override
                public X509Certificate[] getAcceptedIssuers() {
                    return null;
                }
            };
            ctx.init(null, new TrustManager[] { tm }, null);

            SSLSocketFactory sf = new SSLSocketFactory(ctx, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
            Scheme httpsScheme = new Scheme("https", url.getPort(), sf);
            SchemeRegistry schemeRegistry = new SchemeRegistry();
            schemeRegistry.register(httpsScheme);

            // apache HttpClient version >4.2 should use
            // BasicClientConnectionManager
            ClientConnectionManager cm = new SingleClientConnManager(schemeRegistry);

            httpClient = new DefaultHttpClient(cm);
        } else {
            httpClient = new DefaultHttpClient();
        }

        httpClient.getParams().setParameter("http.socket.timeout", TIMEOUT);
        httpClient.getParams().setParameter("http.connection.timeout", TIMEOUT);
        httpClient.getParams().setParameter("http.connection-manager.max-per-host", TIMEOUT);
        httpClient.getParams().setParameter("http.connection-manager.max-total", new Integer(3000));
        // > params.setDefaultMaxConnectionsPerHost(3000);
        // > params.setMaxTotalConnections(3000);

        beforeSend = System.nanoTime();

        HttpResponse response = httpClient.execute(post);
        StringWriter writer = new StringWriter();
        IOUtils.copy(response.getEntity().getContent(), writer, "UTF-8");
        responseString = writer.toString();

        afterReceive = System.nanoTime();
        // System.out.println("Response status code: " + result);
        // System.out.println("Response body: " + responseString);
    } catch (IOException ex) {
        // Logger.getLogger(RequestSender.class.getName()).log(Level.SEVERE,
        // null, ex);
        System.out.println("--RequestSender - IO Exception: " + ex.getMessage());

        // ex.printStackTrace();
    } catch (Exception e) {
        // Request timed out!?
        System.out.println("--RequestSender - unexpected Exception: " + e.getMessage());
    } finally {
        // Release current connection to the connection pool
        // post.releaseConnection();

        if (responseString == null) {
            responseString = "";
        }

        // Set afterReceive to beforeSend if afterReceive is 0 so that there 
        // is no huge negative response time when the web service doesn't answer
        if (afterReceive == 0) {
            afterReceive = beforeSend;
        }
    }

    return responseString;
}

From source file:org.eclipse.dirigible.ide.common.io.ProxyUtils.java

private static SSLContext createTrustAllSSLContext() throws NoSuchAlgorithmException, KeyManagementException {
    SSLContext sslContext = SSLContext.getInstance("SSL");

    // Create a trust manager that does not validate certificate chains
    TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
        @Override/*from  w w  w .  jav  a  2 s  .c o  m*/
        public java.security.cert.X509Certificate[] getAcceptedIssuers() {
            return null;
        }

        @Override
        public void checkClientTrusted(X509Certificate[] certs, String authType) {
        }

        @Override
        public void checkServerTrusted(X509Certificate[] certs, String authType) {
        }
    } };

    // Set up a TrustManager that trusts everything
    sslContext.init(null, trustAllCerts, new SecureRandom());
    return sslContext;
}

From source file:cn.isif.util_plus.util.OtherUtils.java

public static void trustAllHttpsURLConnection() {
    // Create a trust manager that does not validate certificate chains
    if (sslSocketFactory == null) {
        TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
            @Override/*from   ww w.  j a v  a  2s  .co m*/
            public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                return null;
            }

            @Override
            public void checkClientTrusted(X509Certificate[] certs, String authType) {
            }

            @Override
            public void checkServerTrusted(X509Certificate[] certs, String authType) {
            }
        } };
        try {
            SSLContext sslContext = SSLContext.getInstance("TLS");
            sslContext.init(null, trustAllCerts, null);
            sslSocketFactory = sslContext.getSocketFactory();
        } catch (Throwable e) {
            LogUtils.e(e.getMessage(), e);
        }
    }

    if (sslSocketFactory != null) {
        HttpsURLConnection.setDefaultSSLSocketFactory(sslSocketFactory);
        HttpsURLConnection.setDefaultHostnameVerifier(
                org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
    }
}

From source file:JMeter.plugins.functional.samplers.geoevent.HttpJsonToStreamServiceSampler.java

private void setupTarget() {
    try {// w w w .ja va2  s .c  om
        // Send the Message to Rest Endpoint
        String urlString = getRestInputURL();
        //System.out.println(urlString);

        URL url = new URL(urlString);

        Client client;

        if (url.getProtocol().equalsIgnoreCase("https")) {
            TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
                @Override
                public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                    return new X509Certificate[0];
                }

                @Override
                public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType) {
                }

                @Override
                public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType) {
                }
            } };
            SSLContext sc = null;

            sc = SSLContext.getInstance("SSL");
            sc.init(null, trustAllCerts, new java.security.SecureRandom());

            client = ClientBuilder.newBuilder().sslContext(sc).build();
        } else {
            client = ClientBuilder.newClient();
        }

        target = client.target(urlString);
    } catch (MalformedURLException ex) {
        java.util.logging.Logger.getLogger(HttpJsonToStreamServiceSampler.class.getName()).log(Level.SEVERE,
                null, ex);
        ex.printStackTrace();
    } catch (NoSuchAlgorithmException ex) {
        java.util.logging.Logger.getLogger(HttpJsonToStreamServiceSampler.class.getName()).log(Level.SEVERE,
                null, ex);
        ex.printStackTrace();
    } catch (KeyManagementException ex) {
        java.util.logging.Logger.getLogger(HttpJsonToStreamServiceSampler.class.getName()).log(Level.SEVERE,
                null, ex);
        ex.printStackTrace();
    }
}

From source file:de.codecentric.boot.admin.zuul.filters.route.SimpleHostRoutingFilter.java

protected PoolingHttpClientConnectionManager newConnectionManager() {
    try {//from w w  w. j  a v  a 2 s. co  m
        final SSLContext sslContext = SSLContext.getInstance("SSL");
        sslContext.init(null, new TrustManager[] { new X509TrustManager() {
            @Override
            public void checkClientTrusted(X509Certificate[] x509Certificates, String s)
                    throws CertificateException {
            }

            @Override
            public void checkServerTrusted(X509Certificate[] x509Certificates, String s)
                    throws CertificateException {
            }

            @Override
            public X509Certificate[] getAcceptedIssuers() {
                return null;
            }
        } }, new SecureRandom());

        RegistryBuilder<ConnectionSocketFactory> registryBuilder = RegistryBuilder
                .<ConnectionSocketFactory>create().register("http", PlainConnectionSocketFactory.INSTANCE);
        if (this.sslHostnameValidationEnabled) {
            registryBuilder.register("https", new SSLConnectionSocketFactory(sslContext));
        } else {
            registryBuilder.register("https",
                    new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE));
        }
        final Registry<ConnectionSocketFactory> registry = registryBuilder.build();

        this.connectionManager = new PoolingHttpClientConnectionManager(registry);
        this.connectionManager.setMaxTotal(this.hostProperties.getMaxTotalConnections());
        this.connectionManager.setDefaultMaxPerRoute(this.hostProperties.getMaxPerRouteConnections());
        return this.connectionManager;
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
}

From source file:com.simiacryptus.util.Util.java

/**
 * Get input stream.//from  w w  w  . jav a2s. c om
 *
 * @param url the url
 * @return the input stream
 * @throws NoSuchAlgorithmException the no such algorithm exception
 * @throws KeyManagementException   the key management exception
 * @throws IOException              the io exception
 */
public static InputStream get(@javax.annotation.Nonnull String url)
        throws NoSuchAlgorithmException, KeyManagementException, IOException {
    @javax.annotation.Nonnull
    final TrustManager[] trustManagers = { new X509TrustManager() {
        @Override
        public void checkClientTrusted(final X509Certificate[] certs, final String authType) {
        }

        @Override
        public void checkServerTrusted(final X509Certificate[] certs, final String authType) {
        }

        @javax.annotation.Nonnull
        @Override
        public X509Certificate[] getAcceptedIssuers() {
            return new X509Certificate[0];
        }
    } };
    @javax.annotation.Nonnull
    final SSLContext ctx = SSLContext.getInstance("TLS");
    ctx.init(null, trustManagers, null);
    final SSLSocketFactory sslFactory = ctx.getSocketFactory();
    final URLConnection urlConnection = new URL(url).openConnection();
    if (urlConnection instanceof HttpsURLConnection) {
        @javax.annotation.Nonnull
        final HttpsURLConnection conn = (HttpsURLConnection) urlConnection;
        conn.setSSLSocketFactory(sslFactory);
        conn.setRequestMethod("GET");
    }
    return urlConnection.getInputStream();
}