溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

怎么使用openSSL構(gòu)造一個(gè)支持https的nodejs服務(wù)器

發(fā)布時(shí)間:2021-11-06 15:21:30 來(lái)源:億速云 閱讀:190 作者:iii 欄目:web開發(fā)

本篇內(nèi)容主要講解“怎么使用openSSL構(gòu)造一個(gè)支持https的nodejs服務(wù)器”,感興趣的朋友不妨來(lái)看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來(lái)帶大家學(xué)習(xí)“怎么使用openSSL構(gòu)造一個(gè)支持https的nodejs服務(wù)器”吧!

首先通過(guò)下面的鏈接下載openSSL
https://slproweb.com/products/Win32OpenSSL.html

怎么使用openSSL構(gòu)造一個(gè)支持https的nodejs服務(wù)器

怎么使用openSSL構(gòu)造一個(gè)支持https的nodejs服務(wù)器

下載完畢后,執(zhí)行openssl進(jìn)入交互式界面:

怎么使用openSSL構(gòu)造一個(gè)支持https的nodejs服務(wù)器

使用命令生成privatekey.pem 1024意思是1024位長(zhǎng)度。

openssl genrsa -out privatekey.pem 1024

怎么使用openSSL構(gòu)造一個(gè)支持https的nodejs服務(wù)器

生成的privatekey.pem,打開看一看長(zhǎng)啥樣:

怎么使用openSSL構(gòu)造一個(gè)支持https的nodejs服務(wù)器

怎么使用openSSL構(gòu)造一個(gè)支持https的nodejs服務(wù)器

什么是pem文件?

.pem - Defined in RFCs 1421 through 1424, this is a container format that may include just the public certificate (such as with Apache installs, and CA certificate files /etc/ssl/certs), or may include an entire certificate chain including public key, private key, and root certificates. Confusingly, it may also encode a CSR (e.g. as used here) as the PKCS10 format can be translated into PEM. The name is from Privacy Enhanced Mail (PEM), a failed method for secure email but the container format it used lives on, and is a base64 translation of the x509 ASN.1 keys.

簡(jiǎn)單的說(shuō),就是一個(gè)密鑰文件。

第二步,基于第一步生成的密鑰文件生成一個(gè)證書請(qǐng)求:
openssl req -new -key privatekey.pem -out certrequest.csr

怎么使用openSSL構(gòu)造一個(gè)支持https的nodejs服務(wù)器

如果懶得維護(hù)證書明細(xì),直接敲回車,會(huì)自動(dòng)填入默認(rèn)值:

怎么使用openSSL構(gòu)造一個(gè)支持https的nodejs服務(wù)器

怎么使用openSSL構(gòu)造一個(gè)支持https的nodejs服務(wù)器

怎么使用openSSL構(gòu)造一個(gè)支持https的nodejs服務(wù)器

最后基于第一步生成的密鑰和證書請(qǐng)求生成一個(gè)數(shù)字證書:當(dāng)然頒發(fā)機(jī)構(gòu)就是自己了,僅用于測(cè)試目的。
openssl x509 -req -in certrequest.csr -signkey privatekey.pem -out certificate.pem

怎么使用openSSL構(gòu)造一個(gè)支持https的nodejs服務(wù)器

怎么使用openSSL構(gòu)造一個(gè)支持https的nodejs服務(wù)器

至此我們有了privatekey.pem和Certificate.pem兩個(gè)證書了。

怎么使用openSSL構(gòu)造一個(gè)支持https的nodejs服務(wù)器

下面是我https服務(wù)器的代碼,很簡(jiǎn)單,只有50幾行:

var app = require('express')();var fs    = require('fs');var https = require('https');var httpOptions =  { key: fs.readFileSync("keys/privatekey.pem"), cert: fs.readFileSync("keys/certificate.pem")
}var server = https.createServer(httpOptions, app);var io = require('socket.io')(server);console.log("https server listens on port 8080...");
server.listen(8080);function print_env(){  console.log(process.env);
}
app.get('/', function (req, res) {  var response = "Hello World";
  res.send(response);
});
app.get('/env', function (req, res) {
  print_env();  // res.sendFile(__dirname + '/index.html');
  var response = JSON.stringify(process.env);
  res.send(response);
});
app.get('/redis', function (req, res) {  var redisClient = require("./redisClient");  
  function callback(response){    // var response = "ok";//JSON.stringify(process.env);
    res.send(response);
  }
  redisClient.test(callback);
});
io.on('connection', function (socket) {  console.log("connect comming from client: " + socket.id);
  socket.emit('messages_jerry', { hello: 'world greeting from Server!' });
  socket.on('messages', function (data) {    console.log("data received from Client:" + JSON.stringify(data,2,2));
  });
});

從代碼里不難理解這兩個(gè)pem文件是如何用在https服務(wù)器里的。
最后在瀏覽器里測(cè)試。因?yàn)槭亲约侯C發(fā)的證書,沒(méi)有經(jīng)過(guò)CA驗(yàn)證,所以瀏覽器會(huì)顯示一個(gè)警告。

怎么使用openSSL構(gòu)造一個(gè)支持https的nodejs服務(wù)器

到此,相信大家對(duì)“怎么使用openSSL構(gòu)造一個(gè)支持https的nodejs服務(wù)器”有了更深的了解,不妨來(lái)實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI