Skip to content

Instantly share code, notes, and snippets.

@alpegon
Last active May 30, 2019 19:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alpegon/6ad3ab45dbcdb2dbb51ac1ac82b8995b to your computer and use it in GitHub Desktop.
Save alpegon/6ad3ab45dbcdb2dbb51ac1ac82b8995b to your computer and use it in GitHub Desktop.
Jersey 2.x SSL client that trusts all certificates
public class SslTrustAllRestClient implements RestClient {
private TrustManager[] getTrustManager() {
return new TrustManager[] { new X509TrustManager() {
@Override
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}
@Override
public void checkServerTrusted(X509Certificate[] chain, String authType)
throws CertificateException {
// Trust all servers
}
@Override
public void checkClientTrusted(X509Certificate[] chain, String authType)
throws CertificateException {
// Trust all clients
}
} };
}
/**
* Build a new Rest client with SSL security that trusts all certificates in
* SSL/TLS.
*
* @return : new REST client
* @throws ImClientException
* : generic exception in the rest client
*/
@Override
public Client createClient() throws ImClientException {
try {
SSLContext ctx = SSLContext.getInstance("TLS");
ctx.init(null, getTrustManager(), new SecureRandom());
HostnameVerifier verifier = new HostnameVerifier() {
@Override
public boolean verify(String hostName, SSLSession sslSession) {
return true;
}
};
return ClientBuilder.newBuilder().sslContext(ctx).hostnameVerifier(verifier)
.build();
} catch (GeneralSecurityException exception) {
// Log (or/and) throw exception
}
}
}
@palucero
Copy link

Hola! ¿Qué dependencia de jersey utilizaste?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment