Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ignoring SSL certificate in Apache HttpClient 4.3

How to ignore SSL certificate (trust all) for Apache HttpClient 4.3?

All the answers that I have found on SO treat previous versions, and the API changed.

Related:

  • How to ignore SSL certificate errors in Apache HttpClient 4.0
  • How to handle invalid SSL certificates with Apache HttpClient?
  • Need to trust all the certificates during the development using Spring
  • Ignore SSL Certificate Errors with Java

Edit:

  • It is only for test purposes. Kids, don't try it at home (or in production)
like image 415
Jakub M. Avatar asked Oct 22 '13 12:10

Jakub M.


People also ask

How do I skip https?

To bypass SSL certificate validation for local and test servers, you can pass the -k or --insecure option to the Curl command. This option explicitly tells Curl to perform "insecure" SSL connections and file transfers. Curl will ignore any security warnings about an invalid SSL certificate and accept it as valid.

How do you skip the SSL handshake in RestTemplate?

To skip or avoid the SSL check, we need to modify the default RestTemplate available with the normal Spring package. In this configuration class, we basically declare a new Bean that creates a HTTPClient with the certificate check as disabled.

What is TrustSelfSignedStrategy?

Class TrustSelfSignedStrategy. java.lang.Object org.apache.http.conn.ssl.TrustSelfSignedStrategy All Implemented Interfaces: org.apache.http.ssl.TrustStrategy public class TrustSelfSignedStrategy extends Object implements TrustStrategy. A trust strategy that accepts self-signed certificates as trusted.


1 Answers

The code below works for trusting self-signed certificates. You have to use the TrustSelfSignedStrategy when creating your client:

SSLContextBuilder builder = new SSLContextBuilder(); builder.loadTrustMaterial(null, new TrustSelfSignedStrategy()); SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(         builder.build()); CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(         sslsf).build();  HttpGet httpGet = new HttpGet("https://some-server"); CloseableHttpResponse response = httpclient.execute(httpGet); try {     System.out.println(response.getStatusLine());     HttpEntity entity = response.getEntity();     EntityUtils.consume(entity); } finally {     response.close(); } 

I did not include the SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER on purpose: The point was to allow testing with self signed certificates so you don't have to acquire a proper certificate from a certification authority. You can easily create a self-signed certificate with the correct host name, so do that instead of adding the SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER flag.

like image 159
mavroprovato Avatar answered Sep 23 '22 16:09

mavroprovato