为客户机配置响应签名验证方法
使用 WSSVerification 和 WSSVerifyPart API 来选择签署验证方法。请求签署验证方法包括摘要算法和传输方法。
开始之前
要完成对签名验证信息的配置以保护 SOAP 消息,必须执行下列算法任务:
- 使用 WSSVerification API 来配置规范方法和签名方法。
- 使用 WSSVerifyPart API 来配置摘要方法和变换方法。
关于此任务
下表描述了此信息的用途。这些定义中的一些基于 XML 签名规范,此规范位于以下 Web 站点 http://www.w3.org/TR/xmldsig-core。
方法名称 | 用途 |
---|---|
摘要算法 | 应用转换后(如果指定的话)应用到数据,以生成 <DigestValue> 元素。<DigestValue> 元素的签署将资源内容绑定到签署者密钥。为客户机请求发送方配置选择的算法必须与在客户机请求接收方配置中选择的算法匹配。 |
变换算法 | 应用于 <Transform> 元素。 |
签名算法 | 指定签名验证方法的统一资源标识 (URI)。 |
规范化算法 | 指定规范方法的统一资源标识 (URI)。 |
配置客户机以便对消息进行数字签署后,必须配置客户机以验证数字签名。可以使用 WSS API 或通过管理控制台配置策略集来验证数字签名以及选择验证和验证部件算法。如果使用 WSS API 来配置,请使用 WSSVerification 和 WSSVerifyPart API 来指定配置客户机进行请求签署时要验证的数字签署消息部件及要使用的算法方法。
WSSVerification 和 WSSVerifyPart API 执行以下步骤以配置签名验证和验证部件算法方法:
过程
结果
示例
以下示例提供了样本 WSS API 代码,这段代码指定了验证信息:将主体指定为要验证的部件、将 HMAC_SHA1 指定为签名方法、将 C14N 和 EXC_C14N 指定为规范方法的候选值、将 TRANSFORM_STRT10 指定为变换方法以及将 SHA256 指定为摘要方法。
// Get the message context
Object msgcontext = getMessageContext();
// Generate the WSSFactory instance
WSSFactory factory = WSSFactory.getInstance();
// Generate the WSSConsumingContext instance
WSSConsumingContext concont = factory.newWSSConsumingContext();
// Generate the certificate list
String certpath = "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 the 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 the callback handler
X509ConsumeCallbackHandler callbackHandler = new
X509ConsumeCallbackHandler(
"dsig-receiver.ks",
"jks",
"server".toCharArray(),
certList,
java.security.Security.getProvider(
"IBMCertPath")
);
// Generate the WSSVerification instance
WSSVerification ver = factory.newWSSVerification(X509Token.class,
callbackHandler);
// Set one or more candidates of the signature method used for
// verification (step. 1)
// DEFAULT : WSSVerification.RSA_SHA1
ver.addAllowedSignatureMethod(WSSVerification.HMAC_SHA1);
// Set one or more candidates of the canonicalization method used for
// verification (step. 2)
// DEFAULT : WSSVerification.EXC_C14N
ver.addAllowedCanonicalizationMethod(WSSVerification.C14N);
ver.addAllowedCanonicalizationMethod(WSSVerification.EXC_C14N);
// Set the part to be specified by WSSVerifyPart
WSSVerifyPart verPart = factory.newWSSVerifyPart();
// Set the part to be specified by the keyword
verPart.setRequiredVerifyPart(WSSVerification.BODY);
// Set the candidates of digest methods to use for verification (step. 3)
// DEFAULT : WSSVerifypart.TRANSFORM_EXC_C14N : String
verPart.addAllowedTransform(WSSVerifyPart.TRANSFORM_STRT10);
// Set the candidates of digest methods to use for verification (step. 4)
// DEFAULT : WSSVerifyPart.SHA1
verPart.addAllowedDigestMethod(WSSVerifyPart.SHA256);
// Set WSSVerifyPart to WSSVerification
ver.addRequiredVerifyPart(verPart);
// Add the WSSVerification to the WSSConsumingContext
concont.add(ver);
// Validate the WS-Security header
concont.process(msgcontext);
下一步做什么
您完成了配置签名验证算法。接下来,配置加密或解密算法(如果尚未配置)。或者,根据需要配置安全性令牌信息。