【问题标题】:Recommend Firebase security rules doesn't work s推荐 Firebase 安全规则不起作用
【发布时间】:2021-12-24 13:23:11
【问题描述】:

我一直收到来自 Firebase 的电子邮件,说我的安全规则是安全的,所以我必须找到确保它们安全的正确方法。

Not recommended:

// Allow read/write access to all users under any conditions
// Warning: **NEVER** use this rule set in production; it allows
// anyone to overwrite your entire database.

service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if true;
    }
  }
}

推荐:

service cloud.firestore {
  match /databases/{database}/documents {
    // Assign roles to all users and refine access based on user roles
    match /some_collection/{document} {
     allow read: if request.auth != null && get(/databases/$(database)/documents/users/$(request.auth.uid)).data.role == "Reader"
     allow write: if request.auth != null && get(/databases/$(database)/documents/users/$(request.auth.uid)).data.role == "Writer"

     // Note: Checking for roles in your database using `get` (as in the code
     // above) or `exists` carry standard charges for read operations.
    }
  }
}

就在我尝试推荐的规则之后,我的应用程序停止为经过身份验证的用户工作,我不得不保留不安全的规则以允许用户重新使用该应用程序...有人知道为什么会发生这种情况吗?

【问题讨论】:

    标签: firebase security google-cloud-firestore firebase-security rules


    【解决方案1】:

    试试这个

    service cloud.firestore {
    match /databases/{database}/documents {
    match /{document=**} {
    allow read, write: if false;
    }   
     match /your_collecton_name/{your_documents} {
    allow read,write: if request.auth != null 
    }
    }
    }
    

    【讨论】:

    • 谢谢,我去试试。
    • 是的!效果很好。
    猜你喜欢
    • 2018-01-30
    • 1970-01-01
    • 2018-11-05
    • 2020-08-24
    • 2016-06-03
    • 2017-08-02
    • 1970-01-01
    • 2023-02-09
    相关资源
    最近更新 更多