使用 WSSSignPart API 添加签署部件
可通过使用 Web Service 安全性 API (WSS API) 保护 SOAP 消息,而不必对配置使用策略集。要配置对请求生成者(客户端)绑定进行签署的部件,请使用 WSSSignPart API 来保护消息的完整性及配置摘要和变换算法方法。WSSSignPart API 是 com.ibm.websphere.wssecurity.wssapi.signature 包的一部分。
开始之前
可以使用 WSS API,或者通过管理控制台配置策略集来配置签名信息。要使用签名信息保护 SOAP 消息,必须完成下列其中一个任务:
- 配置签名信息。
- 根据需要配置签署部件。
关于此任务
WebSphere® Application Server 使用缺省生成器的签名信息来签署消息的部件,并将 XML 数字签名与现有的摘要和变换算法(例如,SHA1 或 TRANSFORM_EXC_C14N)一起使用。
签名信息指定适用于已生成消息的完整性约束。签署部件用于保护消息的完整性。可以指定要添加的签署部件以保护消息完整性。
下表显示了定义数字签名安全性约束(完整性)时的必需签署部件:
签署部件 | 描述 |
---|---|
keyword | 使用关键字添加签署部件。WebSphere Application
Server 支持对签署部件使用以下关键字:
|
xpath | 通过使用 XPath 表达式添加必需签署部件。 |
header | 将 QName 指定的头添加为签署部件。 |
timestamp | 将 WSSTimestamp 对象添加为签署对象。如果指定,那么时间戳记信息将指定生成消息及消息到期的时间。 |
可以在生成器端上请求的消息保护中指定不同的消息部件。WSSSignPart 允许添加变换算法、设置摘要方法、将对象设置为目标、指定元素以及指定诸如 SOAP 主体、WS-Addressing 头和时间戳记信息之类的签署部件。
为了获取签名信息,将执行特定的缺省行为。使用 WSSSignPart API 的最简单方法是使用缺省行为(请参阅示例代码)。缺省签署部件行为包括:
签名决定 | 缺省行为 |
---|---|
要签署的 SOAP 消息部件 | WebSphere Application Server 支持对以下 SOAP 消息部件进行签署并将它们用于消息保护:
|
要使用的摘要方法 | 设置摘要算法方法。在 <SigningInfo> 元素中使用在 <DigestMethod> 元素中指定的摘要方法算法。 WebSphere Application Server 支持以下预配置的摘要方法:
|
要使用的变换算法 | 添加变换方法。变换算法是在 <Transform> 元素中指定的算法,并指定签名的变换算法。 WebSphere Application Server 支持以下预先配置的变换算法:
|
过程
结果
示例
以下示例提供了样本代码,这段代码使用了在 WSSSignPart API 中定义的所有方法:
// Get the message context
Object msgcontext = getMessageContext();
// Generate the WSSFactory instance (step: a)
WSSFactory factory = WSSFactory.getInstance();
// Generate WSSGenerationContext instance (step: b)
WSSGenerationContext gencont = factory.newWSSGenerationContext();
// Generate callback handler
X509GenerateCallbackHandler callbackHandler = new
X509GenerateCallbackHandler
"",
"dsig-sender.ks",
"jks",
"client".toCharArray(),
"soaprequester",
"client".toCharArray(),
"CN=SOAPRequester, OU=TRL, O=IBM, ST=Kanagawa, C=JP", null);
// Generate the security token used to the signature (step: c)
SecurityToken token = factory.newSecurityToken(X509Token.class, callbackHandler);
// Generate WSSSignature instance (step: d)
WSSSignature sig = factory.newWSSSignature(token);
// Set the part specified by WSSSignPart (step: e)
WSSSignPart sigPart = factory.newWSSSignPart();
// Set the part specified by WSSSignPart (step: f)
sigPart.setSignPart(WSSSignature.BODY);
// Set the digest method specified by WSSSignPart (step: g)
sigPart.setDigestMethod(WSSSignPart.SHA256);
// Set the transform method specified by WSSSignPart (step: h)
sigPart.setTransformMethod(WSSSignPart.TRANSFORM_STRT10);
// Add the part specified by WSSSignPart (step: i)
sig.addSignPart(sigPart);
// Add the WSSSignature to the WSSGenerationContext (step: j)
gencont.add(sig);
// Generate the WS-Security header (step: k)
gencont.process(msgcontext);
下一步做什么
使用 WSSVerifyPart API,或者通过管理控制台配置策略集来验证使用者端的签署部件。