Nodejs使用TLS

來源:互聯網
上載者:User

標籤:nodejs   tls   

1. 使用openssl產生伺服器和用戶端認證

產生伺服器憑證,伺服器使用自我簽署憑證(也就是自己扮演CA)

openssl genrsa -out server-key.pem 2048openssl req -new -sha256 -key server-key.pem -out server-csr.pem    //在CN處填寫伺服器主機名稱www.qikangwei.comopenssl x509 -req -in server-csr.pem -signkey server-key.pem -out server-cert.pem

將伺服器私密金鑰server-key.pem和CA根憑證server-cert.pem複製到用戶端,然後產生用戶端認證

openssl genrsa -out client-key.pem 2048openssl req -new -sha256 -key client-key.pem -out client-csr.pem    //在CN出填寫用戶端主機名稱openssl x509 -req -CA server-cert.pem -CAkey server-key.pem -CAcreateserial -in client-csr.pem -out client-cert.pem

2. 建立伺服器和用戶端指令碼

伺服器:

vi tls-server.jsvar tls = require(‘tls‘);var fs = require(‘fs‘);var options = {        key: fs.readFileSync(‘server-key.pem‘),        cert: fs.readFileSync(‘server-cert.pem‘),        ca: [ fs.readFileSync(‘server-cert.pem‘) ],        requestCert: true,        rejectUnauthorized: true};var server = tls.createServer(options, function(test) {        console.log(‘server connected‘, test.authorized ? ‘authorized‘ : ‘unauthorized‘);        test.write("welcome!\n");        test.setEncoding(‘utf8‘);        test.on(‘data‘, function(data) {                console.log(data);        });        test.on(‘close‘, function() {                console.log(‘client has closed‘);                server.close();        });});server.listen(2345, function() {        console.log(‘server bound‘);});

用戶端:

vi tls-client.jsvar tls = require(‘tls‘);var fs = require(‘fs‘);var options = {        host: ‘www.qikangwei.com‘,        port: 2345,        key: fs.readFileSync(‘client-key.pem‘),        cert: fs.readFileSync(‘client-cert.pem‘),        ca: [ fs.readFileSync(‘server-cert.pem‘) ],        rejectUnauthorized: true};var client = tls.connect(options, function() {        console.log(‘client connected‘, client.authorized ? ‘authorized‘ : ‘unauthorized‘);        process.stdin.setEncoding(‘utf8‘);        process.stdin.on(‘readable‘, function() {                var chunk = process.stdin.read();                if (chunk !== null) {                        client.write(chunk);                }        });});client.setEncoding(‘utf8‘);client.on(‘data‘, function(data) {        console.log(data);});client.write("happy new year!");

3. 測試

伺服器:

node tls-server.js

用戶端:

node tls-client.js

指令碼啟動後,在用戶端輸入內容,伺服器端會顯示同樣的內容

本文出自 “我的青春我做主” 部落格,請務必保留此出處http://qikangwei.blog.51cto.com/2282397/1613013

Nodejs使用TLS

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.