Skip to content

Instantly share code, notes, and snippets.

@venkyvb
Forked from JAlexoid/JerseyWithSSL.java
Created January 13, 2018 02:27
Show Gist options
  • Save venkyvb/0ef266c62f995c641d5f1956a27065af to your computer and use it in GitHub Desktop.
Save venkyvb/0ef266c62f995c641d5f1956a27065af to your computer and use it in GitHub Desktop.
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 (alex@panz.in)
*/
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