首页 > 解决方案 > 如何将我的私有 api 密钥存储在 Vue.js 应用程序上?

问题描述

我阅读了有关 Vue CLI 的文档,我想确定我是否理解正确。

文件说,

Note that only NODE_ENV, BASE_URL, and variables that start with VUE_APP_ will be statically embedded into the client bundle with webpack.DefinePlugin. It is to avoid accidentally exposing a private key on the machine that could have the same name.

那么这是否意味着我应该在没有前缀 VUE_APP 的情况下存储我的私钥?例如,

VUE_APP_NOT_SECRET_KEY=not_secret_key
SECRET_KEY=secret_key

存储我的密钥的正确方法是什么?

标签: vue.jsenvironment-variablesvue-clidotenv

解决方案


您可以将密钥存储在 VUE_APP_NOT_SECRET_KEY=not_secret_keySECRET_KEY=secret_key中,从安全的角度来看没有区别,任何具有最低前端技能的人都可以从浏览器中读取此值。

唯一的区别是,如果您使用VUE_APP_*前缀,您的变量将位于 process.env 中,并且将在编译时使用 DefinePluing 被 Vue 替换。

cli.vuejs.org/guide/mode-and-env.html#environment-variables –</p>

注意:您存储在 Vue 应用程序中的任何内容都不是秘密。


推荐阅读