我在尝试向服务器发送soap请求(soapCall)时遇到错误。
Fatal error: Uncaught SoapFault exception: [ns1:InvalidSecurity] An error was discovered processing the <wsse:Security> header我需要发送ws-security头。
<wsse:Security soapenv:mustUnderstand="1" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
<wsse:UsernameToken wsu:Id="UsernameToken-1" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
<wsse:Username>userID</wsse:Username>
<wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">passwd</wsse:Password>
<wsse:Nonce EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary">ZTQ3YmJjZmM1ZTU5ODg3YQ==</wsse:Nonce>
<wsu:Created>2013-07-05T19:55:36.458Z</wsu:Created>
</wsse:UsernameToken>
</wsse:Security>经过大量的研究,我认为我得到的问题是nonce不符合要求。在我编写soap标头时,它看起来就像我得到的示例。唯一未知的元素是计算这个现时值...
从我得到的示例nonce中,它是一组24个数字+字母表+特殊字符
像这样的东西
ZTQ3YmJjZmM1ZTU5ODg3YQ==但是,我不太确定如何从php...is计算wsse现时值,有没有标准?
我拥有的代码
$nonce = sha1(mt_rand());结果
dabddf9dbd95b490ace429f7ad6b55c3418cdd58这是与example...and完全不同的东西,我相信这就是为什么这个代码不能工作的原因。
所以我做了更多的研究,现在我用这个
$NASC = substr(md5(uniqid('the_password_i_am _using', true)), 0, 16);
$nonce = base64_encode($NASC); 结果
NzJlMDQ4OTAyZWIxYWU5ZA==现在,它看起来与示例类似,但我仍然看到从一开始就显示的错误。
有人能帮我一下吗?
使用soapUI进行进一步的测试。
使用相同的userID和密码,将密码类型设置为密码文本
而且它正在发挥作用。
有人知道soapUI是如何计算随机数的吗?或者知道soapUI是如何通过ws-security的吗?
发布于 2013-09-21 20:29:43
试试这样的东西
string usn = "MyUsername";
string pwd = "MyPassword";
DateTime created = DateTime.Now.ToUniversalTime();
var nonce = getNonce();
string nonceToSend = Convert.ToBase64String(Encoding.UTF8.GetBytes(nonce));
string createdStr = created.ToString("yyyy-MM-ddTHH:mm:ssZ");
string passwordToSend = GetSHA1String(nonce + createdStr + pwd);和函数:
protected string getNonce()
{
string phrase = Guid.NewGuid().ToString();
return phrase;
}
protected string GetSHA1String(string phrase)
{
SHA1CryptoServiceProvider sha1Hasher = new SHA1CryptoServiceProvider();
byte[] hashedDataBytes = sha1Hasher.ComputeHash(Encoding.UTF8.GetBytes(phrase));
string test = Convert.ToString(hashedDataBytes);
return Convert.ToBase64String(hashedDataBytes);
}发布于 2015-07-03 20:19:12
由于uniqid()基于伪随机数生成器,因此它不能提供足够的熵。Siehe Insufficient Entropy For Random Values
$nonce = base64_encode( bin2hex( openssl_random_pseudo_bytes( 16 ) ) );如果您没有OpenSSL模块,请尝试使用mcrypt_create_iv(),请参阅:
发布于 2013-09-04 01:15:01
微软将WS-Security现时值定义为:
The nonce is 16 bytes long and is passed along as a base64 encoded value.下面的.Net代码生成遵循Microsoft -Security标准的代码:
$prefix = gethostname();
$nonce = base64_encode( substr( md5( uniqid( $prefix.'_', true)), 0, 16));一些没有使用$prefix的测试是成功的,但是这段代码的生产版本使用了$prefix,到目前为止还没有遇到任何身份验证问题。这个随机数代码的原始版本来自以下库(对substr中返回的字符数进行了修改):http://code.ronoaldo.net/openemm/src/e25a2bad5aa7/webservices/WSSESoapClient.php#cl-267
https://stackoverflow.com/questions/18117695
复制相似问题