Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable SSL Certificate check #70

Closed
aemreunal opened this issue Feb 8, 2015 · 17 comments
Closed

Disable SSL Certificate check #70

aemreunal opened this issue Feb 8, 2015 · 17 comments

Comments

@aemreunal
Copy link

Hello,

I'm writing a server on Spring and I'm using Spring Security to limit connections to HTTPS, running the WAR on Tomcat. The server auto-generates a self-signed certificate and as I am using it in a development environment, I don't want to go through the hassle of obtaining proper certificates or something. This means that Unirest (or rather the underlying HTTP client) rejects the certificate with the following exception:

javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

It's a pretty frustrating situation to be in, as I genuinely believe many people are using Unirest with these certificates. I think I can circumvent this issue by creating a custom HttpClient object and giving it to Unirest as the default, but I'd rather not mess with Unirest, in order to avoid any future issues down the line.

I've noticed that the PHP version got this option (Kong/unirest-php#27) quite some time ago.

My questions are:

  1. Am I missing something obvious that would let me disable SSL certificate check? If so, would you kindly point me in the direction?
  2. If not, are there any plans to introduce this setting to the Java version as well?

Thanks in advance for your time.

@aemreunal
Copy link
Author

I achieved self-signed certificate trusting with the following code:

SSLContext sslcontext = SSLContexts.custom()
                                   .loadTrustMaterial(null, new TrustSelfSignedStrategy())
                                   .build();

SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext);
CloseableHttpClient httpclient = HttpClients.custom()
                                            .setSSLSocketFactory(sslsf)
                                            .build();
Unirest.setHttpClient(httpclient);

However, the requests are incredibly slow (~10 seconds with custom HttpClient and Unirest, on the localhost) compared to a regular REST client (~0.14 second with CocoaRestClient, on the localhost). You might imagine that with such a speed, Unirest becomes unusable.

@aemreunal
Copy link
Author

The issue seems to be resolved when a bunch of packages were updated. I don't know what caused the issue.

@monarchwadia
Copy link

I'm also having trouble with this issue. It would be nice to be able to ignore SSL certificate checks in Java.
@aemreunal could you please tell me what packages were updated, if you still remember? It would help :-)

@aemreunal
Copy link
Author

@monarchjhaveri I'm so sorry, I really don't remember :( Maybe it was a Java update that seemingly resolved this speed issue. Are you having that speed issue as well?

@monarchwadia
Copy link

Hi @aemreunal , thanks for replying, I managed to resolve that speed issue :-) it was a problem with my code

@kamaljeetrathi
Copy link

How to set X509Certificate in unirest request.

@ToroLiu ToroLiu mentioned this issue May 23, 2017
@boodskap
Copy link

import java.security.cert.X509Certificate;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.exceptions.UnirestException;

static {
	try {

		TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
			public java.security.cert.X509Certificate[] getAcceptedIssuers() {
				return null;
			}

			public void checkClientTrusted(X509Certificate[] certs, String authType) {
			}

			public void checkServerTrusted(X509Certificate[] certs, String authType) {
			}

		} };
		

		SSLContext sslcontext = SSLContext.getInstance("SSL");
		sslcontext.init(null, trustAllCerts, new java.security.SecureRandom());
		HttpsURLConnection.setDefaultSSLSocketFactory(sslcontext.getSocketFactory());
		SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext);
		CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
		Unirest.setHttpClient(httpclient);

	} catch (Exception e) {
		e.printStackTrace();
	}
}

@peasunseries
Copy link

peasunseries commented Jul 31, 2017

@boodskap It's worked well, thanks.

sic2 pushed a commit to sea-of-stuff/sos that referenced this issue Sep 4, 2017
…t how to test network requests with node identity verification.

This is requiring some mocking of DigitalSignature class using PowerMock
Using the right versions of some libraries, otherwise Unirest breaks

Unirest client does not seem to work properly anymore. This might be due to some updates.
These thread over here might have the answer to such problems: Kong/unirest-java#70
But it must be investigated further.

Additionally, I added a boolean variable in the settings to enable/disable node pinging
@Jofairden
Copy link

Im still getting com.mashape.unirest.http.exceptions.UnirestException: javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure with all of this.

@wanghuanjing
Copy link

@Jofairden , it may be you don't set proxy. and I found you must set the proxy through httpclient instead of unirest. which like:
CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslsf). setProxy(new HttpHost("proxy_url", proxy_port))build();
Unirest.setHttpClient(httpclient)

It works for me!

@arthur-dai-618
Copy link

@wanghuanjing Works for me too, poor chinese~

@ryber
Copy link
Collaborator

ryber commented Sep 22, 2019

This is actually built into Unirest now with Unirest.config().verifySsl(false);

@javase
Copy link

javase commented Oct 25, 2019

This is actually built into Unirest now with Unirest.config().verifySsl(false);

thank you so much , it works

@josegabrielx
Copy link

Thank you it works for me!

@yuvikrishnaios1
Copy link

Thanks it work for me also.

@atilioch
Copy link

atilioch commented Jun 4, 2021

Excelent!! Thank you very much!!

@ryber
Copy link
Collaborator

ryber commented Jun 4, 2021

Just don't do that prod please

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests