技术开发 频道

Java Web 服务: Axis2 WS-Security 基础

  【IT168技术文档】安全性对于众多企业服务来说都是一个重要需求。并且,尝试实现自己的安全性也颇具风险,因为任何微小的疏忽都会导致严重的安全漏洞。这些特征引起了人们对安全处理标准化的兴趣,许多专家为标准作出贡献并避免出现任何个人的疏漏。基于 SOAP 的 Web 服务可以使用广泛支持的 WS-Security 和相关标准来满足它们的安全需求,允许针对每种服务相应地配置安全性。

  Apache Axis2 通过 Rampart 模块支持这些安全标准(见 参考资料)。在本文中,您将看到如何为 Axis2 安装、配置和使用 Rampart 以实现基本的安全功能,即在一个服务请求中发送用户名和密码。在本系列的后续文章中,您将理解如何使用 Rampart 实现更加复杂的安全性。

  WS-Security

  WS-Security 是向 SOAP Web 服务消息交换添加安全性的一种标准(见 参考资料)。它使用一个 SOAP 消息头部元素将安全信息附加到消息中,使用令牌(token)的形式传递不同类型的声明(包括名称、身份、密匙、组、特权、功能等等)以及加密和数字签名信息。WS-Security 支持多种形式的令牌、多个可信任域、多种签名格式和多种加密技术,因此大多数情况下头部信息需要针对每种内容包含特定的格式和算法识别。附加的信息会导致头部信息产生复杂的结构,如 清单 1 所示(进行了大量编辑)— 包含签名和加密的样例消息:

  清单 1. 包含签名和加密的样例消息

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" ...>
<soap:Header>
  
<wsse:Security soap:mustUnderstand="1">
  
<wsu:Timestamp wsu:Id="Timestamp-d2e3c4aa-da82-4138-973d-66b596d66b2f">
    
<wsu:Created>2006-07-11T21:59:32Z</wsu:Created>
    
<wsu:Expires>2006-07-12T06:19:32Z</wsu:Expires>
  
</wsu:Timestamp>
  
<wsse:BinarySecurityToken ValueType="...-x509-token-profile-1.0#X509v3"
     EncodingType
="...-wss-soap-message-security-1.0#Base64Binary"
     xmlns:wsu
="...oasis-200401-wss-wssecurity-utility-1.0.xsd"
     wsu:Id
="SecurityToken-faa295...">MIIEC56MQswCQY...</wsse:BinarySecurityToken>
  
<xenc:EncryptedKey xmlns:xenc="http://www.w3.org/2001/04/xmlenc#">
    
<xenc:EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#rsa-1_5" />
    
<KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#">
    
<wsse:SecurityTokenReference>
      
<wsse:KeyIdentifier ValueType=
      
"...#X509SubjectKeyIdentifier">LlYsHyhNnOVA9Aj7...</wsse:KeyIdentifier>
    
</wsse:SecurityTokenReference>
    
</KeyInfo>
    
<xenc:CipherData>
    
<xenc:CipherValue>g+A2WJhsoGBKUydZ9Za...</xenc:CipherValue>
    
</xenc:CipherData>
    
<xenc:ReferenceList>
    
<xenc:DataReference URI="#EncryptedContent-ba0556c3-d443-4f34-bcd1-14cbc32cd689" />
    
</xenc:ReferenceList>
  
</xenc:EncryptedKey>
  
<Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
    
<SignedInfo>
    
<ds:CanonicalizationMethod
       Algorithm
="http://www.w3.org/2001/10/xml-exc-c14n#"
       xmlns:ds
="http://www.w3.org/2000/09/xmldsig#" />
    
<SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1" />
    
<Reference URI="#Id-c80f735c-62e9-4001-8094-702a4605e429">
      
<Transforms>
      
<Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#" />
      
</Transforms>
      
<DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" />
      
<DigestValue>lKjc5nyLQDZAIu/hZb4B6mLquow=</DigestValue>
    
</Reference>
     ...
    
</SignedInfo>
    
<SignatureValue>TiLmWvlz3mswinLVQn58BgYS0368...</SignatureValue>
    
<KeyInfo>
    
<wsse:SecurityTokenReference>
      
<wsse:Reference URI="#SecurityToken-faa295..."
        ValueType
="...-x509-token-profile-1.0#X509v3" />
    
</wsse:SecurityTokenReference>
    
</KeyInfo>
  
</Signature>
  
</wsse:Security>
</soap:Header>
<soap:Body wsu:Id="Id-8db9ff44-7bef-4737-8091-cdac51a34db8">
  
<xenc:EncryptedData Id="EncryptedContent-ba05..."
    Type
="http://www.w3.org/2001/04/xmlenc#Content"
    xmlns:xenc
="http://www.w3.org/2001/04/xmlenc#">
  
<xenc:EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#aes128-cbc"/>
  
<xenc:CipherData>
    
<xenc:CipherValue>mirmi0KuFEEI56eu2U3cICz...</xenc:CipherValue>
  
</xenc:CipherData>
  
</xenc:EncryptedData>
</soap:Body>
</soap:Envelope>

 

  WS-Security 应用于实际的 SOAP 消息交换。服务实现可以检验 WS-Security 是否恰当应用于传入的消息,但是客户机需要提前知道它们必须实现什么才能使用服务。由于 WS-Security 的复杂性以及所支持的选项有限,因此仅使用文本描述很难实现这个目的,而手动配置 WS-Security 处理极容易出现错误。WS-Policy 是一个用于为 Web 服务指定扩展需求的通用结构,而 WS-SecurityPolicy 是一个专门针对 WS-Security 支持的 WS-Policy 扩展。这两者合起来可以以机器可读的方式描述 WS-Security 需求。WS-Policy 和 WS-SecurityPolicy 信息可以单独使用,也可以直接嵌入到 Web Services Description Language (WSDL) 文档内部,这样 Web 服务框架就可以将它们自动配置为服务的需求。

0
相关文章