Java Web 服务: WS-Security 的细粒度使用
2009-11-05 00:00:00 来源:WEB开发网在早期文章中,您已经了解了如何配置 Axis2/Rampart:将一个 WS-SecurityPolicy 文档连接到 org.apache.axis2.client.ServiceClient 实例(在客户机端),或者将策略文档嵌入到 services.xml 服务配置(在服务器端)。这个方法可以奏效,并且可用于测试,但是对于生产应用来说,最好通过将 WS-SecurityPolicy 内嵌到 WSDL 文档将其直接关联到服务定义。WS-Policy 和 WS-SecurityPolicy 旨在为这种嵌入提供支持,并且使用来自 <wsdl:binding>、<wsdl:binding>/<wsdl:operation> 或 <wsdl:binding>/<wsdl:operation>/<wsdl:message> 定义的引用标识将被应用到绑定、操作或消息的相应策略。Axis2 1.4.1 实现对内嵌在 WSDL 中的策略的初始处理,而这个实现在当前的 Axis2 1.5 发行版代码中得到了改进。为了演示策略在 WSDL 中的使用,本文结合使用了 Axis2 1.5 发行版代码和尚未发行的最新 Rampart 代码(后者最终会作为 Rampart 1.5 发布)。
清单 1 展示了示例应用程序的 WSDL,其中添加了策略,并在相应的位置中引用。(清单 1 针对长度和宽度进行了编辑;完整的 WSDL 可以从 代码下载 中的 library.wsdl 文件获得)。每个策略定义了一个 Id 值,该值随后从相应的操作(针对 UsernameToken 策略)或消息(针对签名策略)中引用,以粗体显示所有策略。
清单 1. 具有细粒度安全策略的 WSDL<wsdl:definitions targetNamespace="http://ws.sosnoski.com/library/wsdl"
xmlns:wns="http://ws.sosnoski.com/library/wsdl"
xmlns:tns="http://ws.sosnoski.com/library/types"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/">
<!-- Policy for signing message, with certificate from client included in each
message to server -->
<wsp:Policy wsu:Id="SignOnly" xmlns:wsu=
"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"
xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy">
<wsp:ExactlyOne>
<wsp:All>
<sp:AsymmetricBinding
xmlns:sp="http://docs.oasis-open.org/ws-sx/ws-securitypolicy/200702">
<wsp:Policy>
<sp:InitiatorToken>
<wsp:Policy>
<sp:X509Token sp:IncludeToken=".../IncludeToken/AlwaysToRecipient"/>
</wsp:Policy>
</sp:InitiatorToken>
...
</wsp:Policy>
</sp:AsymmetricBinding>
<sp:SignedParts
xmlns:sp="http://docs.oasis-open.org/ws-sx/ws-securitypolicy/200702">
<sp:Body/>
</sp:SignedParts>
</wsp:All>
</wsp:ExactlyOne>
</wsp:Policy>
<!-- Policy for UsernameToken with plaintext password, sent from client to
server only -->
<wsp:Policy wsu:Id="UsernameToken" xmlns:wsu=
"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"
xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy">
<wsp:ExactlyOne>
<wsp:All>
<sp:SupportingTokens
xmlns:sp="http://docs.oasis-open.org/ws-sx/ws-securitypolicy/200702">
<wsp:Policy>
<sp:UsernameToken sp:IncludeToken=".../IncludeToken/AlwaysToRecipient"/>
</wsp:Policy>
</sp:SupportingTokens>
</wsp:All>
</wsp:ExactlyOne>
</wsp:Policy>
...
<wsdl:binding name="LibrarySoapBinding" type="wns:Library">
<wsdlsoap:binding style="document"
transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="getBook">
<wsdlsoap:operation soapAction="urn:getBook"/>
<wsdl:input name="getBookRequest">
<wsdlsoap:body use="literal"/>
</wsdl:input>
<wsdl:output name="getBookResponse">
<wsdlsoap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="getBooksByType">
<wsp:PolicyReference
xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy"
URI="#UsernameToken"/>
<wsdlsoap:operation soapAction="urn:getBooksByType"/>
<wsdl:input name="getBooksByTypeRequest">
<wsdlsoap:body use="literal"/>
</wsdl:input>
<wsdl:output name="getBooksByTypeResponse">
<wsdlsoap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="addBook">
<wsdlsoap:operation soapAction="urn:addBook"/>
<wsdl:input name="addBookRequest">
<wsp:PolicyReference
xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy"
URI="#SignOnly"/>
<wsdlsoap:body use="literal"/>
</wsdl:input>
<wsdl:output name="addBookResponse">
<wsdlsoap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="library-granular">
...
</wsdl:service>
</wsdl:definitions>
更多精彩
赞助商链接