簡體   English   中英

Java中與平台無關的信任庫路徑

[英]Platform-independent trust store path in Java

我正在嘗試用Java加載系統信任庫。 問題是我的代碼將在庫中發布,將被Android,Windows,Linux和OSX的應用程序使用,並且cacerts文件的位置在每個系統上都不同。

這是我的代碼:

        // Load the JDK's cacerts keystore file.
        String filename = System.getProperty("javax.net.ssl.trustStore");
        if (filename == null)
            filename = System.getProperty("java.home") + "/lib/security/cacerts".replace('/', File.separatorChar);
        FileInputStream is = new FileInputStream(filename);
        // Load the root certificate authorities. Despite the name, KeyStore can also hold certificates.
        KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
        // Yes this is really the password.
        String password = "changeit";
        keystore.load(is, password.toCharArray());
        // Retrieves the most-trusted CAs from keystore.
        PKIXParameters params = new PKIXParameters(keystore);

在Linux上進行測試時,此方法工作正常,但例如,我認為這不適用於Android。

是否有一種簡便的方法可以以編程方式找到系統信任庫的位置,還是我應該明確列出所有可能性並對每個信任庫路徑進行硬編碼?

致電:

KeyStore keystore = KeyStoreUtil.getCacertsKeyStore();

將返回所有System CA可信證書,這是一種獨立於平台的方式來讀取代碼文件。

備注:如果您使用空密碼,您的代碼將起作用:

String password = null;//"changeit";

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM