【问题标题】:Node soap, consume password protected WSDLNode soap,使用受密码保护的 WSDL
【发布时间】:2014-11-19 22:47:17
【问题描述】:

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

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

我已经尝试过:

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

然后调用“createClient”但无济于事。

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

我做错了什么?

感谢您的帮助!

【问题讨论】:

    标签: node.js web-services soap wsdl


    【解决方案1】:

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

    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

    【讨论】:

      【解决方案2】:

      如果其密码受保护,您还需要检查正确的安全机制。我花了一天时间试图弄清楚该服务使用了 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/

      【讨论】:

        【解决方案3】:

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

        // 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}))
        
            });
        });
        

        【讨论】:

        • 你不需要 auth 来获取你的 WSDL,你需要 auth 来调用服务。 @iloo 的问题和答案就是关于这个的。不过,完全有可能有人需要两者。
        【解决方案4】:

        这对我有用,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);
          }
        
          ...
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-07-26
          • 2010-09-27
          • 2013-11-16
          • 1970-01-01
          • 2011-12-23
          • 2011-03-11
          • 2010-11-27
          相关资源
          最近更新 更多