在NodeJS中使用WSSecurity发送SOAP请求

时间:2017-06-28 22:29:23

标签: node.js soap wsdl ws-security

我试图请求这个内部服务,负责它的团队说它需要一个用户名+密码并且需要用证书加密。

我想过使用这个模块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的新手,任何帮助都会非常感激

2 个答案:

答案 0 :(得分:0)

我遇到了同样的要求。我正在构建一个自定义WSSecurityCertSSL安全模块,虽然不是很好,但是可能会起作用。最好的办法是修改节点肥皂,以便您可以堆叠多个证券,因为某些证券(即:ssl)仅处理连接,而其他证券(包括:WssSecurity)处理信封。

答案 1 :(得分:0)

我完全反对修改任何第三方依赖的来源。它通常会导致将来出现问题(更新,兼容性,意外的错误等) 这是我尝试堆叠证券的尝试。

CKEDITOR.editorConfig = function (config) {

config.allowedContent = true;
config.disableNativeSpellChecker = false;
config.scayt_autoStartup = false;
// Define changes to default configuration here. For example:

// add custom font
config.contentsCss = 'fontcalibri.css';
config.font_names = 'Calibri/Calibri;' + 
config.font_names;
config.contentsCss = 'fontopen-sans.css';
config.font_names = 'Open Sans/Open Sans'; + 
config.font_names;


config.pasteFromWordRemoveFontStyles=false;
config.pasteFromWordRemoveStyles=false;
};

然后使用:

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

请记住,并非每种安全方法都可以组合。