首先,我們需要一個私鑰。
? https openssl genrsa -out privatekey.pem 1024
Generating RSA private key, 1024 bit long modulus
...........................++++++
..........................++++++
e is 65537 (0x10001)
之后用該私鑰生成一個證書,按照提示 輸入國家縮寫等信息 需要注意的是 common Name需要填寫 認證的域名。
? 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
之后對私鑰和證書進行自簽名的認證
? 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
到此為止我們需要的三個基礎文件已經準備就緒,之后就是用https模塊起一個node服務。
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);
這時我們起服務的時候Chrome
瀏覽器中顯示不安全提醒,這是因為瀏覽器沒有ca
認證的證書。這時需要我們手動拷貝生成好的證書到系統中即可,如下圖所示。
Paste_Image.png
Paste_Image.png
這樣再會刷新頁面,頁面就已經是驗證通過的狀態了。
Paste_Image.png