1
我試圖在Java中加載系統信任存儲。問題在於,我的代碼將在Android庫,Windows,Linux和OSX應用程序中使用,並且每個系統上cacerts文件的位置都不相同。Java中與平臺無關的信任存儲路徑
這裏是我的代碼:
// 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上工作,例如。
有沒有一種簡單的方法來編程地查找系統信任存儲的位置,還是我譴責明確列舉每種可能性並對每個信任存儲路徑進行硬編碼?