base.ssl.TrustingSslSocketFactory Maven / Gradle / Ivy
/**
* Creative commons Attribution-NonCommercial license.
*
* http://creativecommons.org/licenses/by-nc/2.5/au/deed.en_GB
*
* NO WARRANTY IS GIVEN OR IMPLIED, USE AT YOUR OWN RISK.
*/
package base.ssl;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.security.GeneralSecurityException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import javax.net.SocketFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
public class TrustingSslSocketFactory extends SocketFactory {
private static SocketFactory factory = null;
public static SocketFactory getDefault() {
return new TrustingSslSocketFactory();
}
public Socket createSocket(String arg0, int arg1) throws IOException {
return factory.createSocket(arg0, arg1);
}
public Socket createSocket(InetAddress arg0, int arg1) throws IOException {
return factory.createSocket(arg0, arg1);
}
public Socket createSocket(String arg0, int arg1, InetAddress arg2, int arg3) throws IOException {
return factory.createSocket(arg0, arg1, arg2, arg3);
}
public Socket createSocket(InetAddress arg0, int arg1, InetAddress arg2, int arg3) throws IOException {
return factory.createSocket(arg0, arg1, arg2, arg3);
}
/**
* Setup the default trusting SSL socket factory.
*/
static {
TrustManager[] trustManager = new TrustManager[] {
new X509TrustManager() {
public X509Certificate[] getAcceptedIssuers() { return null; }
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
// By not throwing an exception we indicate that we trust this certificate
}
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
// By not throwing an exception we indicate that we trust this server
}
}
};
// Create the socket factory that uses the trusting trust manager.
try {
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustManager, new java.security.SecureRandom());
factory = sc.getSocketFactory();
} catch (GeneralSecurityException e) {
// This should never happen. If it does there is nothing anyone can do about it
e.printStackTrace();
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy