Skip to content

Instantly share code, notes, and snippets.

@JAlexoid
Last active April 24, 2019 15:16
Show Gist options
  • Save JAlexoid/b15dba31e5919586ae51 to your computer and use it in GitHub Desktop.
Save JAlexoid/b15dba31e5919586ae51 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 ([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;
}
}
}
@anotherrohit
Copy link

can you tell me how to call this class in a main(). I want to use the config returned by this class.
Client c = ClientBuilder.newBuilder().sslContext(sc).hostnameVerifier(allHostsValid).build();
String baseUrl = "https://localhost:8088";
c.register(HttpAuthenticationFeature.basic(userName, passWord));
target = c.target(baseUrl);

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