-
-
Save venkyvb/0ef266c62f995c641d5f1956a27065af to your computer and use it in GitHub Desktop.
This file contains hidden or 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
package com.example.jersey; | |
import javax.net.ssl.*; | |
import javax.ws.rs.client.Client; | |
import javax.ws.rs.client.ClientBuilder; | |
import javax.ws.rs.core.Configuration; | |
import java.security.KeyManagementException; | |
import java.security.NoSuchAlgorithmException; | |
import java.security.SecureRandom; | |
import java.security.cert.CertificateException; | |
import java.security.cert.X509Certificate; | |
/** | |
* Created by Aleksandr Panzin ([email protected]) | |
*/ | |
public class JerseyWithSSL { | |
public Client initClient(Configuration config) throws NoSuchAlgorithmException, KeyManagementException { | |
SSLContext ctx = SSLContext.getInstance("SSL"); | |
ctx.init(null, certs, new SecureRandom()); | |
return ClientBuilder.newBuilder() | |
.withConfig(config) | |
.hostnameVerifier(new TrustAllHostNameVerifier()) | |
.sslContext(ctx) | |
.build(); | |
} | |
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 { | |
} | |
} | |
}; | |
public static class TrustAllHostNameVerifier implements HostnameVerifier { | |
public boolean verify(String hostname, SSLSession session) { | |
return true; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment