Last active
May 30, 2019 19:57
-
-
Save alpegon/6ad3ab45dbcdb2dbb51ac1ac82b8995b to your computer and use it in GitHub Desktop.
Jersey 2.x SSL client that trusts all certificates
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 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 | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hola! ¿Qué dependencia de jersey utilizaste?