在Node.js SOAP客户端中设置授权

时间:2014-11-08 11:38:59

标签: node.js web-services soap

我想通过Node.js中的SOAP客户端访问WSDL服务。我使用了 soap 节点模块。但我无法找到任何设置用户名和密码的文档。我不打算创建SOAP服务器,我只想要类似于PHP的SoapClient的SOAPClient,使用它我可以访问WSDL服务。

更新

我已经分叉并自定义了源代码以支持此功能https://github.com/sincerekamal/node-soap

5 个答案:

答案 0 :(得分:21)

您可以提供如下用户名和密码:

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

答案 1 :(得分:7)

添加基本身份验证的另一个选项是使用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);
});

答案 2 :(得分:6)

只是为了分享我从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'));
});

答案 3 :(得分:3)

您需要通过将授权传递给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();
    }
});

答案 4 :(得分:2)

对现有答案进行一些细微调整:例如,您也可以使用安全性对象为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.
});