在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

在 Node.js 中设置计时器

在 WCF 客户端的代码中设置消息版本

如何在 Winston/Node.js 中设置日志级别

如何在 WPF 应用程序中 SOAP 客户端的“Set-Cookie”标头响应的新请求中设置“Cookie”标头

在 node.js 中的 HTTP GET 中设置 URL 参数的默认值