使用 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)一起使用。

签名信息指定适用于已生成消息的完整性约束。签署部件用于保护消息的完整性。可以指定要添加的签署部件以保护消息完整性。

下表显示了定义数字签名安全性约束(完整性)时的必需签署部件:

表 1. 签署部件信息. 使用签署部件来保护消息。
签署部件 描述
keyword 使用关键字添加签署部件。WebSphere Application Server 支持对签署部件使用以下关键字:
  • BODY
  • ADDRESSING_HEADERS
  • TIMESTAMP
WS-Addressing 头未进行加密,但可以签署。
xpath 通过使用 XPath 表达式添加必需签署部件。
header 将 QName 指定的头添加为签署部件。
timestamp 将 WSSTimestamp 对象添加为签署对象。如果指定,那么时间戳记信息将指定生成消息及消息到期的时间。

可以在生成器端上请求的消息保护中指定不同的消息部件。WSSSignPart 允许添加变换算法、设置摘要方法、将对象设置为目标、指定元素以及指定诸如 SOAP 主体、WS-Addressing 头和时间戳记信息之类的签署部件。

为了获取签名信息,将执行特定的缺省行为。使用 WSSSignPart API 的最简单方法是使用缺省行为(请参阅示例代码)。缺省签署部件行为包括:

表 2. 签署部件的缺省行为. 缺省情况下,已配置多个签名部分特征。
签名决定 缺省行为
要签署的 SOAP 消息部件

WebSphere Application Server 支持对以下 SOAP 消息部件进行签署并将它们用于消息保护:

  • WSSSignature.BODY
  • WSSSignature.ADDRESSING_HEADERS
  • WSSSignature.TIMESTAMP
要使用的摘要方法

设置摘要算法方法。在 <SigningInfo> 元素中使用在 <DigestMethod> 元素中指定的摘要方法算法。

WebSphere Application Server 支持以下预配置的摘要方法:
  • WSSSignPart.SHA1(缺省值):http://www.w3.org/2000/09/xmldsig#sha1
  • WSSSignPart.SHA256:http://www.w3.org/2001/04/xmlenc#sha256
  • WSSSignPart.SHA512:http://www.w3.org/2001/04/xmlenc#sha512
要使用的变换算法 添加变换方法。变换算法是在 <Transform> 元素中指定的算法,并指定签名的变换算法。

WebSphere Application Server 支持以下预先配置的变换算法:

  • WSSSignPart.TRANSFORM_EXC_C14N(缺省值):http://www.w3.org/2001/10/xml-exc-c14n#
  • WSSSignPart.TRANSFORM_XPATH2_FILTER:http://www.w3.org/2002/06/xmldsig-filter2

    使用此变换方法来确保与基本安全概要文件 (BSP) 的一致性。

  • WSSSignPart.TRANSFORM_STRT10:http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#STR-Transform
  • WSSSignPart.TRANSFORM_ENVELOPED_SIGNATURE:http://www.w3.org/2000/09/xmldsig#enveloped-signature

过程

  1. 要通过使用 WSS API (WSSSignPart) 来支持 Web Service 安全性,请首先确保安装了应用程序服务器。
  2. 使用 WSSSignPart API 来签署消息部件并在 SOAP 消息中指定算法。 用于签署部件的 WSS API 过程遵循以下过程步骤:
    1. 使用 WSSFactory.getInstance() 获取 WSS API 实现实例。
    2. 根据 WSSFactory 实例创建 WSSGenerationContext 实例。
    3. 根据 WSSFactory 创建 SecurityToken 以配置密钥进行签署。
    4. 根据 WSSFactory 实例使用 SecurityToken 创建 WSSSignature。
    5. 根据 WSSFactory 实例创建 WSSSignPart。
    6. 设置要签署的部件及由步骤 g 或步骤 h 指定的摘要方法或变换方法(如果缺省值不合适)。
    7. 设置摘要方法(如果缺省值不合适)。
    8. 设置变换方法(如果缺省值不合适)。
    9. 将 WSSSignPart 添加至 WSSSignature。 将任何 WSSSignPart 设置为 WSSSignature 后,将忽略在 WSSSignature 中指定的要签署的缺省部件。
    10. 将 WSSSignature 添加至 WSSGenerationContext。
    11. 使用 SOAPMessageContext 调用 WSSGenerationContext.process()。

结果

您完成了相应的步骤来配置绑定文件的生成器部分的签署部件。如果存在错误情况,那么将提供 WSSException。如果成功,那么将调用 WSSGenerationContext.process() 并将 Web Service 安全性应用于 SOAP 消息。

示例

以下示例提供了样本代码,这段代码使用了在 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);
注: 由于使用专用密钥进行签署,因此 X509GenerationCallbackHandler 需要密钥密码。

下一步做什么

使用 WSSVerifyPart API,或者通过管理控制台配置策略集来验证使用者端的签署部件。


指示主题类型的图标 任务主题



时间戳记图标 最近一次更新时间: last_date
http://www14.software.ibm.com/webapp/wsbroker/redirect?version=cord&product=was-nd-mp&topic=twbs_confsignaturepartgen
文件名:twbs_confsignaturepartgen.html