Last active
April 24, 2019 15:16
-
-
Save JAlexoid/b15dba31e5919586ae51 to your computer and use it in GitHub Desktop.
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
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
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);