在 Node.js 中进行需要客户端证书进行身份验证的 REST 调用

2024-04-16

有没有办法进行需要客户端证书才能通过 Node.js 进行身份验证的休息调用?


是的,您可以非常简单地做到这一点,这里使用常规的 https 请求来完成;

var https = require('https'),                  // Module for https
    fs =    require('fs');                     // Required to read certs and keys

var options = {
    key:   fs.readFileSync('ssl/client.key'),  // Secret client key
    cert:  fs.readFileSync('ssl/client.crt'),  // Public client key
    // rejectUnauthorized: false,              // Used for self signed server
    host: "rest.localhost",                    // Server hostname
    port: 8443                                 // Server port
};

callback = function(response) {
  var str = '';    
  response.on('data', function (chunk) {
    str += chunk;
  });

  response.on('end', function () {
    console.log(str);
  });
}

https.request(options, callback).end();
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 Node.js 中进行需要客户端证书进行身份验证的 REST 调用 的相关文章

随机推荐