2

我正在将node-soap lib 用于 SOAP 服务并首次使用它。我要求我需要在每个请求强制时都传递证书和基本授权标头。

我已经实现了我的代码如下:

var options = {
    wsdl_options: {
        key: fs.readFileSync(path.resolve("./xxx.key")),
        cert: fs.readFileSync(path.resolve("./xxx.crt")),
        ca: fs.readFileSync(path.resolve("./xxx.pem")),
    },
    wsdl_headers : {    
     Authorization : 'Basic ' + new Buffer(username +':'+ password ).toString('base64')
    },
    "overrideRootElement": {
        "namespace": "con",
    },
    envelopeKey : 'soapenv'
};



soap.createClient(url, options, function(err, client) {
    if(err){
        console.log("Error ::: >",err);
        res.json({message : err});
    }


    if(client){
        console.log(JSON.stringify(client.describe()));
        var data = actualRequestObject  

        client.setSecurity(new soap.ClientSSLSecurity(
            fs.readFileSync(path.resolve("./XXX.key")), 
            fs.readFileSync(path.resolve("./XXX.crt")),
            fs.readFileSync(path.resolve("./XXX.pem"))
        ));

        client.setSecurity(new soap.BasicAuthSecurity(username, password));
        client.IndicativeEnrichment(data, function(err, result){
            console.log("lastRequest :::: >>>>> ",client.lastRequest);
            if(err){
                console.log("ERROR Enrichment :::: >>> ", err);
            }

            if(result){
                console.log("RESULT ::: >>>>", result);
            }
        })
    }
});

当我尝试使用 setSecurity() 方法设置 Basic auth 和 Certs 时。它覆盖了我使用 setSecurity() 设置的第一件事。如果我没有通过其中任何一个,我就会收到未经授权的错误。

请帮助我提供解决方案。

4

1 回答 1

2

获得客户端证书和基本身份验证的一个好方法是实现您自己的node-soap安全协议您可以从现有的node-soap安全协议中获得灵感并将它们组合起来,或者编写一个足够通用的来链接两个(或更多)现有的安全协议。当然,使用解决方案创建拉取请求会更好,因此可以考虑将其直接包含在node-soap.

个人最终通过配置传递给 options构造函数/的附加值。https.Agent(...)BasicAuthSecurity

var https = require('https');

var options = {
  // https://nodejs.org/api/https.html#https_class_https_agent
  agent: new https.Agent({
    key: someKeyBuffer,
    cert: someCertBuffer,
    ca: [
      someCACertBuffer,
    ]
  });
}

client.setSecurity(new soap.BasicAuthSecurity('username', 'password', options));

这种方式也可用于将基本身份验证与.pfx.p12文件中使用的文件相结合ClientSSLSecurityPFX

于 2017-09-01T15:37:37.877 回答