【问题标题】:How do I add custom SOAP Headers with specific namespaces and prefixes with Spyne?如何使用 Spyne 添加具有特定命名空间和前缀的自定义 SOAP 标头?
【发布时间】:2016-05-09 18:35:06
【问题描述】:

我正在尝试使用 Django + Spyne 2.11/2.12.x 模拟现有的 Axis 1.4 服务,并且需要具有特定命名空间前缀 (wsse / wsu) 的 WS-security Timestamp 令牌。我将它与已经正常工作的 suds 数字签名插件 (sudssigner) 一起使用。

向 spyne 添加动态 SOAP 标头的推荐方法是什么?

如何强制使用具体的命名空间前缀?

更新:WS 响应应尽可能接近以下示例:

<?xml version='1.0' encoding='utf-8'?>
<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" soapenv:mustUnderstand="1">
            <wsu:Timestamp xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" wsu:Id="Timestamp-15452452">
                <wsu:Created>2016-02-01T10:14:54.517Z</wsu:Created>
                <wsu:Expires>2016-02-01T10:19:54.517Z</wsu:Expires>
            </wsu:Timestamp>
            <ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#" Id="Signature-2088192064">
                <ds:SignedInfo>
                    <ds:CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#" />
                    <ds:SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1" />
                    <ds:Reference URI="#Id-1052429873">
                        <ds:Transforms>
                            <ds:Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#" />
                        </ds:Transforms>
                        <ds:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" />
                        <ds:DigestValue>...</ds:DigestValue>
                    </ds:Reference>
                    <ds:Reference URI="#Timestamp-15452452">
                        <ds:Transforms>
                            <ds:Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#" />
                        </ds:Transforms>
                        <ds:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" />
                        <ds:DigestValue>...</ds:DigestValue>
                    </ds:Reference>
                </ds:SignedInfo>
                <ds:SignatureValue>
...
                </ds:SignatureValue>
                <ds:KeyInfo Id="KeyId-8475839474">
                    <wsse:SecurityTokenReference xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" wsu:Id="STRId-680050181">
                        <wsse:KeyIdentifier EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary" ValueType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509SubjectKeyIdentifier">...</wsse:KeyIdentifier>
                    </wsse:SecurityTokenReference>
                </ds:KeyInfo>
            </ds:Signature>
        </wsse:Security>
    </soapenv:Header>
    <soapenv:Body xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" wsu:Id="Id-1052429873">
...
    </soapenv:Body>
</soapenv:Envelope>

提前感谢。

【问题讨论】:

    标签: python django web-services soap spyne


    【解决方案1】:

    向 spyne 添加动态 SOAP 标头的推荐方法是什么?

    Spyne 已经实现了动态 SOAP 标头。如果您想使用 Spyne 向响应中添加 SOAP 标头,请参见此处的示例:

    https://github.com/arskom/spyne/blob/dec5286212602fb793db10ea67c5a1bdcad36315/spyne/test/interop/server/_service.py#L144

    如何强制使用具体的命名空间前缀?

    什么是“具体的命名空间前缀”?如果您希望您的对象位于命名空间中(具体或不具体),请将它们放在一个中:

    class SomeClass(ComplexModel):
        __namespace__ = "https://a.very.concrete/namespace"
    
        i = Integer
        s = Unicode
        # etc
    

    【讨论】:

    • 使用wsse作为命名空间http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd的前缀,wsu作为http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd的前缀
    • Spyne 只是一个 xml 库,它没有实现 wsse。您需要在服务定义中设置__out_header__ = Security,其中“Security”是您根据 WSSE 规范实现的类。
    • @BurakArslan 我将Security 定义为ComplexModel 的子类,并将__in_header__ = Security 添加到我的Service 类中。这种方法适用于发送原始 xml 有效负载,例如通过邮递员,但是当我尝试使用客户端库使用服务时出现错误,因为客户端库正在名称空间 (http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd) 中查找名为 Security 的消息避免这种情况的方法?我不希望将安全性定义为我的 WSDL 中的消息。
    • 我解决了命名空间问题,方法是继承 ComplexModel 并覆盖 resolve_namespace 函数,以便在 WSDL 中为消息提供不同的命名空间。服务仍然可以接受和处理标头中的 wsse:Security 标签。
    猜你喜欢
    • 2014-02-25
    • 1970-01-01
    • 1970-01-01
    • 2019-01-12
    • 2012-10-22
    • 1970-01-01
    • 1970-01-01
    • 2012-05-17
    • 1970-01-01
    相关资源
    最近更新 更多