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