2010-01-26 12 views
31

Ho installato un certificato autofirmato per testare una connessione js ssl - tuttavia, si rifiuta di localizzare il java trustStore. Ne ho salvate copie in/Java/jre6/lib/security oltre alla cartella in cui sono compilate le classi (im usando netbeans) e anche a/java/jre6/bin nessuna di queste sembra funzionare, perché quando eseguo il seguente - trustStore = null.java - path to trustStore - set property non funziona?

public class ShowTrustStore { 

    public static void main(String[] args) { 

     System.setProperty("javax.net.ssl.keyStore", "keystore.jks"); 
     System.setProperty("javax.net.ssl.trustStrore", "cacerts.jks"); 
     System.setProperty("javax.net.ssl.keyStorePassword", "changeit"); 



     String trustStore = System.getProperty("javax.net.ssl.trustStore"); 
     if (trustStore == null) { 
      System.out.println("javax.net.ssl.trustStore is not defined"); 
     } else { 
      System.out.println("javax.net.ssl.trustStore = " + trustStore); 
     } 
    } 
} 

come impostare il percorso correttamente?

********** UPDATE ************ Utilizzando il metodo getFile() e ancora un po 'di dati di debug:

package ssltest; 

public class Main { 

    public static void main(String[] args) { 

//  System.setProperty("javax.net.ssl.keyStore", "/keystore.jks"); 
//  System.setProperty("javax.net.ssl.trustStrore", "/java.home/cacerts.jks"); 
//  System.setProperty("javax.net.ssl.keyStorePassword", "changeit"); 
//  System.setProperty("javax.net.ssl.trustStorePassword", "changeit"); 

     try { 
      Main.class.getResource("trustStore.jks").getFile(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

     String trustStore = System.getProperty("javax.net.ssl.trustStore"); 

     if (trustStore == null) { 
      String storeLoc; 
      storeLoc = System.getProperty("java.class.path"); 
      System.out.println("classpath: " + storeLoc); 
     } 

     trustStore = System.getProperty("javax.net.ssl.trustStore"); 
     if (trustStore == null) { 
      System.out.println("javax.net.ssl.trustStore is not defined"); 
     } else { 
      System.out.println("javax.net.ssl.trustStore = " + trustStore); 
     } 
    } 
} 

run: java.lang.NullPointerException classpath: C: \ Users \ Main \ Documents \ NetBeansProjects \ sslTest \ build \ classes; C: \ Users \ Main \ Documents \ NetBeansProjects \ sslTest \ src su ssltest.Main.main (Main.java : 15) javax.net.ssl.trustStore non è definito BUILD SUCCESSFUL (tempo totale: 0 secondi)

+0

vedi la mia risposta aggiornata – Bozho

+0

funziona! grazie molto! – oneAday

+0

Buono a sapersi che non sono l'unico a cercare un'ora di errore per ora ... a causa di un errore di battitura ... ;-) – Steffen

risposta

47

Hai un errore di battitura - è trustStore.

A parte l'impostazione delle variabili con System.setProperty(..), è anche possibile utilizzare

-Djavax.net.ssl.keyStore=path/to/keystore.jks 
+0

non sono sicuro di cos'altro posso provare - ho avuto il valore java.home come C: \ Programmi (x86) \ Java \ jdk1.6.0_17 \ jre ora anche se memorizzo i due file qui ed eseguo questo: System.setProperty ("javax.net.ssl.keyStore", "/ java .home/keystore.jks "); System.setProperty ("javax.net.ssl.trustStrore", "/java.home/cacerts.jks"); ancora non li trova ... – oneAday

+0

java.home non è stato risolto come percorso. è possibile scaricarlo tramite System.getProperty ("java.home"), ma non è consigliabile inserire il keystore in tale posizione. Inoltre, ti ho dato tre opzioni: provale. – Bozho

+0

ho provato ad usare entrambi - il primo non funziona e il secondo metodo lancia un'eccezione del puntatore nullo - ho aggiunto il codice e le informazioni di debug sopra. ive ha memorizzato i file in entrambe queste posizioni (che vengono visualizzate come classpath): C: \ Users \ Main \ Documents \ NetBeansProjects \ sslTest \ build \ classes; C: \ Users \ Main \ Documents \ NetBeansProjects \ sslTest \ src qualsiasi idea che cosa sta andando storto? grazie !! – oneAday

37

Sembra che si dispone di un errore di battitura - "trustStrore" dovrebbe essere "trustStore", vale a dire

System.setProperty("javax.net.ssl.trustStrore", "cacerts.jks"); 

dovrebbe essere :

System.setProperty("javax.net.ssl.trustStore", "cacerts.jks"); 
+0

Ha funzionato? Mi chiedo se ho bisogno di usare il -Djavax.net.ssl ​​.... roba – simgineer

+3

Ha funzionato per me usando '-Djavax.net.ssl.trustStore =/home/percorso/to/truststore' con Tomcat8 – Goot

5

Entrambi

-Djavax.net.ssl.trustStore=path/to/trustStore.jks 

e

System.setProperty("javax.net.ssl.trustStore", "cacerts.jks"); 

fare la stessa cosa e non hanno alcuna differenza di lavoro saggio. Nel tuo caso hai solo un refuso. Hai sbagliato l'ortografia trustStore in javax.net.ssl.trustStore.

+3

Non è vero, fuorviante. Un trustStore è un contenitore client per le chiavi pubbliche dei server attendibili, mentre keyStore è un contenitore delle chiavi private. – peterh

+1

@peterh Non vero, fuorviante. Un truststore è un contenitore laterale client- * o server- * per * certificati * di firmatari attendibili. – EJP

Problemi correlati