构建 WS-Security 的 XPath 表达式
JAX-RPC 和 JAX-WS WS-Security 配置使用基于 XML 的 SOAP 消息在应用程序之间交换信息。您可以使用 XPath 表达式来选择 SOAP 消息中的特定元素以进行签署或加密。
开始之前
如果要签署或加密简单对象访问协议 (SOAP) 消息中的元素,那么您可以使用 XPath 表达式来选择该消息中的特定元素。SOAP 1.1 消息与 SOAP 1.2 消息的格式不同,因此 SOAP 1.1 消息需要不同的 XPath 表达式来从每一个版本的消息中选择元素。JAX-WS 运行时环境同时支持 SOAP 1.1 和 SOAP 1.2,因此必须为您要选择的每一个元素都添加两个 XPath 表达式到 WS-Security 策略中:一个适用于 SOAP1.1,另一个适用于 SOAP 1.2。JAX-RPC 运行时环境仅支持 SOAP 1.1,因此必须将 SOAP 1.1 版本的 XPath 表达式添加到 WS-Security 策略中。
在用于从 JAX-WS WS-Security 配置中的 SOAP 消息中选择 Timestamp 元素的以下场景中,已突出显示了 SOAP 1.1 消息与 SOAP 1.2 消息在格式上的差异。
以下示例显示了包含 Timestamp 元素的 SOAP 1.1 消息。
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header>
<wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
<wsu:Timestamp wsu:Id="Timestamp-16" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
<wsu:Created>2012-01-11T12:55:41.781Z</wsu:Created>
<wsu:Expires>2012-01-11T15:42:21.781Z</wsu:Expires>
</wsu:Timestamp>
</wsse:Security>
</soapenv:Header>
</soapenv:Envelope>
以下示例显示了包含 Timestamp 元素的 SOAP 1.2 消息。
<soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope">
<soapenv:Header>
<wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
<wsu:Timestamp u:Id="uuid-169b0950-217e-48af-9057-ea832e0c7e19-14" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
<wsu:Created>2009-09-08T14:08:36.224Z</wsu:Created>
<wsu:Expires>2009-09-08T14:13:36.224Z</wsu:Expires>
</wsu:Timestamp>
</wsse:Security>
</soapenv:Header>
</soapenv:Envelope>
用于选择 SOAP 消息元素的 XPath 表达式位于 WS-Security 缺省策略中,该缺省策略包含 XML 数字签名的配置信息。在 JAX-WS WS-Security 配置中,XPath 表达式成对出现,每一个 XPath 表达式都对应于每一个受支持的 SOAP 版本。
以下示例显示了用于从上述示例中的 SOAP 1.1 消息和 SOAP 1.2 消息中选择 Timestamp 元素的 XPath 表达式。 第一个 XPath 表达式从 SOAP 1.1 消息中选择 Timestamp 元素,第二个 XPath 表达式从 SOAP 1.2 消息中选择 Timestamp 元素。
/*[namespace-uri()='http://schemas.xmlsoap.org/soap/envelope/' and local-name()='Envelope']
/*[namespace-uri()='http://schemas.xmlsoap.org/soap/envelope/' and local-name()='Header']
/*[namespace-uri()='http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd' and local-name()='Security']
/*[namespace-uri()='http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd' and local-name()='Timestamp']
/*[namespace-uri()='http://www.w3.org/2003/05/soap-envelope' and local-name()='Envelope']
/*[namespace-uri()='http://www.w3.org/2003/05/soap-envelope' and local-name()='Header']
/*[namespace-uri()='http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd' and local-name()='Security']
/*[namespace-uri()='http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd' and local-name()='Timestamp']
每一个版本的 XPath 表达式的前两行不同,但 XPath 表达式的最后两行相同。这两个 XPath 表达式之间的差异在于 Envelope 元素和 Header 元素的名称空间不同。XPath 表达式中的名称空间对应于 SOAP 1.1 消息和 SOAP 1.2 消息中的名称空间。
关于此任务
此项任务描述了如何构建用于选择元素以在 WS-Security 中签署或加密的 XPath 表达式。JAX-WS 安全性配置需要分别适用于 SOAP 1.1 和 SOAP 1.2 的不同 XPath 表达式,而 JAX-RPC 安全性配置则只需要适用于 SOAP 1.1 的 XPath 表达式。
如果要选择 WS-Security 中常用的 SOAP 消息元素,请参阅选项 1 来使用预先构建的代码段来构建 XPath 表达式。否则,请继续执行到选项 2 以了解如何为其他元素构建定制 XPath 表达式。

过程
结果
现在,已构建了用于选择 SOAP 1.1 消息头或 SOAP 1.2 消息头的元素的 XPath 表达式。
示例
/*[namespace-uri()='http://www.w3.org/2003/05/soap-envelope' and local-name()='Envelope']
/*[namespace-uri()='http://www.w3.org/2003/05/soap-envelope' and local-name()='Header']
/*[namespace-uri()='http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd' and local-name()='Security']
/*[namespace-uri()='http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd' and local-name()='UsernameToken']