节点 soap,使用受密码保护的 WSDL



我正在尝试使用Node构建SOAP客户端,我正在使用"soap"包(https://www.npmjs.org/package/soap)尝试使用受用户/密码保护的WSDL。

在通过"soap.createClient"创建客户端之前,我找不到如何传递这些凭据,当然,如果我不提供正确的凭据,我将无法检索 WSDL。

我试过做:

soap.security.WSSecurity('user', 'pass');

然后调用"createClient",但无济于事。

另外,我

尝试使用node-soap-client来做到这一点,有了这个客户端,我(显然)可以连接到WSDL,但在那之后,我不知道该去哪里(如何调用方法)。

我做错了什么?

感谢您的帮助!

用户名和密码凭据可以像这样传递:

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,谢谢加布里埃尔·卢塞纳 https://github.com/glucena)

如果其密码受保护,您还需要检查正确的安全机制。我花了一天时间试图弄清楚该服务是否使用了 NTLM 安全性(这是一个客户端项目,我只有用户名和密码来访问 wsdl)。在这种情况下,您需要传递正确的wsdl_options对象

var wsdl_options = {
  ntlm: true,
  username: "your username",
  password: "your password",
  domain: "domain",
  workstation: "workstation"
}
soap.createClient(data.credentials[data.type], {
    wsdl_options
  },
  function(err, client) {
    console.log(client.describe());
  });

此外,在使用任何服务之前,您需要在客户端上设置安全性。完整解释的链接:https://codecalls.com/2020/05/17/using-soap-with-node-js/

当我将身份验证添加到标头时,我仍然遇到问题。在阅读了代码和一些文章后,我发现这是有效的。

// or use local wsdl if security required
let url = 'http://service.asmx?wsdl' 
let wsdl = 'wsdl.wsdl';
let soap = require('soap');
let util = require('util')
soap.createClient(wsdl, function(err, client) {
    //don't forget to double slash the string or else the base64 will be incorrect
    client.setSecurity(new soap.BasicAuthSecurity('admin\userName', 'password'));
    client.MethodFromWSDL(args, function (err, result) {
        console.log(util.inspect(result,{depth: null}))
    });
});

这对我有用,API 需要身份验证参数作为

<UserDetails xmlns="http://url/">';
  <userName>{$Username}</userName>
  <password>{$Password}</password>
  <program>{$program}</program>
</UserDetails>

经过大量的试验和错误 - 这结束了工作

const soapHeader = {
  UserDetails: {
   userName: process.env.userName,
   password: process.env.password,
   program: process.env.program
  }
}

soap.createClient(path, function (err, client) {
if (err) {
  console.log('Error creating SOAP client: ' + err);
}
client.addSoapHeader(soapHeader, "", "tns", process.env.URN);
client[process.env.FUNCTION](sargs, function (err, result, rawResponse, soapHeader, rawRequest) {
  if (err) {
    console.log('Error call SOAP function ' +  process.env.FUNCTION + ': ', err);
  }
  else {
    console.log(result);
  }
  ...

最新更新