【发布时间】:2014-06-27 23:38:55
【问题描述】:
有没有办法进行需要客户端证书才能通过 Node.js 进行身份验证的休息调用?
【问题讨论】:
标签: node.js rest certificate
有没有办法进行需要客户端证书才能通过 Node.js 进行身份验证的休息调用?
【问题讨论】:
标签: node.js rest certificate
是的,您可以很简单地做到这一点,这里使用常规 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();
【讨论】:
http 替换为 https 并将上面显示的密钥和证书选项添加到 post_options 即可使用客户端证书。