Example usage for javax.net.ssl SSLContext init

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

Introduction

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

Prototype

public final void init(KeyManager[] km, TrustManager[] tm, SecureRandom random) throws KeyManagementException 

Source Link

Document

Initializes this context.

Usage

From source file:com.wx.kernel.util.HttpKit.java

private static SSLSocketFactory initSSLSocketFactory() {
    try {//from  ww w.j a  v  a 2s .c o  m
        TrustManager[] tm = { new HttpKit().new TrustAnyTrustManager() };
        SSLContext sslContext = SSLContext.getInstance("TLS", "SunJSSE");
        sslContext.init(null, tm, new java.security.SecureRandom());
        return sslContext.getSocketFactory();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:com.baasbox.android.HttpUrlConnectionClient.java

private static SSLSocketFactory createSocketFactory(Context context, int certStoreId, String certPassword) {
    TrustManagerFactory tmf;/*from  w  w w  .  j ava2s .  co  m*/
    InputStream in = null;
    try {
        in = context.getResources().openRawResource(certStoreId);
        KeyStore keyStore = KeyStore.getInstance("BKS");
        keyStore.load(in, certPassword.toCharArray());

        tmf = TrustManagerFactory.getInstance("X509");
        tmf.init(keyStore);

        SSLContext sslContext = SSLContext.getInstance("TLS");
        sslContext.init(null, tmf.getTrustManagers(), null);

        return sslContext.getSocketFactory();
    } catch (Exception e) {
        throw new BaasRuntimeException(e);
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
                // swallow
            }
        }
    }
}

From source file:com.jaspersoft.ireport.jasperserver.ws.IReportSSLSocketFactory.java

private static SSLContext createIRSSLContext() {

    try {//from  w w  w .j a v a 2 s  .c  o m

        SSLContext context = SSLContext.getInstance("SSL");

        context.init(null, new TrustManager[] { new IReportTrustManager() }, null);

        return context;

    } catch (Exception ex) {

        ex.printStackTrace();

        throw new HttpClientError(ex.toString());

    }

}

From source file:com.longtime.ajy.support.weixin.HttpsKit.java

/**
 * ??Get/*from ww w  .j  av a2s  .  co  m*/
 * 
 * @param url
 * @return
 * @throws NoSuchProviderException
 * @throws NoSuchAlgorithmException
 * @throws IOException
 * @throws KeyManagementException
 */
public static String get(String url) {//throws NoSuchAlgorithmException, NoSuchProviderException, IOException, KeyManagementException {
    InputStream in = null;
    HttpsURLConnection http = null;

    try {
        StringBuffer bufferRes = null;
        TrustManager[] tm = { new MyX509TrustManager() };
        SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE");
        sslContext.init(null, tm, new java.security.SecureRandom());
        // SSLContextSSLSocketFactory  
        SSLSocketFactory ssf = sslContext.getSocketFactory();

        URL urlGet = new URL(url);
        http = (HttpsURLConnection) urlGet.openConnection();
        // 
        http.setConnectTimeout(TIME_OUT_CONNECT);
        // ? --??
        http.setReadTimeout(TIME_OUT_READ);
        http.setRequestMethod("GET");
        http.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        http.setSSLSocketFactory(ssf);
        http.setDoOutput(true);
        http.setDoInput(true);
        http.connect();

        in = http.getInputStream();
        BufferedReader read = new BufferedReader(new InputStreamReader(in, DEFAULT_CHARSET));
        String valueString = null;
        bufferRes = new StringBuffer();
        while ((valueString = read.readLine()) != null) {
            bufferRes.append(valueString);
        }
        return bufferRes.toString();
    } catch (Exception e) {
        logger.error(String.format("HTTP GET url=[%s] due to fail.", url), e);
    } finally {
        if (null != in) {
            try {
                in.close();
            } catch (IOException e) {
                logger.error(String.format("HTTP GET url=[%s] close inputstream due to fail.", url), e);
            }
        }
        if (http != null) {
            // 
            http.disconnect();
        }
    }

    return StringUtils.EMPTY;

}

From source file:com.example.zch.imspeak.utils.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  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) {
            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:com.lidroid.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  va 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) {
            }
        } };
        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:io.wcm.caravan.commons.httpclient.impl.helpers.CertificateLoader.java

/**
 * Creates default SSL context.//  w  w  w. j  a v  a 2s  . c  o  m
 * @return SSL context
 */
public static SSLContext createDefaultSSlContext() throws SSLInitializationException {
    try {
        final SSLContext sslcontext = SSLContext.getInstance(SSL_CONTEXT_TYPE_DEFAULT);
        sslcontext.init(null, null, null);
        return sslcontext;
    } catch (NoSuchAlgorithmException ex) {
        throw new SSLInitializationException(ex.getMessage(), ex);
    } catch (KeyManagementException ex) {
        throw new SSLInitializationException(ex.getMessage(), ex);
    }
}

From source file:io.wcm.caravan.commons.httpclient.impl.helpers.CertificateLoader.java

/**
 * Build SSL Socket factory./* w w  w.j a  v a2 s  .  c o m*/
 * @param config Http client configuration
 * @return SSL socket factory.
 * @throws IOException
 * @throws GeneralSecurityException
 */
public static SSLContext buildSSLContext(HttpClientConfig config) throws IOException, GeneralSecurityException {

    KeyManagerFactory kmf = null;
    if (isSslKeyManagerEnabled(config)) {
        kmf = getKeyManagerFactory(config.getKeyStorePath(), new StoreProperties(config.getKeyStorePassword(),
                config.getKeyManagerType(), config.getKeyStoreType()));
    }
    TrustManagerFactory tmf = null;
    if (isSslTrustStoreEnbaled(config)) {
        StoreProperties storeProperties = new StoreProperties(config.getTrustStorePassword(),
                config.getTrustManagerType(), config.getTrustStoreType());
        tmf = getTrustManagerFactory(config.getTrustStorePath(), storeProperties);
    }

    SSLContext sslContext = SSLContext.getInstance(config.getSslContextType());
    sslContext.init(kmf != null ? kmf.getKeyManagers() : null, tmf != null ? tmf.getTrustManagers() : null,
            null);

    return sslContext;
}

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  v a2  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) {
            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.sana.net.http.ssl.EasySSLSocketFactory.java

private static SSLContext createEasySSLContext() throws IOException {
    try {//from   w ww.  j av a  2s  .  c o m
        SSLContext context = SSLContext.getInstance("TLS");

        // Create a trust manager that does not validate certificate chains     
        context.init(null, new TrustManager[] { new EasyX509TrustManager(null) }, new SecureRandom());
        return context;
    } catch (Exception e) {
        throw new IOException(e.getMessage());
    }
}