我有一个问题并正在寻找在 java 中运行时更改系统属性的示例。换句话说,我有一个独立的库,它将加载 System.setProperty("javax.net.ssl.trustStore", trustStorePath)
其中 trustStorePath 的值将根据条件而变化。如果条件发生变化,那么我需要更改 trustStorePath 的值并需要设置系统属性。
但故事是当我第一次设置该值时,即使我更改 trustStorePath 的值并再次设置系统属性,它也会存储该值并使用它。变化没有反射(reflect)出来。
那么,我怎样才能做同样的事情呢?下面是相同的示例代码片段。
if (getFile(keyStorePath).exists() && isChanged ) {
System.setProperty("javax.net.ssl.keyStore", keyStorePath);
System.setProperty("javax.net.ssl.keyStoreType", "JKS");
System.setProperty("javax.net.ssl.keyStorePassword", Pwd);
}else if (getFile(testMerchantKeyStorePath).exists() ) {
System.setProperty("javax.net.ssl.keyStore", testMerchantKeyStorePath);
System.setProperty("javax.net.ssl.keyStoreType", "JKS");
System.setProperty("javax.net.ssl.keyStorePassword",Pwd);
}
最佳答案
听起来您想使用动态信任存储。您可以在打开任何连接之前执行此操作:
KeyStore ts = KeyStore.getInstance("JKS");
ts.load(new FileInputStream(new File("Your_New_Trust_Store_Path")), "password".toCharArray());
TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
tmf.init(ts);
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, tmf.getTrustManagers(), null);
HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
// Open Connection .... etc. ....
每次 trustStorePath
更改时,您都可以执行此操作。
关于java - 在java中运行时递归地改变系统属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35125154/