在 java 中的运行时递归更改系统 属性
Recursively change system property at runtime in java
我有一个问题,正在寻找在 java 中运行时更改系统 属性 的示例。换句话说,我有一个独立的库,它将加载 System.setProperty("javax.net.ssl.trustStore", trustStorePath)
,其中 trustStorePath 的值将根据条件发生变化。如果条件发生变化,那么我需要更改 trustStorePath 的值并需要设置 System 属性。
但故事是当我第一次设置值时,即使我更改 trustStorePath 的值并再次设置系统 属性,它也会存储并使用它。变化没有反映出来。
那么,我该怎么做呢。下面是相同的示例代码片段。
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 中运行时更改系统 属性 的示例。换句话说,我有一个独立的库,它将加载 System.setProperty("javax.net.ssl.trustStore", trustStorePath)
,其中 trustStorePath 的值将根据条件发生变化。如果条件发生变化,那么我需要更改 trustStorePath 的值并需要设置 System 属性。
但故事是当我第一次设置值时,即使我更改 trustStorePath 的值并再次设置系统 属性,它也会存储并使用它。变化没有反映出来。
那么,我该怎么做呢。下面是相同的示例代码片段。
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
更改时执行此操作。