【问题标题】:Send a SOAP request with WSSecurity in NodeJS在 NodeJS 中使用 WSSecurity 发送 SOAP 请求
【发布时间】:2017-06-28 22:29:22
【问题描述】:

我正在尝试请求此内部服务,负责它的团队说它需要用户名 + 密码并使用证书加密。

我想到了使用这个模块node-soap,我在文档中找到了这个:

1-https://github.com/vpulim/node-soap#wssecurity

2-https://github.com/vpulim/node-soap#wssecuritycert

它解释了如何实现 WSSecurity,但是一个规则覆盖了另一个。所以这段代码不起作用:

var wsSecurity = new soap.WSSecurity('username', 'password', options)
client.setSecurity(wsSecurity);

var wsSecurity = new soap.WSSecurityCert(privateKey, publicKey, password);
client.setSecurity(wsSecurity);

使用这两种策略的正确方法是什么?

我是 SOAP 新手,非常感谢任何帮助

【问题讨论】:

  • 我真正需要的是如何在 Node 上实现这两件事,也许会改变这个模块。
  • 没有多大意义。当你有密钥时,你不需要用户名和密码。
  • 我同样需要添加两种类型的安全性(这就是我需要与之交谈的服务的设置方式)。我想知道@VictorFerreira,你找到解决方案了吗?

标签: node.js soap wsdl ws-security


【解决方案1】:

我遇到了同样的要求。我正在构建一个自定义的 WSSecurityCertSSL 安全模块,它不是很好,但可能只是工作。最好的办法是修改 node-soap,以便您可以堆叠多个证券,因为一些(即:ssl)仅处理连接,而另一些处理信封操作(即:WssSecurity)。

【讨论】:

    【解决方案2】:

    我完全反对修改任何第 3 方依赖项的来源。它通常会导致未来的问题(更新、兼容性、未预料到的错误等) 这是我堆叠证券的尝试。

    import { IHeaders, ISecurity } from "soap";
    
    export class SecurityStack implements ISecurity {
        private stack: ISecurity[];
        public constructor(...security: ISecurity[]) {
            this.stack = security;
    
            const hasPostProcessMethod = this.stack.some(s => s["postProcess"]);
            const hasToXmlMethod = this.stack.some(s => s["toXML"]);
    
            if(hasPostProcessMethod && hasToXmlMethod)
                throw new Error("Security with `postProcess` and those with `toXml` methods cannot be used together");
    
            if(!hasPostProcessMethod)
                this.postProcess = undefined;
        }
    
        public addOptions(options: any): void {
            this.stack.forEach(security => {
                if(security["addOptions"])
                    security.addOptions(options);
            });
        }
    
        public toXML(): string {
            let result = "";
            this.stack.forEach(security => {
                if(security["toXML"])
                    result += security.toXML();
            });
            return result;
        }
    
        public addHeaders(headers: IHeaders): void {
            this.stack.forEach(security => {
                if(security["addHeaders"])
                    security.addHeaders(headers);
            });
        }
    
        public postProcess?(xml: any, envelopeKey: any): string {
            let result = xml;
            this.stack.forEach(security => {
                if(security["postProcess"])
                    result = security.postProcess(xml, envelopeKey);
            });
            return result;
        }
    }
    

    然后用法:

    const client =  await soap.createClientAsync(wsdl);
    const sslSecurity = new soap.ClientSSLSecurity(xxx, xxx);
    const wsSecurity = new soap.WSSecurity("xxx","xxx");
    const securityStack = new SecurityStack(sslSecurity, wsSecurity);
    client.setSecurity(securityStack);
    

    请记住,并非所有安全方法都可以组合使用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-13
      • 1970-01-01
      • 1970-01-01
      • 2017-07-29
      相关资源
      最近更新 更多