一尘不染

java-trustStore的路径-set属性不起作用?

java

我已经设置了一个自签名证书来测试ssl java连接-但是,它拒绝查找java
trustStore。除了将类编译到的文件夹(使用netbeans)(以及使用netbeans)之外,我还将其副本保存在/ Java / jre6 /
lib / security中,以及上述都不起作用,因为当我运行以下命令时-trustStore = null。

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);
        }
    }
}

如何正确设置路径?

* UPDATE ***使用getFile()方法和更多调试数据:

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);
        }
    }
}

运行:java.lang.NullPointerException类路径:C:\ Users \ Main \ Documents \
NetBeansProjects \ sslTest \ build \ classes; C:\ Users \ Main \ Documents \
NetBeansProjects \ sslTest \ src at ssltest.Main.main(Main.java
:15)未定义javax.net.ssl.trustStore建立成功(总时间:0秒)


阅读 486

收藏
2020-09-08

共1个答案

一尘不染

您有错字-是trustStore

除了使用设置变量外System.setProperty(..),您还可以使用

-Djavax.net.ssl.keyStore=path/to/keystore.jks
2020-09-08