简体   繁体   中英

Apache HttpClient Error: javax.net.ssl.SSLPeerUnverifiedException: Peer Not Authenticated

I'm trying to configure org.apache.http.client.HttpClient to work with https. This is the client configuration:

TrustManager[] trustManagers = new TrustManager[] { new DummyTrustManager() };

SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(keyManagers, trustManagers, null);

SSLSocketFactory sf = new SSLSocketFactory(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

Scheme scheme = new Scheme("https", 443, sf);
SchemeRegistry registry = new SchemeRegistry();
registry.register(scheme);

ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(registry);

DefaultHttpClient client = new DefaultHttpClient(cm, httpParameters);

This is the code of DummyTrustManager :

public static class DummyTrustManager implements X509TrustManager {

    @Override
    public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
    }

    @Override
    public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
    }

    @Override
    public X509Certificate[] getAcceptedIssuers() {
        return null;
    }
}

And when I send request, I get

 `javax.net.ssl.SSLPeerUnverifiedException: peer not authenticated`

What could be the problem?

You also need to adjust the TrustStrategy :

TrustStrategy acceptingTrustStrategy = new TrustStrategy() {
    @Override
    public boolean isTrusted(X509Certificate[] certificate, String authType) {
        return true;
    }
};
SSLSocketFactory sf = new SSLSocketFactory(acceptingTrustStrategy, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
...

see a full example at: http://www.baeldung.com/httpclient-ssl

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM