Created
August 18, 2015 07:21
-
-
Save chathudan/59b2efe4743787b8a7e1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class TrustedSSLSocketFactory extends SSLSocketFactory { | |
SSLContext sslContext = SSLContext.getInstance("TLS"); | |
public TrustedSSLSocketFactory(KeyStore truststore) | |
throws NoSuchAlgorithmException, KeyManagementException, | |
KeyStoreException, UnrecoverableKeyException { | |
super(truststore); | |
TrustManager tm = new X509TrustManager() { | |
public void checkClientTrusted(X509Certificate[] chain, | |
String authType) throws CertificateException { | |
} | |
public void checkServerTrusted(X509Certificate[] chain, | |
String authType) throws CertificateException { | |
} | |
public X509Certificate[] getAcceptedIssuers() { | |
return null; | |
} | |
}; | |
sslContext.init(null, new TrustManager[] { tm }, null); | |
} | |
@Override | |
public Socket createSocket(Socket socket, String host, int port, | |
boolean autoClose) throws IOException, UnknownHostException { | |
return sslContext.getSocketFactory().createSocket(socket, host, | |
port, autoClose); | |
} | |
@Override | |
public Socket createSocket() throws IOException { | |
return sslContext.getSocketFactory().createSocket(); | |
} | |
} | |
private static SSLSocketFactory getSSLSocketFactory() { | |
KeyStore trustStore; | |
SSLSocketFactory sslSocketFactory = null; | |
try { | |
trustStore = KeyStore.getInstance(KeyStore.getDefaultType()); | |
trustStore.load(null, null); | |
sslSocketFactory = new TrustedSSLSocketFactory(trustStore); | |
sslSocketFactory | |
.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); | |
} catch (KeyStoreException e) { | |
e.printStackTrace(); | |
} catch (NoSuchAlgorithmException e) { | |
e.printStackTrace(); | |
} catch (CertificateException e) { | |
e.printStackTrace(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} catch (KeyManagementException e) { | |
e.printStackTrace(); | |
} catch (UnrecoverableKeyException e) { | |
} | |
return sslSocketFactory; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment