将命名空间添加到 Suds 中的默认 WSSE 安全对象

发布于 2024-11-30 23:29:20 字数 1143 浏览 0 评论 0原文

我了解如何向 SOAP 请求添加标头。但这会生成一个与我需要传递的标头不匹配的标头。返回此标头:

   <SOAP-ENV:Header>
      <wsse:Security mustUnderstand="true">
         <wsse:UsernameToken>
            <wsse:Username>CABLE</wsse:Username>
            <wsse:Password>CABLE</wsse:Password>
         </wsse:UsernameToken>
      </wsse:Security>
   </SOAP-ENV:Header>

但是,我需要修改该标头的命名空间以传递 Security 对象和 UsernameToken 对象的特定命名空间。我似乎无法弄清楚如何覆盖提供的默认值。

<soapenv:Header>
 <wsse:Security xmlns:wsse="http://schemas.xmlsoap.org/ws/2002/07/secext">
       http://schemas.xmlsoap.org/ws/2002/07/secext
 <wsse:UsernameToken xmlns:wsu="http://schemas.xmlsoap.org/ws/2002/07/utility">
 <wsse:Username>CABLE</wsse:Username>
 <wsse:Password Type="wsse:PasswordText">CABLE</wsse:Password>
 </wsse:UsernameToken>
 </wsse:Security>
</soapenv:Header>

这是生成上述内容的Python代码

security = Security()
token = UsernameToken('CABLE', 'CABLE')
security.tokens.append(token)
client.set_options(wsse=security)

I understand how to add a header to the SOAP request. But that generates a header that doesn't match the one I need to pass. That returns this header:

   <SOAP-ENV:Header>
      <wsse:Security mustUnderstand="true">
         <wsse:UsernameToken>
            <wsse:Username>CABLE</wsse:Username>
            <wsse:Password>CABLE</wsse:Password>
         </wsse:UsernameToken>
      </wsse:Security>
   </SOAP-ENV:Header>

However, I need to modify the namespace of that header to pass the particular namespace for the Security object and the UsernameToken object. I can't seem to figure out how to override the default values supplied.

<soapenv:Header>
 <wsse:Security xmlns:wsse="http://schemas.xmlsoap.org/ws/2002/07/secext">
       http://schemas.xmlsoap.org/ws/2002/07/secext
 <wsse:UsernameToken xmlns:wsu="http://schemas.xmlsoap.org/ws/2002/07/utility">
 <wsse:Username>CABLE</wsse:Username>
 <wsse:Password Type="wsse:PasswordText">CABLE</wsse:Password>
 </wsse:UsernameToken>
 </wsse:Security>
</soapenv:Header>

Here is the Python Code to generate the above

security = Security()
token = UsernameToken('CABLE', 'CABLE')
security.tokens.append(token)
client.set_options(wsse=security)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小猫一只 2024-12-07 23:29:20

想通了。这就是答案。只需使用 ns 参数

def createWSSecurityHeader(username,password):
    # Namespaces
    wsse = ('wsse', 'http://schemas.xmlsoap.org/ws/2002/07/secext')

    # Create Security Element
    security = Element('Security', ns=wsse)

    # Create UsernameToken, Username/Pass Element
    usernametoken = Element('UsernameToken', ns=wsse)
    uname = Element('Username', ns=wsse).setText(username)
    passwd = Element('Password', ns=wsse).setText(password)

    # Add Username and Password elements to UsernameToken element
    usernametoken.insert(uname)
    usernametoken.insert(passwd)

    security.insert(usernametoken)
    return security

Figured it out. Here is the answer. Just need to use the ns argument

def createWSSecurityHeader(username,password):
    # Namespaces
    wsse = ('wsse', 'http://schemas.xmlsoap.org/ws/2002/07/secext')

    # Create Security Element
    security = Element('Security', ns=wsse)

    # Create UsernameToken, Username/Pass Element
    usernametoken = Element('UsernameToken', ns=wsse)
    uname = Element('Username', ns=wsse).setText(username)
    passwd = Element('Password', ns=wsse).setText(password)

    # Add Username and Password elements to UsernameToken element
    usernametoken.insert(uname)
    usernametoken.insert(passwd)

    security.insert(usernametoken)
    return security
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文