【问题标题】:Recursively change system property at runtime in javajava 在运行时递归更改系统属性
【发布时间】:2016-02-01 07:36:11
【问题描述】:

我有一个问题,正在寻找在 java 运行时更改系统属性的示例。换句话说,我有一个独立的库,它将加载System.setProperty("javax.net.ssl.trustStore", trustStorePath),其中 trustStorePath 的值将根据条件而变化。如果条件发生变化,那么我需要更改 trustStorePath 的值并需要设置系统属性。

但故事是当我第一次设置该值时,它会存储该值并使用它,即使我更改了 trustStorePath 的值并再次设置系统属性。变化没有反映出来。

那么,我该怎么做。下面是相同的示例代码 sn-p。

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

    }

【问题讨论】:

标签: java runtime keystore truststore system-properties


【解决方案1】:

听起来您想使用动态信任库。您可以在打开任何连接之前执行此操作:

    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 更改时执行此操作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-12-02
    • 1970-01-01
    • 1970-01-01
    • 2017-05-06
    • 2014-07-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多