在Node.js SOAP客户端中设置授权
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在Node.js SOAP客户端中设置授权相关的知识,希望对你有一定的参考价值。
我想通过Node.js中的SOAP客户端访问WSDL服务。我用肥皂节点模块。但我无法找到任何设置用户名和密码的文档。我不打算创建SOAP服务器,我只想要类似于php的SoapClient的SOAPClient,使用它我可以访问WSDL服务。
更新:
我已经分叉并定制了源代码来支持这个功能https://github.com/sincerekamal/node-soap
您可以提供如下的用户名和密码:
var soap = require('soap');
var url = 'your WSDL url';
var auth = "Basic " + new Buffer("your username" + ":" + "your password").toString("base64");
soap.createClient(url, { wsdl_headers: {Authorization: auth} }, function(err, client) {
});
(来自https://github.com/vpulim/node-soap/issues/56,谢谢Gabriel Lucena https://github.com/glucena)
添加基本身份验证的另一个选项是使用client.addHttpHeader。我尝试了setSecurity和设置wsdl_headers,但在对Cisco CUCM AXL进行身份验证时,我都没有工作。
这对我有用:
var soap = require('soap');
var url = 'AXLAPI.wsdl'; // Download this file and xsd files from cucm admin page
var auth = "Basic " + new Buffer("your username" + ":" + "your password").toString("base64");
soap.createClient(url,function(err,client){
client.addHttpHeader('Authorization',auth);
});
只是为了分享我从https://github.com/vpulim/node-soap读到的内容:
var soap = require('soap');
var url = 'your WSDL url';
soap.createClient(url, function(err, client) {
client.setSecurity(new soap.BasicAuthSecurity('your username','your password'));
});
您需要通过将授权传递给wsdl_headers对象来设置用户名和密码,例如
var auth = "Basic " + new Buffer('username' + ':' + 'password').toString("base64");
var client = Soap.createClient('wsdlUrl', { wsdl_headers: { Authorization: auth } }, (err, client) => {
if (err) {
throw err;
} else {
client.yourMethod();
}
});
对现有答案的一个小调整:您可以使用安全对象来创建WSDL请求的标头,例如
const security = new soap.BasicAuthSecurity(username, password);
const wsdl_headers = {};
security.addHeaders(wsdl_headers);
soap.createClientAsync(url, { wsdl_headers }).then((err, client) => {
client.setSecurity(security);
// etc.
});
或者如果你使用比BasicAuthSecurity更复杂的东西,你可能还需要从安全对象设置wsdl_options,例如
const security = new soap.NTLMSecurity(username, password, domain, workstation);
const wsdl_headers = {}, wsdl_options = {};
security.addHeaders(wsdl_headers);
security.addOptions(wsdl_options);
soap.createClientAsync(url, { wsdl_headers, wsdl_options }).then((err, client) => {
client.setSecurity(security);
// etc.
});
以上是关于在Node.js SOAP客户端中设置授权的主要内容,如果未能解决你的问题,请参考以下文章
如何在 express.js node.js 中设置 X-Frame-Options