OpenSSL证书生成

OpenSSL是用于安全通信的著名开源密码学工具包,包括主要的密码算法、常见密码和证书封装功能。

确认本机是否已安装openssl

openssl version -a

地址: http://slproweb.com/products/Win32OpenSSL.html

安装:设置环境变量:例如工具安装在C:Program FilesOpenSSL-Win64,则将C:Program FilesOpenSSL-Win64in;复制到Path中

// 引入HPPTS
const https = require('https')
const fs = require('fs')
var privateKey = fs.readFileSync(path.join(__dirname, './cert/private.pem'), 'utf8')
var certificate = fs.readFileSync(path.join(__dirname, './cert/file.crt'), 'utf8')
var credentials = { key: privateKey, cert: certificate }
const httpsPort = process.env.httpsPort || config.dev.httpsPort

...
app.listen(port, function () {
    console.log(chalk.green(`> Preview at  http://localhost:${port}${publicPath}`))
    if (report) {
console.log(chalk.green(`> Report at  http://localhost:${port}${publicPath}report.html`))
    }
  })
  var httpsServer = https.createServer(credentials, app);
  httpsServer.listen(httpsPort, function() {
    console.log('HTTPS Server is running on: https://localhost:%s', httpsPort);
  });

生成本地证书

在build目录下新建一个目录cert。

  1. 生成私钥key文件(下面的pathway表示你要保存的文件路径位置)

    openssl genrsa 1024 > cert/private.pem 
  2. 通过上面生成的私钥文件生成CSR证书签名
    openssl req -new -key  cert/private.pem -out csr.pem
  3. 通过上述私钥文件和CSR证书签名生成证书文件
    openssl x509 -req -days 365 -in csr.pem -signkey cert/private.pem -out  cert/file.crt

 

 

 

 

 

 

原文地址:https://www.cnblogs.com/huoqin/p/13785038.html