0

I am getting javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure when running my test case for Restful web service using Jersey. Somebody suggested trusting all certificate as a solution. The solution I have tried below is as a result of reading through similar StackOverflow questions but none apply to my case.

@Test
public void shouldCheckURI() throws IOException {
try{
 // Create a trust manager that does not validate certificate chains
TrustManager[] trustAllCerts = new TrustManager[] {new X509TrustManager() {
    public java.security.cert.X509Certificate[] getAcceptedIssuers() { 
return null; }
    public void checkClientTrusted(java.security.cert.X509Certificate[] 
certs, String authType) { }
    @Override
    public void checkServerTrusted(java.security.cert.X509Certificate[] 
certs, String authType)throws CertificateException  { }
}};

// Install the all-trusting trust manager
SSLContext sc;
try {
    sc = SSLContext.getInstance("SSL");
    sc.init(null, trustAllCerts, new java.security.SecureRandom());
    HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
} catch (Exception e) {
    e.printStackTrace();
}

// Create all-trusting host name verifier
HostnameVerifier allHostsValid = new HostnameVerifier() {
    public boolean verify(String hostname, SSLSession session) {
        return true;
    }
};
// Install the all-trusting host verifier
HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
Client client = Client.create();

WebResource webResource = client.resource("https://SSLResfulendpoint");

ClientResponse response = 
webResource.type("application/json").get(ClientResponse.class);

assertEquals(200, response.getStatus());
} catch (Exception e) {
System.out.println(e.getMessage());  
e.printStackTrace();
}

}
OAM
  • 105
  • 8
  • There are [many other questions](https://stackoverflow.com/search?tab=votes&q=javax.net.ssl.SSLHandshakeException%3a%20Received%20fatal%20alert%3a%20handshake_failure) about this kind of problem. Please study these first to see if they address your problem. If not please make clear how your question differs from the other questions and add enough information (as seen on the other questions) so that one is able to analyze your specific problem. – Steffen Ullrich May 19 '17 at 07:39
  • Agree with @SteffenUllrich, update your post to explain how your question is different and we'll vote to reopen. – Kelly S. French May 19 '17 at 15:27

0 Answers0