Firebase 安全规则使用入门
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
Firebase 安全规则让您能够以完全自定义的方式可靠地保护 Cloud Firestore、Realtime Database 和 Cloud Storage 中的数据。您可以按照本指南中的步骤轻松开始使用Rules,确保数据安全,并保护您的应用免受恶意用户攻击。
了解 Firebase 安全规则语言
在您开始编写规则之前,值得花一些时间查看您正在使用的 Firebase 产品的特定 Firebase 安全规则语言。
Realtime Database 在其Rules中使用类似 JavaScript 的语法和 JSON 结构。
首先学习 Firebase 安全规则语言的核心语法。
设置 Authentication
如果您尚未将 Firebase Authentication 添加到您的应用,请现在添加。Firebase Authentication 支持多种常见的身份验证方法,并且与 Firebase 安全规则集成,从而提供全面的验证功能。
您可以为您的应用设置其他自定义身份验证信息。
详细了解 Firebase 安全规则和 Firebase Authentication。
定义您的数据和规则结构
定义要进行安全保护的数据
您构建数据的方式可能会影响您构建和实现规则的方式。在您定义数据结构时,请考虑它们对 Rules 结构可能产生的影响。
例如,在 Realtime Database 中,您可能希望包含一个表示每个用户的特定角色的字段。然后,您的规则可以读取该字段并使用它来授予基于角色的访问权限。
在定义数据和规则架构时,请记住规则是否级联取决于产品。使用 Realtime Database 时,规则自上而下发挥作用,浅层规则将替换深层规则。如果某个规则授予对特定路径的读取或写入权限,则也会授予对此路径下所有子节点的访问权限。相比之下,使用 Cloud Firestore 和 Cloud Storage 时,规则将仅在数据层次结构的指定级层应用,并且您可以编写明确的规则来控制对不同级层的访问权限。
访问您的规则
如需查看现有的 Rules,请使用 Firebase CLI 或 Firebase 控制台。确保始终使用同一方法修改您的规则,以避免错误地覆盖更新。如果您不能确定本地定义的规则是否反映了最新的更新,Firebase 控制台中会始终显示最近部署的 Firebase 安全规则版本。
如需从 Firebase 控制台访问您的规则,请选择您的项目,然后点击左侧导航面板中的 Realtime Database。在进入正确的数据库或存储桶之后,请点击 Rules。
如需从 Firebase CLI 访问您的规则,请打开 firebase.json 文件中记录的规则文件。
编写基本规则
在您开发应用和了解 Rules 时,请尝试实现 Rules 以应对一些基本使用场景,其中包括:
- 仅限内容所有者:限制用户对内容的访问。
- 混合访问:限制用户的写入权限,但允许公开读取。
- 基于属性的访问:仅限一组用户或一种类型的用户访问。
测试您的规则
如果您在 Firebase 控制台中设置 Firebase 安全规则,则可以使用 Firebase 规则测试平台快速验证行为。但是,在您将更改部署到生产环境之前,我们建议您使用 Local Emulator Suite 进行更全面的测试。
部署规则
使用 Firebase 控制台或 Firebase CLI 将您的规则部署到生产环境中。按照管理和部署 Firebase 安全规则中介绍的步骤进行操作。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-13。
[null,null,["最后更新时间 (UTC):2025-08-13。"],[],[],null,["\u003cbr /\u003e\n\nFirebase Security Rules provide robust, completely customizable protection for your data in\nCloud Firestore, Realtime Database, and Cloud Storage. You can easily get\nstarted with Rules following the steps in this guide, securing your\ndata and protecting your app from malicious users.\n\nUnderstand the Firebase Security Rules language\n\nBefore you start writing rules, it's worthwhile to take some time to review\nthe specific Firebase Security Rules language for the Firebase products you're using.\nRealtime Database leverages a JavaScript-like syntax and JSON structure for its\nRules.\n\nStart by learning the [core syntax of the Firebase Security Rules language](/docs/database/security/core-syntax).\n\nSet up Authentication\n\nIf you haven't done it already, add [Firebase Authentication to your app](/docs/auth).\nFirebase Authentication supports many common authentication methods and integrates with\nFirebase Security Rules to provide comprehensive verification capabilities.\n\nYou can set up additional, custom authentication information for your app.\n\nLearn more about [Firebase Security Rules and Firebase Authentication](/docs/rules/rules-and-auth).\n\nDefine your data and rules structures\n\nDefine data for security\n\nThe way you structure your data might affect the way you structure and\nimplement your rules. As you define your data structures, consider the\nimplications they might have on your Rules structure.\n\nFor example, in Realtime Database, you might want to include\na field that denotes a specific role for each user. Then, your rules can read\nthat field and use it to grant role-based access.\n\nAs you define your data and rules architectures, keep in mind the way that\nrules cascade or don't cascade, depending on your product. With Realtime Database,\nrules work from top-down, with shallower rules overriding deeper rules. If a\nrule grants read or write permissions at a particular path, then it also grants\naccess to all child nodes under it. In contrast, with Cloud Firestore and\nCloud Storage, rules apply only at specified levels of the data hierarchy,\nand you write explicit rules to control access to different levels.\n\nAccess your rules\n\nTo view your existing Rules, use either the Firebase CLI or the\nFirebase console. Make sure you edit your rules using the same method,\nconsistently, to avoid mistakenly overwriting updates. If you're not sure\nwhether your locally defined rules reflect the most recent updates, the Firebase\nconsole always shows the most recently deployed version of your Firebase Security Rules.\n\nTo access your rules from the [Firebase console](//console.firebase.google.com/), select your\nproject, then in the left-hand navigation panel, click\n**Realtime Database** . Click **Rules** once you're\nin the correct database or storage bucket.\n\nTo access your rules from the Firebase CLI, go to the\nrules file noted in your [firebase.json file](/docs/cli#the_firebasejson_file).\n\nWrite basic rules\n\nAs you're developing your app and understanding Rules, try\nimplementing Rules to address a few [basic use cases](/docs/rules/basics),\nincluding the following:\n\n- **Content-owner only:** Restrict access to content by user.\n- **Mixed access:** Restrict write access by user, but allow public read access.\n- **Attribute-based access:** Restrict access to a group or type of user.\n\nTest your rules\n\nIf you're setting up your Firebase Security Rules in the Firebase console, you can use\nthe [Firebase Rules Playground](/docs/rules/simulator) to quickly validate\nbehavior. However, we recommend more thorough testing with the\n[Local Emulator Suite](/docs/rules/emulator-setup) before you deploy your\nchanges to production.\n\nDeploy rules\n\nUse the Firebase console or the Firebase CLI to deploy your rules\nto production. Follow the steps outlined in\n[Manage and deploy Firebase Security Rules](/docs/rules/manage-deploy)."]]