创建使用SimpleXML一个WS-Security头?(Create a WS-Security

2019-10-17 10:45发布

想创建一个WSS头authentificate担保网络服务。

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

    $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 :: __结构()的SimpleXMLElement .--构建物]:命名空间错误:在安全命名空间前缀WSSE没有定义

我想我会错过一些与创建命名空间的个XML的方式,你可以给我一些提示?

Answer 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命名空间。



文章来源: Create a WS-Security header using SimpleXML?