使用SimpleXML创建WS-Security标头? [英] Create a WS-Security header using SimpleXML?

查看:81
本文介绍了使用SimpleXML创建WS-Security标头?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to create a WSS header to authentificate on secured web services.

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

I can do it using an ugly :

    $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使其更整洁.

I am now trying to do it cleaner, using SimpleXML.

但是,如果我尝试做一个简单的事情:

But if I try to do a simple :

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

我明白了:

警告:SimpleXMLElement :: __ construct()[simplexmlelement .-- construct]: 名称空间错误:安全性"中的名称空间前缀wsse未在

warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: namespace error : Namespace prefix wsse on Security is not defined in

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

I think I miss something with the way to create namespaced xmls, can you give me some hints?

推荐答案

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

I found a way to solve my problem :

$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命名空间.

And also, there is a mistake in my XML, I put a SOAP-ENV:mustUnderstand="1" but I never define the SOAP-ENV namespace.

这篇关于使用SimpleXML创建WS-Security标头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆