以下是我们在 I/O 大会上宣布的所有内容,从新的 Firebase Studio 功能到集成 AI 的更多方式,内容非常丰富。
阅读博客。
開始使用 Firebase 安全性規則
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
Firebase Security Rules為 Cloud Firestore、Realtime Database 和 Cloud Storage 中的資料提供強大且完全可自訂的保護措施。請按照本指南中的步驟操作,輕鬆開始使用 Rules,確保資料安全無虞,並保護應用程式免於惡意使用者侵擾。
瞭解 Firebase Security Rules 種語言
開始編寫規則前,請花點時間查看所用 Firebase 產品的特定 Firebase Security Rules 語言。Realtime Database 採用類似 JavaScript 的語法和 JSON 結構。Rules或者,Cloud Firestore 和 Cloud Storage 會運用一般運算語言 (CEL) 的超集,並依據 match
和 allow
陳述式,在定義的路徑中設定存取條件。
進一步瞭解 Firebase Security Rules 語言。
設定「Authentication」
如果尚未完成這項操作,請找出具有 Firebase Authentication 的使用者。
Firebase Authentication 支援多種常見的驗證方法,並與 Firebase Security Rules 整合,提供完整的驗證功能。
您可以為應用程式設定額外的自訂驗證資訊。
進一步瞭解 Firebase Security Rules 和 Firebase Authentication。
定義資料和規則結構
資料結構可能會影響規則的結構和實作方式。定義資料結構時,請考量這些結構對 Rules 結構可能造成的影響。
舉例來說,在 Cloud Firestore 中,您可能想加入一個欄位,指出每位使用者的特定角色。然後,規則就能讀取該欄位,並用來授予角色型存取權。
定義資料和規則架構時,請注意,如果任何規則授予資料集存取權,Firebase Security Rules 就會授予該資料集存取權。換句話說,如果您已在資料階層的較高層級授予存取權,就無法在子路徑中調整存取權。
存取規則
如要查看現有的 Rules,請使用 Firebase CLI 或 Firebase 控制台。請務必使用相同方法編輯規則,以免不小心覆寫更新。如果不確定本機定義的規則是否反映最新更新,Firebase 控制台一律會顯示最近部署的 Firebase Security Rules 版本。
如要從 Firebase 控制台存取規則,請選取專案,然後前往 Realtime Database、Cloud Firestore 或「Storage」。進入正確的資料庫或儲存空間 bucket 後,按一下「規則」。
如要透過 Firebase CLI 存取規則,請前往firebase.json 檔案中註明的規則檔案。
撰寫基本規則
開發應用程式並瞭解 Rules 時,請嘗試實作幾項基本安全性規則,包括下列用途:
- 僅限內容擁有者:限制使用者存取內容。
- 混合存取權:限制使用者的寫入權限,但允許公開讀取。
- 屬性存取權:限制特定群組或類型使用者的存取權。
測試規則
如要完整驗證應用程式的行為並確認 Firebase Security Rules
設定,請使用 Firebase 模擬器在本機環境中執行及自動化單元測試。
如果您在 Firebase 控制台中設定 Firebase Security Rules,可以使用 Firebase 規則模擬器快速驗證行為。不過,建議您先使用 Firebase 模擬器進行更徹底的測試,再將變更部署至正式環境。
部署規則
使用 Firebase 控制台或 Firebase CLI 將規則部署至正式環境。請按照「管理及部署 Firebase Security Rules」一文中的步驟操作。
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-07-25 (世界標準時間)。
[null,null,["上次更新時間:2025-07-25 (世界標準時間)。"],[],[],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. Alternately, Cloud Firestore and Cloud Storage leverage 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\nLearn more about the [Firebase Security Rules language](/docs/rules/rules-language).\n\nSet up Authentication\n\nIf you haven't done it already, identify your users with [Firebase Authentication](/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 Firestore, you might want to include a field that denotes\na specific role for each user. Then, your rules can read that field and use it\nto grant role-based access.\n\nAs you define your data and rules architectures, keep in mind that, if *any*\nrule grants access to a dataset, Firebase Security Rules grants access to that dataset. In\nother words, you can't refine access at a subpath if you've granted access at\na higher level in your data hierarchy.\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 navigate to **Realtime Database** , **Cloud Firestore** or\n**Storage** . Click **Rules** once you're in the correct database or storage\nbucket.\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 a few [basic Security Rules](/docs/rules/basics), including the following\nuse cases:\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\nTo fully validate your app's behavior and verify your Firebase Security Rules \n\nconfigurations, use the [Firebase Emulator](/docs/rules/emulator-setup) to run and automate unit\ntests in a local environment.\n\nIf you're setting up your Firebase Security Rules in the Firebase console, you can use\nthe [Firebase Rules Simulator](/docs/rules/simulator) to quickly validate behavior. However, we\nrecommend more thorough testing with the Firebase Emulator 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)."]]