【发布时间】:2014-02-04 16:47:40
【问题描述】:
我有一个需要调用 Web 服务的要求。为了调用该 Web 服务,我需要在 Java 的 SOAP 请求中提供证书链和签名值(Web 服务中的数据类型是字符串)。
我已经使用 wsimport 从 wsdl 生成了源代码,并在我的 Java 程序中使用了它。我有一个数字签名证书文件 (certificateFile.pfx)(我可以使用密码)。
最终的 SOAP 消息示例应该是这样的
<soapenv:Envelope
xmlns:soapenv = "http://schemas.xmlsoap.org/soap/envelope/"
xmlns:v = "http://www.something.com">
<soapenv:Header/>
<soapenv:Body>
<v:Auth>
<v:userID>xxxxxxxxxx</v:userID>
<v:password>xxxxxxxxxx</v:password>
<v:certChain>xxxxxxxxxx</v:certChain>
<v:signature>xxxxxxxxxx</v:signature>
</v:Auth>
</soapenv:Body>
</soapenv:Envelope>
从 WSDL 生成 java 源代码后,对于 Auth,我设置了这样的值。 (以下 4 个字段均为字符串数据类型)
Auth authInfo = new Auth();
authInfo.setUserID(userId);
authInfo.setPassword(password);
authInfo.setCertChain("");
authInfo.setSignature("");
这里我没有为 CertChain 和 Signature 设置什么。
由于我有证书文件,所以我尝试了这个
XMLSignatureFactory fac = XMLSignatureFactory.getInstance("DOM");
// Load the KeyStore and get the signing key and certificate.
KeyStore ks = KeyStore.getInstance("PKCS12");
ks.load(new FileInputStream(new File(certificateName)),
certificatePassword.toCharArray());
KeyStore.PrivateKeyEntry keyEntry =
(KeyStore.PrivateKeyEntry) ks.getEntry(
certificateAlias,
new KeyStore.PasswordProtection(
certificatePassword.toCharArray()));
X509Certificate cert =
(X509Certificate) keyEntry.getCertificate();
使用过
authInfo.setSignature(new String(cert.getSignature()));
但没有一个有效。 cert.getSignature() 返回二进制数据而不是真正的字符串。
我没有为这两个字段设置什么。如何使用我的证书文件在 Java 中获取 certificateChain 值和签名值?不幸的是,我无法联系网络服务提供商以获取更多信息。我想如果我得到certificateChain 和字节数组中的签名,我可以做new String(byte[]) 并在SOAP 字段中使用该值。如何获取certificateChiain的byte[]值和签名?
我该如何解决这个问题?有什么建议吗?
【问题讨论】:
标签: java certificate digital-signature