Domanda

Ecco un codice che ho copiato dal web

/**
 * A simple example that uses HttpClient to perform a GET using Basic
 * Authentication. Can be run standalone without parameters.
 *
 * You need to have JSSE on your classpath for JDK prior to 1.4
 *
 * @author Michael Becke
 */
public class BasicAuthenticationExample {

    /**
     * Constructor for BasicAuthenticatonExample.
     */
    public BasicAuthenticationExample() {
        super();
    }

    public static void main(String[] args) throws Exception {
        HttpClient client = new HttpClient();

       client.getState().setCredentials(
                new AuthScope("login.website.com", 443),
                new UsernamePasswordCredentials("login id", "password")
                );

        GetMethod get = new GetMethod(
                "https://url to the file");

        get.setDoAuthentication( true );

        try {
            // execute the GET
            int status = client.executeMethod( get );

            // print the status and response
            System.out.println(status + "\n" + 
                    get.getResponseBodyAsString());

        } finally {
            // release any connection resources used by the method
            get.releaseConnection();
        }
    }
}

ora a causa della linea

            int status = client.executeMethod( get );

ottengo il seguente errore

Exception in thread "main" 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
at com.sun.net.ssl.internal.ssl.Alerts.getSSLException(Unknown Source)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.fatal(Unknown Source)
at com.sun.net.ssl.internal.ssl.Handshaker.fatalSE(Unknown Source)
at com.sun.net.ssl.internal.ssl.Handshaker.fatalSE(Unknown Source)
at com.sun.net.ssl.internal.ssl.ClientHandshaker.serverCertificate(Unknown Source)
at com.sun.net.ssl.internal.ssl.ClientHandshaker.processMessage(Unknown Source)
at com.sun.net.ssl.internal.ssl.Handshaker.processLoop(Unknown Source)
at com.sun.net.ssl.internal.ssl.Handshaker.process_record(Unknown Source)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readRecord(Unknown Source)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.writeRecord(Unknown Source)
at com.sun.net.ssl.internal.ssl.AppOutputStream.write(Unknown Source)
at java.io.BufferedOutputStream.flushBuffer(Unknown Source)
at java.io.BufferedOutputStream.flush(Unknown Source)
at org.apache.commons.httpclient.HttpConnection.flushRequestOutputStream(HttpConnection.java:828)
at org.apache.commons.httpclient.HttpMethodBase.writeRequest(HttpMethodBase.java:2116)
at org.apache.commons.httpclient.HttpMethodBase.execute(HttpMethodBase.java:1096)
at org.apache.commons.httpclient.HttpMethodDirector.executeWithRetry(HttpMethodDirector.java:398)
at org.apache.commons.httpclient.HttpMethodDirector.executeMethod(HttpMethodDirector.java:171)
at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:397)
at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:323)
at BasicAuthenticationExample.main(BasicAuthenticationExample.java:38)
Caused by: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
at sun.security.validator.PKIXValidator.doBuild(Unknown Source)
at sun.security.validator.PKIXValidator.engineValidate(Unknown Source)
at sun.security.validator.Validator.validate(Unknown Source)
at com.sun.net.ssl.internal.ssl.X509TrustManagerImpl.validate(Unknown Source)
at com.sun.net.ssl.internal.ssl.X509TrustManagerImpl.checkServerTrusted(Unknown Source)
at com.sun.net.ssl.internal.ssl.X509TrustManagerImpl.checkServerTrusted(Unknown Source)
... 18 more
Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
at sun.security.provider.certpath.SunCertPathBuilder.engineBuild(Unknown Source)
at java.security.cert.CertPathBuilder.build(Unknown Source)
... 24 more

Ora, la ragione di questo errore è, naturalmente, che il certificato inviato dal mio server non è già nella mia lista dei certificati affidabili. La mia domanda è come ottenere il certificato nella lista di fiducia.

Grazie Pranabesh

È stato utile?

Soluzione

Questa pagina sembra rispondere alla tua domanda: Aggiunta di certificati per il tuo Java Keystore

Si fa riferimento InstallCert di Andres Sterbenz & spiegazione post sul blog .

Non si potrebbe desiderare di aggiungere in realtà il certificato nel vostro negozio di fiducia, se è solo per il test. Così si può anche disable la convalida del certificato .

Altri suggerimenti

È necessario un keystore che contiene il certificato di fiducia. È possibile utilizzare keytool comando di Java per fare quello. Ci sono alcuni modi per fare questo:

  • Aggiungi alla chiavi di default Java (.keystore nella vostra home directory o directory utente su Windows)
  • Imposta il javax.net.ssl.trustStore proprietà di sistema per la posizione di un archivio chiavi alternativo (qualcosa come java -Djavax.net.ssl.trustStore=/path/to/keystore)
  • Costruisci il tuo SSLSocketFactory implementazione in grado di caricare un archivio di chiavi qualsiasi modo si desidera (questo è sicuramente il più complicato)

Un keystore è solo un file che contiene le chiavi e / o certificati che possono essere utilizzati per varie cose, in questo caso un certificato che l'applicazione avrà bisogno di fiducia. Keytool può essere utilizzato come segue:

keytool -importcert -file /path/to/certificate/file -keystore /path/to/keystore

Per le altre opzioni della riga di comando, basta eseguire

keytool -help

È possibile utilizzare keytool import .

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top