如何以编程方式设置 Axis 客户端的 SSL 上下文?

How to programmatically set the SSL context of a Axis client?

在一个非常古老的项目中,我们使用使用 Axis 1.4 开发的客户端来调用 SOAP Web 服务。此 Web 服务使用相互身份验证机制,因此我们在密钥库中安装了一个私有证书,在信任库中安装了一个 public 密钥。

SOAP 客户端在 BPM 流程的任务中使用。我们不能也不想使用 JVM 全局信任库和密钥库。然后我们不能以编程方式配置 JVM global trustore 和 keystore:

// Keystore
System.setProperty("javax.net.ssl.keyStore", fileKeystore);
System.setProperty("javax.net.ssl.keyStorePassword", pwdKeystore);
System.setProperty("javax.net.ssl.keyStoreType", "PKCS12");
// Truststore
System.setProperty("javax.net.ssl.trustStore", fileTruststore);
System.setProperty("javax.net.ssl.trustStorePassword", pwdTruststore);
System.setProperty("javax.net.ssl.trustStoreType", "JKS");

类似的方法将迫使我们在 JVM 属性上同步进程,我们不想这样做。而且,机器上还有其他java个进程运行。

我的问题是:Axis 1.4 是否提供一些 API 来指定用于特定 Web 服务调用的密钥库和信任库?

好的,谷歌搜索一下我找到了问题的答案。答案是,仅使用 Axis 1.4 不可能为每个服务调用指定不同的 keystore/truststore。我们需要一个名为 axistools.

的外部库

该库实现了一种特殊的 EngineConfiguration,允许您为每个服务调用指定一个密钥库 and/or 一个信任库。

下面的示例将进行说明:

// Setting up the configuration
SSLClientAxisEngineConfig config = new SSLClientAxisEngineConfig();
config.setKeystore("path/to/your/keystore");
config.setKeystoreType("JKS");
config.setKeystorePassword("password");
config.setTruststore("path/to/your/truststore");
config.setTruststoreType("JKS");
config.setTruststorePassword("password");
// Very important: without this method invocation 
// the client won't work at all
config.initialize();

// Calling the web service
URL url = new URL("https://localhost:8443/someService");
WebServiceLocator locator = new WebServiceLocator (config);
WebServiceSoap port = locator.getWebServiceSoap(url);
WebServiceSoapStub stub = (WebServiceSoapStub) port;
stub.serviceMethod();

就这些了,伙计们!!!