They will require separate solutions. For the self-signed certificate, we can use this code to tell Java to trust all certificates.
TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager(){
public X509Certificate[] getAcceptedIssuers(){return null;}
public void checkClientTrusted(X509Certificate[] certs, String authType){}
public void checkServerTrusted(X509Certificate[] certs, String authType){}
}};
try {
SSLContext sc = SSLContext.getInstance("TLS");
sc.init(null, trustAllCerts, new SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
} catch (Exception e) {}
|
For the IP address issue, which will throw an error (CertificateException: No subject alternative names present), we can use the following code:
HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier(){
public boolean verify(String hostname, SSLSession session){
// you may choose to return true all the time here or return true for certain IPs
}
});
|
Note: this solution has been tested to work with the Jersey REST client.
References:
No comments:
Post a Comment