在 React 应用程序中保护 Firebase API 密钥以使其不可公开访问的最佳方法是什么?
What is the best way to secure Firebase API keys in a react app so it is not accessible publicly?
我目前有一个使用 Firebase 身份验证并使用 Firestore 作为数据库的 React 应用程序。
我目前使用亚马逊的 Amplify 服务部署了该应用程序。我的 Firebase API 密钥,包括数据库 URL 和 firebase 应用程序 ID 都作为环境变量添加到 AWS 控制台中。
当我检查 public 包时,我可以看到所有 API 键都是 public。
我的问题是:
- 这听起来对吗?
- 是否有处理这种情况的最佳实践?
- 我需要构建后端来处理这个吗?
您的 API 密钥、数据库 URL、应用程序 ID 等应该是 public。
根据 firebase docs:
The content is considered public, including your platform-specific ID
(entered in the Firebase console setup workflow) and values that are
specific to your Firebase project, like your API Key, Realtime
Database URL, and Storage bucket name. Given this, use security rules
to protect your data and files in Realtime Database, Cloud Firestore,
and Cloud Storage.
使您与 firebase 的连接安全的方法是使用 security rules。您可以添加自定义规则以确保您的数据库安全。
例如:您可以确保只有userId 12345有权创建、读取、更新和删除userId12345的记录。
我目前有一个使用 Firebase 身份验证并使用 Firestore 作为数据库的 React 应用程序。
我目前使用亚马逊的 Amplify 服务部署了该应用程序。我的 Firebase API 密钥,包括数据库 URL 和 firebase 应用程序 ID 都作为环境变量添加到 AWS 控制台中。
当我检查 public 包时,我可以看到所有 API 键都是 public。
我的问题是:
- 这听起来对吗?
- 是否有处理这种情况的最佳实践?
- 我需要构建后端来处理这个吗?
您的 API 密钥、数据库 URL、应用程序 ID 等应该是 public。
根据 firebase docs:
The content is considered public, including your platform-specific ID (entered in the Firebase console setup workflow) and values that are specific to your Firebase project, like your API Key, Realtime Database URL, and Storage bucket name. Given this, use security rules to protect your data and files in Realtime Database, Cloud Firestore, and Cloud Storage.
使您与 firebase 的连接安全的方法是使用 security rules。您可以添加自定义规则以确保您的数据库安全。
例如:您可以确保只有userId 12345有权创建、读取、更新和删除userId12345的记录。