【问题标题】:how to write rules in firebase realtime database?如何在firebase实时数据库中编写规则?
【发布时间】:2020-11-25 11:42:35
【问题描述】:

我得到了如下所示的安全规则。但是在规则操场上进行测试时。 读取和更新时 - 模拟拒绝。

{
  "rules": {
    "users": {
      "$uid": {
        ".read": "$uid === auth.uid",
        ".write": "$uid === auth.uid"
      }
    }
  }
}

我需要根据我的数据库结构添加json数据。鉴于我的实时数据库结构。如何编写安全的读写规则?

【问题讨论】:

    标签: android json firebase firebase-realtime-database firebase-authentication


    【解决方案1】:

    您的规则不允许用户访问整个 /users 集合。在您的模拟中,您可以尝试访问/users/<uid> 位置,应该可以正常工作。

    【讨论】:

    • 是的。我需要为我的实时数据库下的每个集合编写规则以确保安全。
    【解决方案2】:

    如下改变你的规则

    "users": {
          "$uid": {
            ".read": "$uid === auth.uid",
            ".write": "$uid === auth.uid"
          }
    

    优秀的文档 - https://firebase.google.com/docs/database/security

    .read 和 .write 规则级联,因此此规则集授予对路径 /foo/ 以及任何更深的路径(例如 /foo/bar/baz)的任何数据的读取访问权限。请注意,数据库中较浅的 .read 和 .write 规则会覆盖较深的规则,因此即使路径 /foo/bar/baz 的规则评估为 false,在此示例中仍将授予对 /foo/bar/baz 的读取访问权限。

    【讨论】:

    • 根据您的解决方案更新了我的问题。在规则游乐场中仍然显示拒绝。
    • 您的数据输入正确。您正在尝试更新 uid...您需要更新 { "key": "value" }
    猜你喜欢
    • 2020-12-28
    • 2018-08-27
    • 1970-01-01
    • 2019-10-11
    • 1970-01-01
    • 2022-11-11
    • 2021-01-31
    相关资源
    最近更新 更多