天天看點

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