Example usage for javax.net.ssl SSLContext getSocketFactory

List of usage examples for javax.net.ssl SSLContext getSocketFactory

Introduction

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

Prototype

public final SSLSocketFactory getSocketFactory() 

Source Link

Document

Returns a SocketFactory object for this context.

Usage

From source file:com.dh.perfectoffer.event.framework.net.network.NetworkConnectionImpl.java

private static SSLSocketFactory getAllHostsValidSocketFactory()
        throws NoSuchAlgorithmException, KeyManagementException {
    if (sAllHostsValidSocketFactory == null) {
        TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
            public X509Certificate[] getAcceptedIssuers() {
                return null;
            }/*w  ww.j ava 2 s  .  com*/

            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());
        sAllHostsValidSocketFactory = sc.getSocketFactory();
    }

    return sAllHostsValidSocketFactory;
}

From source file:com.dh.superxz_bottom.framework.net.network.NetworkConnectionImpl.java

private static SSLSocketFactory getAllHostsValidSocketFactory()
        throws NoSuchAlgorithmException, KeyManagementException {
    if (sAllHostsValidSocketFactory == null) {
        TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
            public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                return null;
            }/*from w  w w.  j  av a 2  s  . com*/

            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());
        sAllHostsValidSocketFactory = sc.getSocketFactory();
    }

    return sAllHostsValidSocketFactory;
}

From source file:org.sharegov.cirm.StartUp.java

public static void disableCertificateValidation() {
    // Create a trust manager that does not validate certificate chains
    TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
        public X509Certificate[] getAcceptedIssuers() {
            return new X509Certificate[0];
        }/*from w ww  .j a  va2 s . c o m*/

        public void checkClientTrusted(X509Certificate[] certs, String authType) {
            // System.out.println("Check client trusted");
        }

        public void checkServerTrusted(X509Certificate[] certs, String authType) {
            // System.out.println("Check server trusted");

        }
    } };

    // Ignore differences between given hostname and certificate hostname
    HostnameVerifier hv = new HostnameVerifier() {
        @Override
        public boolean verify(String hostname, SSLSession session) {
            // TODO Auto-generated method stub
            return true;
        }
    };

    // Install the all-trusting trust manager
    try {
        // SSLContext sc = SSLContext.getInstance("SSL");
        // sc.init(null, trustAllCerts, new SecureRandom());
        SSLContext ctx = SSLContext.getInstance("TLS");
        ctx.init(new KeyManager[0], trustAllCerts, new SecureRandom());
        HttpsURLConnection.setDefaultSSLSocketFactory(ctx.getSocketFactory());
        HttpsURLConnection.setDefaultHostnameVerifier(hv);
        // see:http://stackoverflow.com/questions/1828775/how-to-handle-invalid-ssl-certificates-with-apache-httpclient
        // see:https://code.google.com/p/jsslutils/wiki/ApacheHttpClientUsage
        org.apache.commons.httpclient.protocol.Protocol.registerProtocol("https",
                new org.apache.commons.httpclient.protocol.Protocol("https",
                        (ProtocolSocketFactory) new SslContextedSecureProtocolSocketFactory(ctx, false), 443));
        SSLContext.setDefault(ctx);
    } catch (Exception e) {
    }
}

From source file:org.rhq.enterprise.server.plugins.rhnhosted.RHNSSLSocketFactory.java

/**
 *
 * @param sslCerts these certs will be used to validate the ssl connection
 * @return/*from w  w  w.  j  av a2  s .  c o m*/
 * @throws IOException
 * @throws GeneralSecurityException
 */
static public SSLSocketFactory getSSLSocketFactory(List<X509Certificate> sslCerts)
        throws IOException, GeneralSecurityException {
    SSLContext sc = null;
    KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
    ks.load(null); //Important, this intializes the keystore
    int counter = 0;
    for (X509Certificate cert : sslCerts) {
        ks.setCertificateEntry("rhn-key-" + counter, cert);
        counter++;
    }
    sc = SSLContext.getInstance("SSL");
    TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
    tmf.init(ks);
    sc.init(null, tmf.getTrustManagers(), new java.security.SecureRandom());
    return sc.getSocketFactory();
}

From source file:org.jevis.commons.driver.DataSourceHelper.java

static public void doTrustToCertificates() throws Exception {
    Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
    TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
        public X509Certificate[] getAcceptedIssuers() {
            return null;
        }//from w  ww.  ja v a  2  s.  com

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

        public void checkClientTrusted(X509Certificate[] certs, String authType) throws CertificateException {
            return;
        }
    } };

    SSLContext sc = SSLContext.getInstance("SSL");
    sc.init(null, trustAllCerts, new SecureRandom());
    HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
    HostnameVerifier hv = new HostnameVerifier() {
        public boolean verify(String urlHostName, SSLSession session) {
            if (!urlHostName.equalsIgnoreCase(session.getPeerHost())) {
                System.out.println("Warning: URL host '" + urlHostName + "' is different to SSLSession host '"
                        + session.getPeerHost() + "'.");
            }
            return true;
        }
    };
    HttpsURLConnection.setDefaultHostnameVerifier(hv);
}

From source file:com.hybris.mobile.data.WebServiceDataProvider.java

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[] {};
        }/*w w w .  j av  a2s .co m*/

        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) {
        LoggingUtils.e(LOG_TAG, "Error with SSL connection. " + e.getLocalizedMessage(), null);
    }
}

From source file:com.android.volley.toolbox.http.HurlStack.java

public static javax.net.ssl.SSLSocketFactory getSSLSocketFactory() {
    javax.net.ssl.SSLSocketFactory sslSocketFactory = null;
    SSLContext context = null;
    try {/*  www .jav  a 2  s  . c o m*/
        // Create an SSLContext that uses our TrustManager
        context = SSLContext.getInstance("TLS");

        TrustManager[] tm = { new JindunX509TrustManager() };

        context.init(null, tm, null);
        sslSocketFactory = context.getSocketFactory();
    } catch (Exception e) {
        e.printStackTrace();
        //?
        sslSocketFactory = context.getSocketFactory();
    }
    return sslSocketFactory;
}

From source file:com.nubits.nubot.utils.Utils.java

/**
 * Install a trust manager that does not validate certificate chains for https calls
 *
 * @throws Exception//w ww .  j a va 2 s .c  om
 */
private static void installTrustAllManager() throws Exception {

    // Create a trust manager that does not validate certificate chains
    TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
        public java.security.cert.X509Certificate[] getAcceptedIssuers() {
            return null;
        }

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

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

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

    // Create all-trusting host name verifier
    HostnameVerifier allHostsValid = new HostnameVerifier() {
        public boolean verify(String hostname, SSLSession session) {
            return true;
        }
    };
    // Install the all-trusting host verifier
    HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
}

From source file:com.gmobi.poponews.util.HttpHelper.java

static void disableSslCheck() {
    if (initialized)
        return;/*from   w w  w  .j  av a 2  s .  com*/
    TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
        public X509Certificate[] getAcceptedIssuers() {
            return null;
        }

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

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

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

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

        // Create all-trusting host name verifier
        HostnameVerifier allHostsValid = new HostnameVerifier() {
            public boolean verify(String hostname, SSLSession session) {
                return true;
            }
        };

        // Install the all-trusting host verifier
        HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
        initialized = true;
    } catch (Exception e) {
        Logger.error(e);
    }

}

From source file:com.glaf.core.util.http.HttpUtils.java

/**
 * ?https?//from  w  w  w  .j a  va 2s .  com
 * 
 * @param requestUrl
 *            ?
 * @param method
 *            ?GET?POST
 * @param content
 *            ???
 * @return
 */
public static String doRequest(String requestUrl, String method, String content, boolean isSSL) {
    log.debug("requestUrl:" + requestUrl);
    HttpsURLConnection conn = null;
    InputStream inputStream = null;
    BufferedReader bufferedReader = null;
    InputStreamReader inputStreamReader = null;
    StringBuffer buffer = new StringBuffer();
    try {
        URL url = new URL(requestUrl);
        conn = (HttpsURLConnection) url.openConnection();
        if (isSSL) {
            // SSLContext??
            TrustManager[] tm = { new MyX509TrustManager() };
            SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE");
            sslContext.init(null, tm, new java.security.SecureRandom());
            // SSLContextSSLSocketFactory
            SSLSocketFactory ssf = sslContext.getSocketFactory();
            conn.setSSLSocketFactory(ssf);
        }
        conn.setDoOutput(true);
        conn.setDoInput(true);
        conn.setUseCaches(false);
        // ?GET/POST
        conn.setRequestMethod(method);
        if ("GET".equalsIgnoreCase(method)) {
            conn.connect();
        }

        // ????
        if (StringUtils.isNotEmpty(content)) {
            OutputStream outputStream = conn.getOutputStream();
            // ????
            outputStream.write(content.getBytes("UTF-8"));
            outputStream.flush();
            outputStream.close();
        }

        // ???
        inputStream = conn.getInputStream();
        inputStreamReader = new InputStreamReader(inputStream, "UTF-8");
        bufferedReader = new BufferedReader(inputStreamReader);

        String str = null;
        while ((str = bufferedReader.readLine()) != null) {
            buffer.append(str);
        }

        log.debug("response:" + buffer.toString());

    } catch (ConnectException ce) {
        ce.printStackTrace();
        log.error(" http server connection timed out.");
    } catch (Exception ex) {
        ex.printStackTrace();
        log.error("http request error:{}", ex);
    } finally {
        IOUtils.closeQuietly(inputStream);
        IOUtils.closeQuietly(bufferedReader);
        IOUtils.closeQuietly(inputStreamReader);
        if (conn != null) {
            conn.disconnect();
        }
    }
    return buffer.toString();
}