Example usage for javax.net.ssl HttpsURLConnection setDefaultSSLSocketFactory

List of usage examples for javax.net.ssl HttpsURLConnection setDefaultSSLSocketFactory

Introduction

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

Prototype

public static void setDefaultSSLSocketFactory(SSLSocketFactory sf) 

Source Link

Document

Sets the default SSLSocketFactory inherited by new instances of this class.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
        public X509Certificate[] getAcceptedIssuers() {
            return null;
        }// ww  w.j  av a 2s  .  co m

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

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

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

    URL url = new URL("https://hostname/index.html");
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
        public java.security.cert.X509Certificate[] getAcceptedIssuers() {
            return null;
        }/*from   www  .  java2 s  .c  om*/

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

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

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

    URL url = new URL("https://hostname/index.html");
}

From source file:com.yahoo.athenz.example.http.tls.client.HttpTLSClient.java

public static void main(String[] args) {

    // parse our command line to retrieve required input

    CommandLine cmd = parseCommandLine(args);

    final String url = cmd.getOptionValue("url");
    final String keyPath = cmd.getOptionValue("key");
    final String certPath = cmd.getOptionValue("cert");
    final String trustStorePath = cmd.getOptionValue("trustStorePath");
    final String trustStorePassword = cmd.getOptionValue("trustStorePassword");

    // we are going to setup our service private key and
    // certificate into a ssl context that we can use with
    // our http client

    try {/*from  w w w.  jav  a2s  .  c  o  m*/
        KeyRefresher keyRefresher = Utils.generateKeyRefresher(trustStorePath, trustStorePassword, certPath,
                keyPath);
        SSLContext sslContext = Utils.buildSSLContext(keyRefresher.getKeyManagerProxy(),
                keyRefresher.getTrustManagerProxy());

        HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
        HttpsURLConnection con = (HttpsURLConnection) new URL(url).openConnection();
        con.setReadTimeout(15000);
        con.setDoOutput(true);
        con.connect();

        try (BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream()))) {
            StringBuilder sb = new StringBuilder();
            String line;
            while ((line = br.readLine()) != null) {
                sb.append(line);
            }
            System.out.println("Data output: " + sb.toString());
        }

    } catch (Exception ex) {
        System.out.println("Exception: " + ex.getMessage());
        ex.printStackTrace();
        System.exit(1);
    }
}

From source file:at.peppol.smp.client.console.SMPClient.java

public static void main(final String[] args) throws Exception {
    if (false) {/*from  w w w  .  ja v a2  s. c  o m*/
        // Enable this section in development mode, if you want to trust all HTTPS
        // certificates
        final SSLContext aSSLContext = SSLContext.getInstance("SSL");
        aSSLContext.init(null, new TrustManager[] { new DoNothingTrustManager() },
                VerySecureRandom.getInstance());
        HttpsURLConnection.setDefaultSSLSocketFactory(aSSLContext.getSocketFactory());
    }

    final SMPClientOptions aOptions = new SMPClientOptions();
    final CommandLine cmd = new PosixParser().parse(aOptions, args);

    ECommand eAction = null;
    boolean bGoodCmd = true;
    String cert = null;

    if (!cmd.hasOption("h")) {
        s_aLogger.error("No Host specified use -h to specify Host");
        bGoodCmd = false;
    }

    if (!cmd.hasOption("u")) {
        s_aLogger.error("No Username specified use -u to specify username");
        bGoodCmd = false;
    }

    if (!cmd.hasOption("p")) {
        s_aLogger.error("No Password specified use -p to specify password");
        bGoodCmd = false;
    }

    if (!cmd.hasOption("c")) {
        s_aLogger.error("No Action specified please use -c parameter to specify command("
                + ECommand.getAllAsString() + ")");
        bGoodCmd = false;
    } else {
        final String sCommand = cmd.getOptionValue("c");
        eAction = ECommand.getFromNameOrNull(sCommand);
        if (eAction == null) {
            s_aLogger.error("Illegal Action specified:" + sCommand + " allowed commands("
                    + ECommand.getAllAsString() + ")");
            bGoodCmd = false;
        } else
            switch (eAction) {
            case ADDGROUP:
                if (!cmd.hasOption("b")) {
                    s_aLogger.error(
                            "No Business/Participant ID specified use -b to specify Business/Participant ID");
                    bGoodCmd = false;
                }
                break;
            case DELGROUP:
                if (!cmd.hasOption("b")) {
                    s_aLogger.error(
                            "No Business/Participant ID specified use -b to specify Business/Participant ID");
                    bGoodCmd = false;
                }
                break;
            case ADD:
                if (!cmd.hasOption("a")) {
                    s_aLogger.error("No Accesspoint URL defined use -a to Specifify AP-URL");
                    bGoodCmd = false;
                }
                if (!cmd.hasOption("b")) {
                    s_aLogger.error(
                            "No Business/Participant ID specified use -b to specify Business/Participant ID");
                    bGoodCmd = false;
                }
                if (!cmd.hasOption("d")) {
                    s_aLogger.error("No DocumentType ID specified use -d to specify Document Type ID");
                    bGoodCmd = false;
                }
                if (!cmd.hasOption("r")) {
                    s_aLogger.error("No Process ID specified use -r to specify Process ID");
                    bGoodCmd = false;
                }
                if (!cmd.hasOption("e")) {
                    s_aLogger.error("No Certificate PEM file specified use -e to specify Certificate PEM file");
                    bGoodCmd = false;
                } else {
                    cert = SimpleFileIO.readFileAsString(new File(cmd.getOptionValue('e')),
                            CCharset.CHARSET_ISO_8859_1);
                }
                break;
            case DEL:
                if (!cmd.hasOption("b")) {
                    s_aLogger.error(
                            "No Business/Participant ID specified use -b to specify Business/Participant ID");
                    bGoodCmd = false;
                }
                if (!cmd.hasOption("d")) {
                    s_aLogger.error("No Document Type ID specified use -d to specify Document Type ID");
                    bGoodCmd = false;
                }
            }
    }

    if (!bGoodCmd) {
        final NonBlockingStringWriter aSW = new NonBlockingStringWriter();
        new HelpFormatter().printHelp(new PrintWriter(aSW), HelpFormatter.DEFAULT_WIDTH,
                CGStringHelper.getClassLocalName(SMPClient.class), null, aOptions,
                HelpFormatter.DEFAULT_LEFT_PAD, HelpFormatter.DEFAULT_DESC_PAD, null);
        s_aLogger.info(aSW.getAsString());
        System.exit(-3);
    }

    final SMPClient client = new SMPClient(new URI(cmd.getOptionValue('h')), cmd.getOptionValue('u'),
            cmd.getOptionValue('p'), cmd.getOptionValue('b'), cmd.getOptionValue('d'), cmd.getOptionValue('r'),
            cmd.getOptionValue('a'), cert);

    switch (eAction) {
    case ADDGROUP:
        client._createServiceGroup();
        break;
    case DELGROUP:
        client._deleteServiceGroup();
        break;
    case ADD:
        client._addDocument();
        break;
    case DEL:
        client._deleteDocument();
        break;
    case LIST:
        client._listDocuments();
        break;
    default:
        throw new IllegalStateException();
    }
}

From source file:Main.java

static public void DisableSecurity() throws GeneralSecurityException {
    SSLContext ctx = SSLContext.getInstance("TLS");
    ctx.init(null, new TrustManager[] { new X509TrustManager() {
        public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
        }//from   ww  w.j a v  a 2 s  . c o m

        public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
        }

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

    HttpsURLConnection.setDefaultSSLSocketFactory(ctx.getSocketFactory());

    HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
        public boolean verify(String hostname, SSLSession session) {
            return true;
        }
    });
}

From source file:Main.java

public static void disableSSLCertificateChecking() {
    TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
        public X509Certificate[] getAcceptedIssuers() {
            return null;
        }/*w  ww  .ja v a2  s .c om*/

        @Override
        public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
            // Not implemented
        }

        @Override
        public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
            // Not implemented
        }
    } };

    try {
        SSLContext sc = SSLContext.getInstance("TLS");

        sc.init(null, trustAllCerts, new java.security.SecureRandom());

        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
    } catch (KeyManagementException e) {
        e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

/**
 * Trust every server - don't check for any certificate only for testing!
 *//*from   ww  w.  j  a  v  a 2 s  .  co m*/
private static void trustAllHosts() {
    // Create a trust manager that does not validate certificate chains
    TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
        public X509Certificate[] getAcceptedIssuers() {
            return new X509Certificate[] {};
        }

        public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
        }

        public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
        }
    } };

    // Install the all-trusting trust manager
    try {
        SSLContext sc = SSLContext.getInstance("TLS");
        sc.init(null, trustAllCerts, new java.security.SecureRandom());
        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

/**
 * Disables the SSL certificate checking for new instances of {@link HttpsURLConnection} This has been created to
 * aid testing on a local box, not for use on production.
 *///from   ww  w.  j ava  2  s .  c  om
private static void disableSSLCertificateChecking() {
    TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
        public X509Certificate[] getAcceptedIssuers() {
            return null;
        }

        @Override
        public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
            // Not implemented
        }

        @Override
        public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
            // Not implemented
        }
    } };

    try {
        SSLContext sc = SSLContext.getInstance("TLS");

        sc.init(null, trustAllCerts, new java.security.SecureRandom());

        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
    } catch (KeyManagementException e) {
        e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void trustAllHosts(boolean trustAnyCert, boolean trustAnyHost) {
    try {/*  ww  w  . j  a  va2s .co m*/
        if (trustAnyCert) {
            X509TrustManager easyTrustManager = new X509TrustManager() {

                public void checkClientTrusted(X509Certificate[] chain, String authType)
                        throws CertificateException {
                    // Oh, I am easy!
                }

                public void checkServerTrusted(X509Certificate[] chain, String authType)
                        throws CertificateException {
                    // Oh, I am easy!
                }

                public X509Certificate[] getAcceptedIssuers() {
                    return null;
                }

            };

            // Create a trust manager that does not validate certificate chains
            TrustManager[] trustAllCerts = new TrustManager[] { easyTrustManager };

            // Install the all-trusting trust manager

            SSLContext sc = SSLContext.getInstance("TLS");

            sc.init(null, trustAllCerts, new java.security.SecureRandom());

            HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
        }

        if (trustAnyHost) {
            HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
                @Override
                public boolean verify(String hostname, SSLSession session) {
                    return true;
                }
            });
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

/**
 * Trust every server - dont check for any certificate
 *//*from   w  ww .  j a v a  2  s.  c o  m*/
private static void trustAllHosts() {
    // Create a trust manager that does not validate certificate chains
    TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
        public java.security.cert.X509Certificate[] getAcceptedIssuers() {
            return new java.security.cert.X509Certificate[] {};
        }

        @Override
        public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
            // TODO Auto-generated method stub

        }

        @Override
        public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
            // TODO Auto-generated method stub

        }
    } };

    // Install the all-trusting trust manager
    try {
        SSLContext sc = SSLContext.getInstance("TLS");
        sc.init(null, trustAllCerts, new java.security.SecureRandom());
        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());

        HttpsURLConnection.setDefaultHostnameVerifier(DO_NOT_VERIFY);
    } catch (Exception e) {
        e.printStackTrace();
    }
}