Ignore self-signed ssl cert using Jersey Client [duplicate] Ignore self-signed ssl cert using Jersey Client [duplicate] java java

Ignore self-signed ssl cert using Jersey Client [duplicate]


After some searching and trawling through some old stackoverflow questions I've found a solution in a previously asked SO question:

Here's the code that I ended up using.

// Create a trust manager that does not validate certificate chainsTrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager(){    public X509Certificate[] getAcceptedIssuers(){return null;}    public void checkClientTrusted(X509Certificate[] certs, String authType){}    public void checkServerTrusted(X509Certificate[] certs, String authType){}}};// Install the all-trusting trust managertry {    SSLContext sc = SSLContext.getInstance("TLS");    sc.init(null, trustAllCerts, new SecureRandom());    HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());} catch (Exception e) {    ;}


For Jersey 2.* (Tested on 2.7) and java 8:

import java.security.cert.CertificateException; import java.security.cert.X509Certificate; import javax.net.ssl.SSLContext; import javax.net.ssl.TrustManager; import javax.net.ssl.X509TrustManager; public static Client ignoreSSLClient() throws Exception {    SSLContext sslcontext = SSLContext.getInstance("TLS");    sslcontext.init(null, new TrustManager[]{new X509TrustManager() {        public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {}        public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {}        public X509Certificate[] getAcceptedIssuers() { return new X509Certificate[0]; }    }}, new java.security.SecureRandom());    return ClientBuilder.newBuilder()                        .sslContext(sslcontext)                        .hostnameVerifier((s1, s2) -> true)                        .build();}


I had the same problem adn did not want this to be set globally, so I used the same TrustManager and SSLContext code as above, I just changed the Client to be created with special properties

 ClientConfig config = new DefaultClientConfig(); config.getProperties().put(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES, new HTTPSProperties(     new HostnameVerifier() {         @Override         public boolean verify( String s, SSLSession sslSession ) {             // whatever your matching policy states         }     } )); Client client = Client.create(config);