Created
July 7, 2011 13:08
-
-
Save outbounder/1069465 to your computer and use it in GitHub Desktop.
jersey client helper for trusting all certificates in SSL/TLS
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
import java.security.SecureRandom; | |
import java.security.cert.CertificateException; | |
import java.security.cert.X509Certificate; | |
import javax.net.ssl.HostnameVerifier; | |
import javax.net.ssl.HttpsURLConnection; | |
import javax.net.ssl.SSLContext; | |
import javax.net.ssl.SSLSession; | |
import javax.net.ssl.TrustManager; | |
import javax.net.ssl.X509TrustManager; | |
import com.sun.jersey.api.client.Client; | |
import com.sun.jersey.api.client.config.ClientConfig; | |
import com.sun.jersey.api.client.config.DefaultClientConfig; | |
import com.sun.jersey.client.urlconnection.HTTPSProperties; | |
public class ClientHelper { | |
public static ClientConfig configureClient() { | |
TrustManager[ ] certs = new TrustManager[ ] { | |
new X509TrustManager() { | |
@Override | |
public X509Certificate[] getAcceptedIssuers() { | |
return null; | |
} | |
@Override | |
public void checkServerTrusted(X509Certificate[] chain, String authType) | |
throws CertificateException { | |
} | |
@Override | |
public void checkClientTrusted(X509Certificate[] chain, String authType) | |
throws CertificateException { | |
} | |
} | |
}; | |
SSLContext ctx = null; | |
try { | |
ctx = SSLContext.getInstance("TLS"); | |
ctx.init(null, certs, new SecureRandom()); | |
} catch (java.security.GeneralSecurityException ex) { | |
} | |
HttpsURLConnection.setDefaultSSLSocketFactory(ctx.getSocketFactory()); | |
ClientConfig config = new DefaultClientConfig(); | |
try { | |
config.getProperties().put(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES, new HTTPSProperties( | |
new HostnameVerifier() { | |
@Override | |
public boolean verify(String hostname, SSLSession session) { | |
return true; | |
} | |
}, | |
ctx | |
)); | |
} catch(Exception e) { | |
} | |
return config; | |
} | |
public static Client createClient() { | |
return Client.create(ClientHelper.configureClient()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For jersey 2.x: