天天看点

vue本地开启https访问模式

vue本地开启https访问模式

简要说明:数字证书是一种用于电脑的身份识别机制。由数字证书颁发机构(CA)对使用私钥创建的签名请求文件做的签名(盖章),表示 CA 结构对证书持有者的认可。数字证书拥有以下几个优点:

① 使用数字证书能够提高用户的可信度

② 数字证书中的公钥,能够与服务端的私钥配对使用,实现数据传输过程中的加密和解密

③ 在证认使用者身份期间,使用者的敏感个人数据并不会被传输至证书持有者的网络系统上

X.509 证书包含三个文件:key,csr,crt。

① key 是服务器上的私钥文件,用于对发送给客户端数据的加密,以及对从客户端接收到数据的解密

② csr 是证书签名请求文件,用于提交给证书颁发机构(CA)对证书签名

③ crt 是由证书颁发机构(CA)签名后的证书,或者是开发者自签名的证书,包含证书持有人的信息,持有人的公钥,以及签署者的签名等信息

在密码学中,X.509 是一个标准,规范了公开秘钥认证、证书吊销列表、授权凭证、凭证路径验证算法等。

浏览器检查一个证书是否仍然有效有两种方法: OCSP (Online Certificate Status Protocol,在线证书状态协议) 和 CRL(Certificate Revoke List,证书吊销列表)。

一、生成本地证书

检查是否安装openssl

openssl version -a

1.在buid文件夹下新建 cert 文件夹,在cert目录下打开git bash输入以下命令生成私钥 .key 文件

openssl genrsa -out private.key 1024

2.通过上面生成的私钥文件生成CSR 证书签名,根据要求填写一些相关信息,可一路按回车即可

openssl req -new -key private.key -out csr.key

3.根据上述私钥文件和csr证书签名文件生成证书文件

openssl x509 -req -days 3650 -in csr.key -signkey private.key -out file.crt

cert目录下分别生成 private.key、csr.key、file.crt 三个文件。

二、config/index.js

module.exports = {

dev: {
    host: 'yw100-fat.yoowang.com',
    port: 443, // https 协议专用端口,注意不要写80
}           

}

三、webpack.dev.conf.js添加代码

'use strict'
const utils = require('./utils')
const webpack = require('webpack')
const config = require('../config')
const merge = require('webpack-merge')
const path = require('path')
const baseWebpackConfig = require('./webpack.base.conf')
const CopyWebpackPlugin = require('copy-webpack-plugin')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const FriendlyErrorsPlugin = require('friendly-errors-webpack-plugin')
const portfinder = require('portfinder')

// https 服务开启
// const https = require('https')
// const fs = require('fs')

const HOST = process.env.HOST
const PORT = process.env.PORT && Number(process.env.PORT)

const devWebpackConfig = merge(baseWebpackConfig, {
  module: {
    rules: utils.styleLoaders({ sourceMap: config.dev.cssSourceMap, usePostCSS: true })
  },
  // cheap-module-eval-source-map is faster for development
  devtool: config.dev.devtool,

  // these devServer options should be customized in /config/index.js
  devServer: {
    clientLogLevel: 'warning',
    historyApiFallback: {
      rewrites: [
        { from: /.*/, to: path.posix.join(config.dev.assetsPublicPath, 'index.html') },
      ],
    },
    // https 服务开启
    https: true,
    hot: true,
    contentBase: false, // since we use CopyWebpackPlugin.
    compress: true,
    host: HOST || config.dev.host,
    port: PORT || config.dev.port,
    open: config.dev.autoOpenBrowser,
    overlay: config.dev.errorOverlay
      ? { warnings: false, errors: true }
      : false,
    publicPath: config.dev.assetsPublicPath,
    proxy: config.dev.proxyTable,
    quiet: true, // necessary for FriendlyErrorsPlugin
    watchOptions: {
      poll: config.dev.poll,
    },
    disableHostCheck: true,
    // https 服务开启
    // https: {
    //   key: fs.readFileSync(path.join(__dirname, './cert/private.key')),
    //   cert: fs.readFileSync(path.join(__dirname, './cert/file.crt')),
    //   ca: fs.readFileSync(path.join(__dirname, './cert/file.crt'))
    // }
  },
  plugins: [
    new webpack.DefinePlugin({
      'process.env': require('../config/dev.env')
    }),
    new webpack.HotModuleReplacementPlugin(),
    new webpack.NamedModulesPlugin(), // HMR shows correct file names in console on update.
    new webpack.NoEmitOnErrorsPlugin(),
    // https://github.com/ampedandwired/html-webpack-plugin
    new HtmlWebpackPlugin({
      filename: 'index.html',
      template: 'index.html',
      inject: true
    }),
    // copy custom static assets
    new CopyWebpackPlugin([
      {
        from: path.resolve(__dirname, '../static'),
        to: config.dev.assetsSubDirectory,
        ignore: ['.*']
      }
    ])
  ]
})

module.exports = new Promise((resolve, reject) => {
  portfinder.basePort = process.env.PORT || config.dev.port
  portfinder.getPort((err, port) => {
    if (err) {
      reject(err)
    } else {
      // publish the new Port, necessary for e2e tests
      process.env.PORT = port
      // add port to devServer config
      devWebpackConfig.devServer.port = port

      // Add FriendlyErrorsPlugin
      devWebpackConfig.plugins.push(new FriendlyErrorsPlugin({
        compilationSuccessInfo: {
          messages: [`Your application is running here: http://${devWebpackConfig.devServer.host}:${port}`],
        },
        onErrors: config.dev.notifyOnErrors
        ? utils.createNotifierCallback()
        : undefined
      }))

      resolve(devWebpackConfig)
    }
  })
})           

运行 npm run dev