2024-12-05 vue本地測(cè)試環(huán)境安全訪問(wèn)

vue.config.js 中寫:

module.exports = {
  publicPath,
  assetsDir,
  outputDir,
  lintOnSave: false,
  devServer: {
    hot: true,
    port: devPort,
    open: true,
    noInfo: false,
    overlay: {
      warnings: true,
      errors: true,
    },
    before: (app, server, compiler) => {
      // 這里可以添加自定義的 IP 白名單中間件
      app.use((req, res, next) => {
        // 獲取客戶端 IP 地址
        let clientIp = req.ip || req.connection.remoteAddress
        console.log('d', clientIp)
        // 檢查是否有代理設(shè)置
        const forwarded = req.headers['x-forwarded-for']
        if (forwarded) {
          const forwardedIps = forwarded.split(',')
          clientIp = forwardedIps[0]
        }

        // 定義允許訪問(wèn)的白名單 IP 地址
        const whitelist = ['127.0.0.1']

        // 判斷客戶端 IP 是否在白名單中
        if (whitelist.includes(clientIp) || clientIp.startsWith('172.18') || clientIp.startsWith('122.228.187')) {
          next()  // 如果在白名單中,繼續(xù)請(qǐng)求
        } else {
          res.status(403).send('Forbidden' + clientIp)  // 如果不在白名單中,拒絕訪問(wèn)
        }
      })
    }
  },
  productionSourceMap: false,
  pluginOptions: {
    "style-resources-loader": {
      preProcessor: "less",
      patterns: "./src/assets/css/color.less"
    }
  },
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容