首先途样,我們需要一個(gè)私鑰捌归。
? https openssl genrsa -out privatekey.pem 1024
Generating RSA private key, 1024 bit long modulus
...........................++++++
..........................++++++
e is 65537 (0x10001)
之后用該私鑰生成一個(gè)證書亚再,按照提示 輸入國(guó)家縮寫等信息 需要注意的是 common Name需要填寫 認(rèn)證的域名鼠锈。
? https openssl req -new -key privatekey.pem -out certrequest.csr
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [AU]:CN
State or Province Name (full name) [Some-State]:beijing
Locality Name (eg, city) []:neijing
Organization Name (eg, company) [Internet Widgits Pty Ltd]:qunar
Organizational Unit Name (eg, section) []:qunar
Common Name (e.g. server FQDN or YOUR name) []:eva.li.qunar.com
Email Address []:xilixinluo@gmail.com
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:eva.li
An optional company name []:qunar
之后對(duì)私鑰和證書進(jìn)行自簽名的認(rèn)證
? https openssl x509 -req -in certrequest.csr -signkey privatekey.pem -out certificate.pem
Signature ok
subject=/C=CN/ST=beijing/L=neijing/O=qunar/OU=qunar/CN=eva.li.qunar.com/emailAddress=xilixinluo@gmail.com
Getting Private key
到此為止我們需要的三個(gè)基礎(chǔ)文件已經(jīng)準(zhǔn)備就緒闪檬,之后就是用https模塊起一個(gè)node服務(wù)。
var https = require('https')
,fs = require("fs");
var options = {
key: fs.readFileSync('./privatekey.pem'),
cert: fs.readFileSync('./certificate.pem'),
ca: fs.readFileSync('./certrequest.pem')
};
https.createServer(options, function(req,res){
res.writeHead(200, {
'Content-type' : 'text/html'
});
res.write('<h1>HTTPS server</h1>');
res.end('<p>Hello baby!</p>');
}).listen(8888);
這時(shí)我們起服務(wù)的時(shí)候Chrome
瀏覽器中顯示不安全提醒购笆,這是因?yàn)闉g覽器沒有ca
認(rèn)證的證書粗悯。這時(shí)需要我們手動(dòng)拷貝生成好的證書到系統(tǒng)中即可,如下圖所示同欠。
Paste_Image.png
Paste_Image.png
這樣再會(huì)刷新頁(yè)面样傍,頁(yè)面就已經(jīng)是驗(yàn)證通過的狀態(tài)了。
Paste_Image.png