以下是我们在 I/O 大会上宣布的所有内容,从新的 Firebase Studio 功能到集成 AI 的更多方式,内容非常丰富。
阅读博客。
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
Firebase Security Rules
plat_ios
plat_android
plat_web
plat_flutter
plat_node
ใช้กฎการรักษาความปลอดภัยของ Firebase ที่ยืดหยุ่นและมีความยืดหยุ่นเพื่อ
รักษาความปลอดภัยให้ข้อมูลของคุณใน Cloud Firestore, Firebase Realtime Database และ
Cloud Storage
Firebase Security Rules อยู่ระหว่างข้อมูลของคุณกับผู้ใช้ที่เป็นอันตราย คุณสามารถเขียนสั้นๆ หรือ
กฎที่ซับซ้อนซึ่งปกป้องข้อมูลของแอปได้ในระดับความละเอียดที่
กับแอปของคุณที่ต้องการ
การใช้ประโยชน์ Firebase Security Rules
ภาษาการกำหนดค่าที่ยืดหยุ่นและยืดหยุ่นเพื่อกำหนดข้อมูลที่ผู้ใช้ของคุณ
เข้าถึงได้สำหรับ Realtime Database, Cloud Firestore และ Cloud Storage
Firebase Realtime Database Security Rules ใช้ประโยชน์จาก JSON ในคำจำกัดความของกฎ
Cloud Firestore Security Rules และ Firebase Security Rules สำหรับ Cloud Storage ใช้ประโยชน์จาก
ภาษาที่สร้างขึ้นมาเพื่อรองรับโครงสร้างที่เจาะจงกฎที่ซับซ้อนมากขึ้น
ดูข้อมูลเพิ่มเติมเกี่ยวกับวิธีตั้งค่า Rules สำหรับผลิตภัณฑ์ Firebase ที่เฉพาะเจาะจง
ที่คุณใช้ในแอปและลักษณะการทำงานของ Rules ที่ต่างกันใน Firebase
Google อีกด้วย
เริ่มใช้งาน
ความสามารถที่สำคัญ
ความยืดหยุ่น |
เขียนกฎที่กำหนดเองที่เหมาะกับโครงสร้างและลักษณะการทำงานของแอป
Rules ใช้ภาษาที่ให้คุณใช้ประโยชน์จากข้อมูลของคุณเอง
เพื่อให้สิทธิ์เข้าถึง
|
แบบละเอียด |
คุณสามารถกำหนดกฎให้กว้างหรือแคบได้ตามต้องการ
|
การรักษาความปลอดภัยแบบอิสระ |
เนื่องจากมีการกำหนด Rules ไว้นอกแอป (ในคอนโซล Firebase หรือ
Firebase CLI) ไคลเอ็นต์
ไม่มีหน้าที่ในการบังคับใช้ความปลอดภัย ข้อบกพร่องจะไม่ทำให้เกิดความเสี่ยงต่อข้อมูล
ข้อมูลของคุณจะได้รับการปกป้องเสมอ
|
วิธีการทำงาน
Firebase Security Rules ทำงานโดยการจับคู่รูปแบบกับเส้นทางฐานข้อมูล แล้วจึงนำไปใช้
เงื่อนไขที่กำหนดเองเพื่ออนุญาตการเข้าถึงข้อมูลในเส้นทางเหล่านั้น Rulesทั้งหมด
ในผลิตภัณฑ์ต่างๆ ของ Firebase จะมีคอมโพเนนต์การจับคู่เส้นทางและ
ข้อความที่อนุญาตการเข้าถึงแบบอ่านหรือเขียนได้ คุณต้องกำหนด Rules สำหรับ
แต่ละผลิตภัณฑ์ Firebase ที่คุณใช้ในแอป
สำหรับ Cloud Firestore และ Cloud Storage Rules ให้ใช้รายการต่อไปนี้
ไวยากรณ์:
service <<name>> {
// Match the resource path.
match <<path>> {
// Allow the request if the following conditions are true.
allow <<methods>> : if <<condition>>
}
}
สำหรับ Realtime Database Rules แบบ JSON ใช้ไวยากรณ์ต่อไปนี้
{
"rules": {
"<<path>>": {
// Allow the request if the condition for each method is true.
".read": <<condition>>,
".write": <<condition>>
}
}
}
Rules จะนำมาใช้กับคำสั่ง OR
ไม่ใช่ AND
คำสั่ง
ดังนั้น ถ้ากฎหลายข้อตรงกับเส้นทางหนึ่ง และกฎใดๆ ที่ตรงกัน
เงื่อนไขให้สิทธิ์เข้าถึง Rules ให้สิทธิ์การเข้าถึงข้อมูล
เส้นทาง ดังนั้น หากกฎทั่วไปให้สิทธิ์เข้าถึงข้อมูล คุณจะไม่สามารถจำกัดด้วย
กฎที่เจาะจงมากขึ้น อย่างไรก็ตาม คุณสามารถหลีกเลี่ยงปัญหานี้ได้ด้วยการตรวจสอบว่า
Rules ไม่ทับซ้อนกันมากเกินไป การทับซ้อนกัน Firebase Security Rules รายการใน
ที่ตรงกันเป็นคำเตือนของคอมไพเลอร์
Firebase Security Rules ยังสามารถใช้ประโยชน์จาก Authentication เพื่อให้สิทธิ์ที่อิงตามผู้ใช้ และ
เงื่อนไขที่ตั้งไว้อาจเป็นเรื่องพื้นฐาน หรือซับซ้อนอย่างมาก ดูข้อมูลเพิ่มเติม
ประมาณ Rules ภาษาและพฤติกรรม
ก่อนที่จะเริ่มเขียน Rules
เส้นทางการใช้งาน
|
ผสานรวม SDK ของผลิตภัณฑ์ |
ตั้งค่า Cloud Firestore
Cloud Storage หรือ
Realtime Database สำหรับ
แอป |
|
เขียนFirebase Security Rulesของคุณ |
ดูข้อมูลเพิ่มเติมเกี่ยวกับ
วิธีการทำงานของ Rules และ
ตั้งค่า Rules พื้นฐาน
|
|
ทดสอบ Firebase Security Rules |
ใช้โปรแกรมจำลอง Realtime Database และ Cloud Firestore เพื่อทดสอบ
การทำงานและตรวจสอบกฎของคุณก่อนทำให้ใช้งานได้จริง |
|
ทำให้ Firebase Security Rules ใช้งานได้ |
ใช้คอนโซล Firebase หรือ CLI ของ Firebase เพื่อทำให้กฎใช้งานได้
สำหรับเวอร์ชันที่ใช้งานจริง |
ขั้นตอนถัดไป
เนื้อหาของหน้าเว็บนี้ได้รับอนุญาตภายใต้ใบอนุญาตที่ต้องระบุที่มาของครีเอทีฟคอมมอนส์ 4.0 และตัวอย่างโค้ดได้รับอนุญาตภายใต้ใบอนุญาต Apache 2.0 เว้นแต่จะระบุไว้เป็นอย่างอื่น โปรดดูรายละเอียดที่นโยบายเว็บไซต์ Google Developers Java เป็นเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-07-24 UTC
[null,null,["อัปเดตล่าสุด 2025-07-24 UTC"],[],[],null,["# Firebase Security Rules\n=======================\n\nplat_ios plat_android plat_web plat_flutter plat_node \nUse our flexible, extensible Firebase Security Rules to\nsecure your data in Cloud Firestore, Firebase Realtime Database, and\nCloud Storage.\n\nFirebase Security Rules stand between your data and malicious users. You can write simple or\ncomplex rules that protect your app's data to the level of granularity that\nyour specific app requires.\n\nFirebase Security Rules leverage\nextensible, flexible configuration languages to define what data your users\ncan access for Realtime Database, Cloud Firestore, and Cloud Storage.\nFirebase Realtime Database Security Rules leverage JSON in rule definitions, while\nCloud Firestore Security Rules and Firebase Security Rules for Cloud Storage leverage a unique\nlanguage built to accommodate more complex rules-specific structures.\n\nLearn more about how to set up Rules for the specific Firebase products\nyou use in your app, and how Rules behavior differs across Firebase\nproducts.\n\n[Get started](/docs/rules/get-started)\n\nKey capabilities\n----------------\n\n|----------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| Flexibility | Write custom rules that make sense for your app's structure and behavior. Rules use languages that allow you to leverage your own data to authorize access. |\n| Granularity | Your rules can be as broad or as narrow as you need. |\n| Independent security | Because Rules are defined outside of your app (in the Firebase console or Firebase CLI), clients aren't responsible for enforcing security, bugs don't compromise data, and your data is always protected. |\n\nHow do they work?\n-----------------\n\nFirebase Security Rules work by matching a pattern against database paths, and then applying\ncustom conditions to allow access to data at those paths. All Rules\nacross Firebase products have a path-matching component and a conditional\nstatement allowing read or write access. You must define Rules for\neach Firebase product you use in your app.\n\nFor Cloud Firestore and Cloud Storage, Rules use the following\nsyntax: \n\n service \u003c\u003cname\u003e\u003e {\n // Match the resource path.\n match \u003c\u003cpath\u003e\u003e {\n // Allow the request if the following conditions are true.\n allow \u003c\u003cmethods\u003e\u003e : if \u003c\u003ccondition\u003e\u003e\n }\n }\n\nFor Realtime Database, JSON-based Rules use the following syntax: \n\n {\n \"rules\": {\n \"\u003c\u003cpath\u003e\u003e\": {\n // Allow the request if the condition for each method is true.\n \".read\": \u003c\u003ccondition\u003e\u003e,\n \".write\": \u003c\u003ccondition\u003e\u003e\n }\n }\n }\n\nRules are applied as `OR` statements, not `AND` statements.\nConsequently, if multiple rules match a path, and any of the matched\nconditions grants access, Rules grant access to the data at that\npath. Therefore, if a broad rule grants access to data, you can't restrict with\na more specific rule. You can, however, avoid this problem by making sure your\nRules don't overlap too much. Firebase Security Rules flag overlaps in your\nmatched paths as compiler warnings.\n\nFirebase Security Rules can also leverage Authentication to grant user-based permissions, and the\nconditions you set can be very basic or incredibly complex. Learn more\nabout Rules [language](/docs/rules/rules-language) and [behavior](/docs/rules/rules-behavior)\nbefore you start writing Rules.\n\nImplementation path\n-------------------\n\n|---|-------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------|\n| | Integrate the product SDKs | Set up [Cloud Firestore](/docs/firestore), [Cloud Storage](/docs/storage), or [Realtime Database](/docs/database) for your app. |\n| | Write your Firebase Security Rules | Learn more about [how Rules work](/docs/rules/rules-behavior) and [set up some basic Rules](/docs/rules/basics) |\n| | Test your Firebase Security Rules | Use the Realtime Database and Cloud Firestore emulators to test your app's behavior and validate your rules before you deploy them to production. |\n| | Deploy your Firebase Security Rules | Use the Firebase console or the Firebase CLI to deploy your rules to production. |\n\nNext steps\n----------\n\n- [Understand the Firebase Security Rules language](/docs/rules/rules-language).\n- Learn more about [how Firebase Security Rules work](/docs/rules/rules-behavior).\n- Explore the [common mistakes you should avoid](/docs/rules/insecure-rules)."]]