【问题标题】:Can I get HttpClient to use Weblogic's custom keystore / truststore settings?我可以让 HttpClient 使用 Weblogic 的自定义密钥库/信任库设置吗?
【发布时间】:2011-05-25 16:29:30
【问题描述】:

我的应用程序使用 Apache 的 HttpClient 3.1 部署在 Weblogic 10.3 上,使用 SSL 相互身份验证执行 POST .我可以使用以下系统属性来配置 keystoretruststore:-

-Djavax.net.ssl.keyStore=C:\Keystore\KEYSTORE.jks
-Djavax.net.ssl.keyStorePassword=changeit
-Djavax.net.ssl.trustStore=C:\Truststore\TRUSTSTORE.jks
-Djavax.net.ssl.trustStorePassword=changeit

有什么方法可以让 HttpClient 识别和使用 Weblogic 自定义 keystoretruststore 设置(在控制台/config.xml 中配置)。除此之外,这将提供使密码“隐藏”并且在配置文件/控制台等中以纯文本形式不可见的能力。

谁能赐教?

【问题讨论】:

    标签: weblogic httpclient keystore truststore


    【解决方案1】:

    通过实现自定义TrustStrategy,我已经能够让 HttpClient 使用自定义 weblogic 信任存储证书进行 SSL 连接:

    import sun.security.provider.certpath.X509CertPath;
    import weblogic.security.pk.CertPathValidatorParameters;
    import java.security.InvalidAlgorithmParameterException;
    import java.security.NoSuchAlgorithmException;
    import java.security.cert.CertPath;
    import java.security.cert.CertPathParameters;
    import java.security.cert.CertPathValidator;
    import java.security.cert.CertPathValidatorException;
    import java.security.cert.CertificateException;
    import java.security.cert.X509Certificate;
    import java.util.Arrays;
    
    public class WeblogicSSLTrustStrategy implements TrustStrategy {
    
      @Override
      public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
        validator = CertPathValidator.getInstance("WLSCertPathValidator");
        CertPath certPath = new X509CertPath(Arrays.asList(chain));
    
        // supply here the weblogic realm name, configured in weblogic console
        // "myrealm" is the default one
        CertPathParameters params = new CertPathValidatorParameters("myrealm", null, null);
        try {
          validator.validate(certPath, params);
        } catch (CertPathValidatorException e) {
          throw new CertificateException(e);
        } catch (InvalidAlgorithmParameterException e) {
          throw new CertificateException(e);
        }
    
        return true;
      }
    } 
    

    此代码基于Weblogic documentation。策略可以通过 SSLSocketFactory 传递给 HttpClient:

    SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory()));
    
    SSLSocketFactory sslSocketFactory = new SSLSocketFactory(new WeblogicSSLTrustStrategy());
    schemeRegistry.register(new Scheme("https", 443, sslSocketFactory));
    
    PoolingClientConnectionManager connectionManager = new PoolingClientConnectionManager(schemeRegistry);
    
    DefaultHttpClient httpClient = new DefaultHttpClient(connectionManager);
    

    唯一未知的参数是 Weblogic Realm 名称,它可以从 Weblogic JMX API 中获取,也可以简单地预先配置。这样就不需要实例化信任存储或重新配置 Weblogic 启动参数。

    【讨论】:

      【解决方案2】:

      您可以使用KeyStoreMBean 通过JMX 获取这些值。不过请注意,由于以下原因,这可能不是一项简单的练习:

      • 这需要在您的 JMX 客户端中以明文形式存储密钥库密码(现在您将在应用程序中编写一个)。这是不安全的,安全审计可能会因此失败,具体取决于审计要查找的内容。
      • 由于 JMX 服务配置,MBean 在运行时可能无法访问,或者在不同的场景中必须以不同的方式访问。假设 WebLogic 11g,这些值可能由setting the value of the EditMBeanServerEnabled attribute of the JMXMBean to false 设置为只读。

      【讨论】:

        猜你喜欢
        • 2010-09-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-10-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-08-24
        相关资源
        最近更新 更多