使用 WSSVerifyPart API 验证签署部件
要在使用者端保护 SOAP 消息,请使用 Web Service 安全性 API (WSS API) 来配置响应使用者(客户端)的使用者绑定的验证部件信息。可以指定要进行验证的算法方法和要进行验证的 SOAP 消息的部件。使用 WSSVerifyPart API 来更改摘要方法或变换方法。WSSVerifyPart API 是 com.ibm.websphere.wssecurity.wssapi.verification 包的一部分。
开始之前
要使用签名验证信息保护 SOAP 消息,必须完成下列其中一个任务:
- 使用 WSSVerification API 配置签名验证信息。
- 根据需要使用 WSSVerifyPart API 配置验证部件。
关于此任务
WebSphere® Application Server 使用签名信息,供缺省使用者用于对消息的签署部件进行验证。仅响应使用者(请求者)支持 WSSVerifyPart API。
下表显示了定义数字签名安全性约束(完整性)时的必需验证部件:
验证部件信息 | 描述 |
---|---|
keyword | 使用下列关键字设置验证部件:
WS-Addressing 头未进行解密,但可以签署和验证。 |
xpath | 使用 XPath 表达式设置验证部件。 |
header | 将由 QName 指定的头设置为必需验证部件。 |
为了进行签名验证,将执行特定的缺省行为。使用 WSSVerification API 的最简单方法是使用缺省行为(请参阅示例代码)。WSS API 为签名算法、规范算法和验证部件定义了缺省值。
验证部件决定 | 缺省行为 |
---|---|
要指定的关键字 | 要进行签署及用于消息保护的不同 SOAP 消息部件。WebSphere Application Server 支持以下关键字:
|
要使用的变换方法(算法) | 添加变换方法。变换算法是在 <Transform> 元素中指定的算法,并指定签名的变换算法。缺省变换方法是 TRANSFORM_EXC_C14N。 WebSphere Application Server 支持以下预先配置的变换算法:
|
要使用的摘要方法(算法) | 设置摘要算法方法。在 <SigningInfo> 元素中使用在 <DigestMethod> 元素中指定的摘要方法算法。缺省摘要方法是 SHA1。 WebSphere Application
Server 支持以下摘要方法算法:
|
过程
结果
示例
以下示例提供了 WSSVerification API 过程的样本代码,用于验证 SOAP 消息中的签名信息:
// Get the message context
Object msgcontext = getMessageContext();
// Generate the WSSFactory instance (step: a)
WSSFactory factory = WSSFactory.getInstance();
// Generate the WSSConsumingContext instance (step: b)
WSSConsumingContext concont = factory.newWSSConsumingContext();
// Generate the certificate list
String certpath =
"c:/WebSphere/AppServer/etc/ws-security/samples/intca2.cer";
// The location of the X509 certificate file
X509Certificate x509cert = null;
try {
InputStream is = new FileInputStream(certpath);
CertificateFactory cf = CertificateFactory.getInstance("X.509");
x509cert = (X509Certificate)cf.generateCertificate(is);
} catch(FileNotFoundException e1){
throw new WSSException(e1);
} catch (CertificateException e2) {
throw new WSSException(e2);
}
Set<Object> eeCerts = new HashSet<Object>();
eeCerts.add(x509cert);
// create certStore
java.util.List<CertStore> certList = new
java.util.ArrayList<CertStore>();
CollectionCertStoreParameters certparam = new
CollectionCertStoreParameters(eeCerts);
CertStore cert = null;
try {
cert = CertStore.getInstance("Collection",
certparam, "IBMCertPath");
} catch (NoSuchProviderException e1) {
throw new WSSException(e1);
} catch (InvalidAlgorithmParameterException e2) {
throw new WSSException(e2);
} catch (NoSuchAlgorithmException e3) {
throw new WSSException (e3);
}
if(certList != null ){
certList.add(cert);
}
// generate callback handler (step: c)
X509ConsumeCallbackHandler callbackHandler = new
X509ConsumeCallbackHandler(
"dsig-receiver.ks",
"jks",
"server".toCharArray(),
certList,
java.security.Security.getProvider("IBMCertPath")
);
// Generate the WSSVerification instance (step: d)
WSSVerification ver = factory.newWSSVerification(X509Token.class,
callbackHandler);
// Set the part to be specified by WSSVerifyPart (step: e)
WSSVerifyPart verPart = factory.newWSSVerifyPart();
// Set the part to be specified by the keyword (step: f)
verPart.setRequiredVerifyPart(WSSVerification.BODY);
// Set the candidates for the digest method for verification (step: g)
// DEFAULT : WSSVerifyPart.SHA1
verPart.addAllowedDigestMethod(WSSVerifyPart.SHA256);
// Set the candidates for the transform method for verification (step: h)
// DEFAULT : WSSVerifypart.TRANSFORM_EXC_C14N : String
verPart.addAllowedTransform(WSSVerifyPart.TRANSFORM_STRT10);
// Set WSSVerifyPart to WSSVerification (step: i)
ver.addRequiredVerifyPart(verPart);
// Add WSSVerification to WSSConsumingContext (step: j)
concont.add(ver);
//Validate the WS-Security header (step: k)
concont.process(msgcontext);