问题描述
|
我的应用程序使用部署在Weblogic 10.3上的Apache的HttpClient 3.1来执行使用SSL相互身份验证的POST。我可以使用以下系统属性来配置密钥库和信任库来使其工作:-
-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定制密钥库和信任库设置(在console / config.xml中配置)。除其他外,这将提供保持密码“隐藏”并且在配置文件/控制台等中不以纯文本显示的能力。
谁能启发我?
解决方法
通过实现自定义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文档。该策略可以通过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领域名称,该名称可以从Weblogic JMX API获取,也可以简单地进行预配置。这样,就不需要实例化信任存储或重新配置Weblogic启动参数。
,您也许可以使用KeyStoreMBean通过JMX获得这些值。但是请注意,由于以下原因,这可能不是一个简单的练习:
这将需要将密钥库密码以明文形式存储在JMX客户端中(现在您将在应用程序中编写一个)。这是不安全的,并且安全审核可能会因此而失败,具体取决于审核要查找的内容。
由于JMX服务配置,可能无法在运行时访问MBean,或者必须在不同情况下以不同的方式访问MBean。假设WebLogic 11g,可以通过将JMXMBean的EditMBeanServerEnabled属性的值设置为false来使这些值变为只读。