/ / SSLPeerUnverifiedException (Unirest) - java, https, ssl-certificate, unirest

SSLPeerUnverifiedException (Unirest) - java, https, ssl-certificate, unirest

Sono nuovo a fare scherzi con le API (sia ufficiali che non ufficiali) e ne sto usando una chiamata JavaSnap. Ho lavorato con un'implementazione molto elementare del codice di esempio, ma ho riscontrato errori. Ecco il codice di base:

Snapchat snapchat = Snapchat.login("xxxx", "xxxxx");

Innanzitutto ho riscontrato un sacco di errori ClassNotFounde dovevo continuare a scaricare i moduli apache (comuni, httpcomponents ecc.) per permettere al programma di progredire, ma essendo file di classe questo significava che non potevo vedere tutti i moduli che avevo bisogno di scaricare. Quindi se qualcuno vuole dirmelo che male ho fatto qualcosa senza sentirmi libero

Ad ogni modo, ora avendo chiarito tutte le eccezioni ClassNotFound (spero) ottengo la seguente eccezione:

com.mashape.unirest.http.exceptions.UnirestException: javax.net.ssl.SSLPeerUnverifiedException: Host name "feelinsonice-hrd.appspot.com" does not match the certificate subject provided by the peer (CN=*.appspot.com, O=Google Inc, L=Mountain View, ST=California, C=US)
at com.mashape.unirest.http.HttpClientHelper.request(HttpClientHelper.java:146)
at com.mashape.unirest.request.BaseRequest.asJson(BaseRequest.java:68)
at com.habosa.javasnap.Snapchat.requestJson(Snapchat.java:953)
at com.habosa.javasnap.Snapchat.login(Snapchat.java:160)
at Tester.go(Tester.java:21)

A quanto ho capito, è perché ne ho bisognoabilitare fidandosi di tutti i certificati, tuttavia per farlo credo di dover usare HostNameVerifiers con SSLSocketFactorys, ma non posso davvero cominciare a scherzare con questo dato che ho solo il sorgente per l'API JavaSnap e tracciando l'errore sul impilare il metodo più recente disponibile per me da modificare è questo:

private static HttpResponse<JsonNode> requestJson(String path, Map<String, Object> params, File file) throws UnirestException {
MultipartBody req = prepareRequest(path, params, file);

// Execute and return response as JSON
HttpResponse<JsonNode> resp = req.asJson();

// Record
lastRequestPath = path;
lastResponse = resp;
lastResponseBodyClass = JsonNode.class;

return resp;

La mia domanda è, sono davvero sulla buona linea con il mio pensiero?
Se io sono come posso raggiungere il mio obiettivo di eliminare questo errore / certificati attendibili? Se non sono allora che cosa è in realtà il problema?

Grazie mille

risposte:

1 per risposta № 1

rispondo a questa vecchia domanda per ricordare la mia ricerca la soluzione di errore del certificato è una combinazione di pochi posti


import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;

import javax.net.ssl.SSLContext;
import javax.security.cert.CertificateException;
import javax.security.cert.X509Certificate;

import org.apache.http.client.HttpClient;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContextBuilder;

import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.JsonNode;
import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.exceptions.UnirestException;

public class XXX {

private static HttpClient unsafeHttpClient;

static {
try {
SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustSelfSignedStrategy() {
public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
return true;
}
}).build();

unsafeHttpClient = HttpClients.custom().setSSLContext(sslContext)
.setSSLHostnameVerifier(new NoopHostnameVerifier()).build();

} catch (KeyManagementException | NoSuchAlgorithmException | KeyStoreException e) {
e.printStackTrace();
}
}

public static HttpClient getClient() {
return unsafeHttpClient;
}

public static void main(String[] args) {

try {
HttpClient creepyClient = RestUnirestClient.getClient();
Unirest.setHttpClient(creepyClient);

HttpResponse<JsonNode> response = Unirest.get("https://httpbin.org/get?show_env=1").asJson();
System.out.println(response.getBody().toString());

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