HTTP
和HTTPS
的区别HTTPS
服务器使用HTTPS
协议,HTTP
服务器使用HTTP
协议.HTTPS
服务器须要向证书受权(Certificate Authority)
中心申请证书,通常免费证书何绍须要交费.在少量读客户端有要求的状况下,也会要求客户端使用证书.HTTP
服务器于客户端之间传输的是明文数据,而HTTPS
服务器于客户端之间传输的是通过SSL
安全加密后的密文数据.HTTP
服务器一般使用80
或8080
端口,HTTPS
服务器使用443
端口.HTTPS
服务器建立HTTPS
服务器之间,服务器端首先须要建立公钥,私钥及证书javascript
openssl
工具建立私钥openssl genrsa -out privatekey.pem 1024
(Certificate Signing Request)
文件openssl req -new -key privatekey.pem -out certreques.csr
openssl x509 -req -in certrequest.csr -signkey privatekey.pem -out certificate.pem
,x509
表明该证书如何国际电信联盟制定的数字证书标准在客户端与服务器端创建链接后,将首先确认证书的合法性,若是在服务器中使用学习或测试用证书,使用浏览器访问该服务器时,浏览器中将先显示一个警告信息,警告用户该证书不是一个通过证书受权中心签名的证书.
在具有了证书文件以后,可使用该证书文件建立一个pfx
文件,所谓的pfx
文件,是指该文件内容必须符合公钥加密技术12号标准(Public Key Cryptography Standards #12, PKCS#12)
为存储和传输用户或服务器私钥,公钥和证书而指定的格式.
+. 在openssl
工具中,能够建立pfx
文件openssl pkcs12 -export -in certificate.pem -inkey privatekey.pem -out certificate.pfx
在这些文件都具有了以后,可使用HTTPS
模块中的createServer
方法建立一个HTTPS
服务器HTTPS.createServer(options, [requestListener(request, response)])
html
HTTPS
服务器HTTPS.createServer(options)
options
为一个对象,使用的属性及属性值以下所示java
pfx
: 属性值为一个字符串或一个Buffer
对象,用于指定从pfx
文件读取出的私钥,公钥及证书,使用该属性值不须要指定key
属性值,cert
属性值以及ca
属性值.key
: 属性值为一个字符串或一个Buffer
对象,用于指定从后缀名为pem
的私钥文件中读取出来的私钥,该属性值为必须指定属性值,除非指定了pfx
属性值passphrase
: 属性值为一个自飞船,用于为私钥文件或pfx
文件指定密码cert
: 属性值为一个字符串或一个Buffer
对象,用于指定从后缀名为pem
的文件中读物出来的公钥,该属性值为必须指定属性值,除非指定了pfx
属性值ca
: 属性值为一个字符串或一个Buffer
对象数组,用于指定一组证书,默认属性值为几个著名的证书受权认证中心,好比VerlSign
crl
: 属性值为一个字符串或一个Buffer
对象数组,用于指定证书吊销列表ciphers
: 属性值为一个字符串值,用于描述须要使用或取消使用的密码.为了阻挡BEAST攻击,推荐奖ciphers
属性与honorCipherOrder
属性结合使用,以指定非CBC(Cipher-block chaining,密码分组连接)
模式的密码优先级,默认属性值为AES128-GCM-SHA256: RC4: HIGH: !MD5: !aNULL: !EDH
handshakeTimeout
: 属性值为一个整数,用于指定多少秒内若是没有完成客户端与服务器之间的握手,则放弃本次链接,默认属性值为120s
.当在指定时间内没有完成握手时,将处罚HTTPS
服务器的clientError
事件.honorCipherOrder
: 属性值为一个布尔值,当属性值指定为true
时,服务器将密码列表发送给客户端,有客户端选择密码,尽管该属性值默认为false,可是仍推荐奖该属性值设置为true
,以阻止BEAST
攻击requestCert
: 属性值为一个布尔值,当属性值指定为true
时,服务器在确认链接时要求客户端提供证书,默认属性值为false
rejectUnauthorized
:属性值为一个布尔值,若是属性值为true
,那么服务器拒绝任何不能提供服务器端所要求的证书的客户端.只有当requestCert
属性值指定为true
时,该属性值才有效,默认属性值为false
NPNProtocols
: 属性值为一个数组或一个Buffer
对象,用于指定服务器端所需使用的NPN
协议(这些协议应该按照其优先级排序).NPN(Next Protocol Negotiation)
协议是一种用于指定服务器可使用多种协议(包括HTTP,SPDY协议等)
的协议sessionIdContext
: 属性值指定为true,那么默认属性值为一个MD5散列值,若是requestCert
属性值指定为false
,不提供默认属性值https
服务器openssl genrsa 1024 > /root/https/private.pem
openssl req -new -key /root/https/private.pem -out csr.pem
openssl x509 -req -days 365 -in csr.pem -signkey /root/https/private.pem -out /root/https/file.crt
app.js
文件代码let app = require('express')(); const fs = require('fs'); const http = require('http'); const https = require('https'); const privateKey = fs.readFileSync('/root/https/private.pem', 'utf8'); const certificate = fs.readFileSync('/root/https/file.crt', 'utf8'); const credentials = {key: privateKey, cert: certificate}; const httpServer = http.createServer(app); const httpsServer = https.createServer(credentials, app); const PORT = 18080; const SSLPORT = 18081; httpServer.listen(PORT, function() { console.log('HTTP Server is running on: http://localhost:%s', PORT); }); httpsServer.listen(SSLPORT, function() { console.log('HTTPS Server is running on: https://localhost:%s', SSLPORT); }); // Welcome app.get('/', function(req, res) { if(req.protocol === 'https') { res.status(200).send('Welcome to Safety Land!'); } else { res.status(200).send('Welcome!'); } });
HTTPS
向其余网站请求数据HTTPS
模块中,可使用request
方法向其余使用HTTPS
协议的网站请求数据let req = https.request(options, callback(res){})
git
options
为一个对象或字符串,用于指定请求的目标的URL
地址,若是该参数值为一个字符串,将自动使用URL
模块中的parse
方法转换为一个对象.在options
参数值对象或使用parse
方法转换后对象中,能够指定的属性及属性值以下所示github
host
: 用于指定域名或目标主机的IP
地址,默认属性为localhost
hostname
: 用于指定域名或目标主机的IP
地址,默认属性为localhost
, 若是hostname
属性值与host
属性值都被指定,优先使用hostname属性值port
: 指定目标服务器用于HTTP
客户单链接的端口号,默认为443
method
: 用于指定HTTP
请求方式,默认为GET
path
: 用于指定请求路径及查询字符串,默认为/
headers
: 用于指定客户端请求头对象auth
: 用于指定认证信息部分,例如user:password
当在options
参数值对象中使用以下所示的属性及属性值时,不能使用全局https.Agent
对象chrome
pfx
: 属性值为一个字符串或一个Buffer
对象,用于指定从pfx
文件读取出的私钥,公钥及证书,使用该属性值不须要指定key
属性值,cert
属性值以及ca
属性值.key
: 属性值为一个字符串或一个Buffer
对象,用于指定从后缀名为pem
的私钥文件中读取出来的私钥,该属性值为必须指定属性值,除非指定了pfx属性值passphrase
: 属性值为一个字符串,用于为私钥文件或pfx
文件指定密码cert
: 属性值为一个字符串或一个Buffer
对象,用于指定从后缀名为pem
的文件中读物出来的公钥,该属性值为必须指定属性值,除非指定了pfx属性值ca
: 属性值为一个字符串或一个Buffer
对象数组,用于指定一组证书,默认属性值为几个著名的证书受权认证中心,好比VerlSign
crl
: 属性值为一个字符串或一个Buffer
对象数组,用于指定证书吊销列表ciphers
: 属性值为一个字符串值,用于描述须要使用或取消使用的密码.为了阻挡BEAST
攻击,推荐奖ciphers
属性与honorCipherOrder
属性结合使用,以指定非CBC(Cipher-block chaining,密码分组连接)
模式的密码优先级,默认属性值为AES128-GCM-SHA256: RC4: HIGH: !MD5: !aNULL: !EDH
rejectUnauthorized
:属性值为一个布尔值,若是属性值为true
,那么服务器在客户端创建链接后,返回响应前首先验证客户端提交的证书,若是验证失败,触发客户端请求对象的error
事件.HTTPS
客户端const https = require('https'); let options = { hostname: 'github.com', port: 443, path: '/', method: 'GET', agent: false } let req = https.get(options, (res) => { console.log('状态码:' + res.statusCode); console.log('响应头:' + JSON.stringify(res.headers)); res.setEncoding('utf8'); res.on('data', (chunk) => { console.log('响应内容:' + chunk); }); }); req.setTimeout(1000, (res) => { res.abort(); }); req.on('error', (err) => { if (err.code === 'ECONNRESET') { console.log('socket端口超时'); } else { console.log('在请求数据过程当中发生错误,错误代码为:' + err.code) } }); /** * 状态码:200 * 响应头:{ "date": "Sun, 25 Feb 2018 14:24:24 GMT", "content-type": "text/html; charset=utf-8", "transfer-encoding": "chunked", "connection": "close", "server": "GitHub.com", "status": "200 OK", "cache-control": "no-cache", "vary": "X-PJAX, Accept-Encoding", "x-ua-compatible": "IE=Edge,chrome=1", "set-cookie": ["logged_in=no; domain=.github.com; path=/; expires=Thu, 25 Feb 2038 14:24:24 -0000; secure; HttpOnly", "_gh_sess=eyJzZXNzaW9uX2lkIjoiMDY1YjM2ZmU4ZGM5MTFlZTliNjllMDI5ZDg0YzQ0ODUiLCJsYXN0X3JlYWRfZnJvbV9yZXBsaWNhcyI6MTUxOTU2ODY2NDUzMywiX2NzcmZfdG9rZW4iOiJ1ck5qWmZTMkpkeGpxMlN2ZzJhbklJM2pvaTJMVWEzWHcvSXEvTGtzVzBrPSJ9--8b5c0c203b3767a35f046b5ea4e375715e4d95be; path=/; secure; HttpOnly"], "x-request-id": "1faefb3e691e8adc7e1a7d727c07236f", "x-runtime": "0.050658", "expect-ct": "max-age=2592000, report-uri=\"https://api.github.com/_private/browser/errors\"", "content-security-policy": "default-src 'none'; base-uri 'self'; block-all-mixed-content; child-src render.githubusercontent.com; connect-src 'self' uploads.github.com status.github.com collector.githubapp.com api.github.com www.google-analytics.com github-cloud.s3.amazonaws.com github-production-repository-file-5c1aeb.s3.amazonaws.com github-production-upload-manifest-file-7fdce7.s3.amazonaws.com github-production-user-asset-6210df.s3.amazonaws.com wss://live.github.com; font-src assets-cdn.github.com; form-action 'self' github.com gist.github.com; frame-ancestors 'none'; img-src 'self' data: assets-cdn.github.com identicons.github.com collector.githubapp.com github-cloud.s3.amazonaws.com *.githubusercontent.com; manifest-src 'self'; media-src 'none'; script-src assets-cdn.github.com; style-src 'unsafe-inline' assets-cdn.github.com; worker-src 'self'", "strict-transport-security": "max-age=31536000; includeSubdomains; preload", "x-content-type-options": "nosniff", "x-frame-options": "deny", "x-xss-protection": "1; mode=block", "x-runtime-rack": "0.058378", "x-github-request-id": "1B82:60FF:117094:18A2C0:5A92C717" } * ****/