【问题标题】:Create a WS-Security header using SimpleXML?使用 SimpleXML 创建 WS-Security 标头?
【发布时间】:2012-12-04 15:42:44
【问题描述】:

我想创建一个 WSS 标头以在安全的 Web 服务上进行身份验证。

我可以用丑陋的来做到这一点:

    $auth = '
     <wsse:Security SOAP-ENV:mustUnderstand="1" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
        <wsu:Timestamp wsu:Id="Timestamp-28" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
            <wsu:Created>' . $timestamp . '</wsu:Created>
            <wsu:Expires>' . $timestampExpires . '</wsu:Expires>
        </wsu:Timestamp>
        <wsse:UsernameToken wsu:Id="UsernameToken-27" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
            <wsse:Username>' . $user . '</wsse:Username>
            <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordDigest">' . $passdigest . '</wsse:Password>
            <wsse:Nonce EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary">' . $encodedNonce . '</wsse:Nonce>
            <wsu:Created>' . $timestamp . '</wsu:Created>
        </wsse:UsernameToken>
     </wsse:Security>';

我现在正在尝试使用 SimpleXML 将其做得更干净。

但如果我尝试做一个简单的:

    $xml = new SimpleXMLElement('<wsse:Security/>', 0, false, 'wsse');

我明白了:

警告:SimpleXMLElement::__construct() [simplexmlelement.--construct]: 命名空间错误:安全性上的命名空间前缀 wsse 未在

中定义

我想我错过了创建命名空间 xml 的方法,你能给我一些提示吗?

【问题讨论】:

    标签: php xml namespaces simplexml


    【解决方案1】:

    我找到了解决问题的方法:

    $root = new SimpleXMLElement('<root/>');
    
    $security = $root->addChild('wsse:Security', 'test', 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd');
    
    $root->registerXPathNamespace('wsse', 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd');
    $auth = $root->xpath('/root/wsse:Security');
    echo htmlentities($auth[0]->asXML());
    

    显示:

    <wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">test</wsse:Security> 
    

    而且,我的 XML 中有一个错误,我放了一个 SOAP-ENV:mustUnderstand="1",但我从未定义 SOAP-ENV 命名空间。

    【讨论】:

      猜你喜欢
      • 2011-04-26
      • 2011-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多