java - 如何制作具有动态 SSL 身份验证的 Soap 客户端?

标签 java ssl-certificate soap-client pfx

我只有这段代码,用于使用 PFX 文件调用 Web 服务

System.setProperty("javax.net.ssl.keyStoreType", "pkcs12");
System.setProperty("javax.net.ssl.keyStore", pathPFX);
System.setProperty("javax.net.ssl.keyStorePassword", pass);
System.setProperty("sun.security.ssl.allowUnsafeRenegotiation", "true");
QName serviceName = new QName("www.example.org", "example");

//QName for Port As defined in wsdl.
QName portName = new QName("www.example.org", "exampleSOAP");

// Create a dynamic Service instance
Service service = Service.create(serviceName);

// Add a port to the Service
service.addPort(portName, SOAPBinding.SOAP11HTTP_BINDING, endpointAddress);

//Create a dispatch instance
Dispatch<SOAPMessage> dispatch = service.createDispatch(portName, SOAPMessage.class, Service.Mode.MESSAGE); 

MessageFactory factory = MessageFactory.newInstance();

SOAPMessage request = factory.createMessage();

// Object for message parts
SOAPPart sp = request.getSOAPPart();
StreamSource prepMsg = new StreamSource(new FileInputStream(pathXML));
sp.setContent(prepMsg);

// Save message
request.saveChanges();

SOAPMessage reply = null;
boolean success = true;
String response = "";
try {
    //Invoke Endpoint Operation and read response
    reply = dispatch.invoke(request);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    reply.writeTo(out);
    creaXMLResponse(out.toString());
} 
catch (WebServiceException wse)
{
  wse.printStackTrace();
    success = false;
}

它工作得很好,但现在我必须为不同的客户端使用不同的 PFX 文件,它们将在同一个 tomcat 上运行。

使用此代码将使我重新启动 tomcat 以使用不同的 PFX,因此我需要创建一种动态方式来选择 pfx 作为 keystore ,但不使用 JKS 文件(客户端数量会增加,我可以'不必每次都创建一个新的 JKS)??

注意:我一直在阅读有关使用 Keystore 和 SSLContext 的内容,但我不知道如何将它们链接到我的实际代码

提前致谢

最佳答案

顾名思义, keystore 可以存储多个 key ,而不仅仅是一个。因此常见的方法是将与所有服务器握手所需的所有 key 存储在同一个 keystore 中。

查看keytool手册页,有很多这方面的教程

关于java - 如何制作具有动态 SSL 身份验证的 Soap 客户端?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19865114/

相关文章:

java - 在 Spring Rest 模板中设置超时

java - 如何根据用户提供的字段创建字段?

Java 无法在 MacOS X Leopard 上运行

macos - 在 Keychain Access : which private key is used? 中生成证书签名请求

asp.net - Visual Studio 中的 FTPS 发布失败,结果为 "Secure connection was closed by the remote connection end."

android - 如何通过android中的ksoap SoapObject检索来自webservice的复杂类型对象

java - NoSuchMethodError : com. google.common.collect.ImmutableList.toImmutableList()

asp.net - 带外部静态内容服务器的 SSL

python - 获取 zeep.exceptions.ValidationError : Missing element for method that worked with suds

java - JAVA 中的 SOAP 读取器和编写器?