Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
Firebase Security Rules
plat_iosplat_androidplat_webplat_flutterplat_node
Sử dụng các Quy tắc bảo mật Firebase linh hoạt và có thể mở rộng để
bảo mật dữ liệu của bạn trong Cloud Firestore, Firebase Realtime Database và
Cloud Storage.
Firebase Security Rules đứng giữa dữ liệu của bạn và người dùng độc hại. Bạn có thể viết mã đơn giản hoặc
để bảo vệ dữ liệu ứng dụng của bạn đến mức độ chi tiết mà
mà ứng dụng cụ thể của bạn yêu cầu.
Đòn bẩy Firebase Security Rules
ngôn ngữ cấu hình linh hoạt, dễ mở rộng để xác định loại dữ liệu mà người dùng của bạn
có thể truy cập cho Realtime Database, Cloud Firestore và Cloud Storage.
Firebase Realtime Database Security Rules tận dụng JSON trong các định nghĩa quy tắc, trong khi
Cloud Firestore Security Rules và Firebase Security Rules cho Cloud Storage tận dụng một giá trị duy nhất
được thiết kế để phù hợp với các cấu trúc phức tạp hơn liên quan đến các quy tắc cụ thể.
Tìm hiểu thêm về cách thiết lập Rules cho các sản phẩm cụ thể trong Firebase
bạn sử dụng trong ứng dụng của mình và hành vi của Rules khác nhau như thế nào trên Firebase
của Google dành cho doanh nghiệp.
Viết các quy tắc tuỳ chỉnh phù hợp với cấu trúc và hành vi của ứng dụng.
Rules sử dụng các ngôn ngữ giúp bạn tận dụng dữ liệu của riêng mình
để cho phép truy cập.
Độ chi tiết
Quy tắc của bạn có thể rộng hoặc hẹp tuỳ theo nhu cầu.
Cấp bảo mật độc lập
Do Rules được định nghĩa bên ngoài ứng dụng của bạn (trong bảng điều khiển Firebase hoặc
Firebase CLI), khách hàng
không chịu trách nhiệm thực thi tính bảo mật, các lỗi không ảnh hưởng đến dữ liệu, và
dữ liệu của bạn luôn được bảo vệ.
Quảng cáo này hoạt động như thế nào?
Firebase Security Rules hoạt động bằng cách so khớp một mẫu với đường dẫn cơ sở dữ liệu, sau đó áp dụng
các điều kiện tuỳ chỉnh để cho phép truy cập vào dữ liệu tại các đường dẫn đó. Tất cả Rules
trên các sản phẩm của Firebase có thành phần phù hợp đường dẫn và
cho phép truy cập đọc hoặc ghi. Bạn phải xác định Rules cho
từng sản phẩm Firebase bạn sử dụng trong ứng dụng của mình.
Đối với Cloud Firestore và Cloud Storage, Rules hãy sử dụng hàm sau đây
cú pháp:
service <<name>> {
// Match the resource path.
match <<path>> {
// Allow the request if the following conditions are true.
allow <<methods>> : if <<condition>>
}
}
Đối với Realtime Database, Rules dựa trên JSON sử dụng cú pháp sau:
{
"rules": {
"<<path>>": {
// Allow the request if the condition for each method is true.
".read": <<condition>>,
".write": <<condition>>
}
}
}
Rules được áp dụng dưới dạng câu lệnh OR chứ không phải câu lệnh AND.
Do đó, nếu nhiều quy tắc phù hợp với một đường dẫn và bất kỳ quy tắc nào phù hợp
điều kiện nào cấp quyền truy cập, Rules cấp quyền truy cập vào dữ liệu tại đó
đường dẫn. Do đó, nếu một quy tắc rộng cấp quyền truy cập vào dữ liệu, bạn không thể hạn chế bằng
một quy tắc cụ thể hơn. Tuy nhiên, bạn có thể tránh vấn đề này bằng cách đảm bảo
Rules không trùng lặp quá nhiều. Firebase Security Rules cờ trùng lặp trong
đường dẫn phù hợp làm cảnh báo của trình biên dịch.
Firebase Security Rules cũng có thể tận dụng Authentication để cấp quyền dựa trên người dùng, và
mà bạn thiết lập có thể rất cơ bản hoặc cực kỳ phức tạp. Tìm hiểu thêm
về ngôn ngữ và hành viRules
trước khi bắt đầu viết Rules.
[[["Dễ hiểu","easyToUnderstand","thumb-up"],["Giúp tôi giải quyết được vấn đề","solvedMyProblem","thumb-up"],["Khác","otherUp","thumb-up"]],[["Thiếu thông tin tôi cần","missingTheInformationINeed","thumb-down"],["Quá phức tạp/quá nhiều bước","tooComplicatedTooManySteps","thumb-down"],["Đã lỗi thời","outOfDate","thumb-down"],["Vấn đề về bản dịch","translationIssue","thumb-down"],["Vấn đề về mẫu/mã","samplesCodeIssue","thumb-down"],["Khác","otherDown","thumb-down"]],["Cập nhật lần gần đây nhất: 2025-07-24 UTC."],[],[],null,["Firebase Security Rules \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| 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\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| | 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- [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)."]]