Firebase 安全规则使用入门
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
Firebase Security Rules 让您能够以完全自定义的方式可靠地保护 Cloud Firestore、Realtime Database 和 Cloud Storage 中的数据。您可以按照本指南中的步骤轻松开始使用 Rules,确保数据安全,并保护您的应用免受恶意用户攻击。
了解 Firebase Security Rules 语言
在您开始编写规则之前,值得花一些时间查看您正在使用的 Firebase 产品的特定 Firebase Security Rules 语言。Cloud Storage 利用通用表达式语言 (CEL) 的超集,该语言依赖于 match
和 allow
语句设置访问所指定路径的条件。
首先学习 Firebase Security Rules 语言的核心语法。
设置 Authentication
请将 Firebase Authentication 添加到您的应用(如果您尚未添加)。Firebase Authentication 支持多种常见的身份验证方法,并且与 Firebase Security Rules 安全规则集成,从而提供全面的验证功能。
您可以为您的应用设置其他自定义身份验证信息。
详细了解 Firebase Security Rules 和 Firebase Authentication。
定义您的数据和规则结构
您构建数据的方式可能会影响您构建和实现规则的方式。在您定义数据结构时,请考虑它们对 Rules 结构可能产生的影响。
例如,在 Cloud Storage 中,您可能希望包含一个表示每个用户的特定角色的字段。然后,您的规则可以读取该字段并使用它来授予基于角色的访问权限。您还可以创建一个 Cloud Firestore 数据库,将访问条件存储在 Cloud Firestore 文档中,然后通过 Cloud Storage Security Rules 访问这些文档。
在定义数据和规则架构时,请记住规则是否级联取决于产品。使用 Realtime Database 时,规则自上而下发挥作用,浅层规则将替换深层规则。如果某个规则授予对特定路径的读取或写入权限,则也会授予对此路径下所有子节点的访问权限。相比之下,使用 Cloud Firestore 和 Cloud Storage 时,规则将仅在数据层次结构的指定级层应用,并且您可以编写明确的规则来控制对不同级层的访问权限。
访问您的规则
如需查看现有的 Rules,请使用 Firebase CLI 或 Firebase 控制台。确保始终使用同一方法修改您的规则,以避免错误地覆盖更新。如果您不能确定本地定义的规则是否反映了最近的更新,Firebase 控制台中会始终显示最近部署的 Firebase Security Rules 版本。
如需从 Firebase 控制台访问您的规则,请选择您的项目,然后点击左侧导航面板中的 Storage。在进入正确的数据库或存储桶之后,请点击 Rules。
如需从 Firebase CLI 访问您的规则,请打开 firebase.json 文件中记录的规则文件。
编写基本规则
在您开发应用和了解 Rules 时,请尝试实现 Rules 以应对一些基本使用场景,其中包括:
- 仅限内容所有者:限制用户对内容的访问。
- 混合访问:限制用户的写入权限,但允许公开读取。
- 基于属性的访问:仅限一组用户或一种类型的用户访问。
测试您的规则
如果您在 Firebase 控制台中设置 Firebase Security Rules,则可以使用 Firebase 规则测试平台快速验证行为。但是,在您将更改部署到生产环境之前,我们建议您使用 Local Emulator Suite 进行更全面的测试。
部署规则
使用 Firebase 控制台或 Firebase CLI 将您的规则部署到生产环境中。按照管理和部署 Firebase Security Rules 中介绍的步骤进行操作。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-12。
[null,null,["最后更新时间 (UTC):2025-08-12。"],[],[],null,["\u003cbr /\u003e\n\n\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.\nCloud Storage leverages a superset\nof the Common Expression Language (CEL) that relies on `match` and `allow`\nstatements that set a condition for access at a defined path..\n\nStart by learning the [core syntax of the Firebase Security Rules language](/docs/storage/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\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 Cloud Storage, 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. You can also create\na Cloud Firestore database, store access criteria in Cloud Firestore\ndocuments, then access those documents from Cloud Storage Security Rules.\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**Storage** . 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)."]]