实时数据库规则语言中的 Firebase 速率限制

Firebase Rate Limiting In Realtime Database Rules Language


我目前正在尝试通过检查最后一个 post 的时间戳来实现速率限制,然后向其添加 + 60sec,然后检查它是否小于(<)然后当前的 Firebase 服务器时间戳(现在)。 它总是 returns true 并授予访问权限?!

这些是我的规则:

{
  "rules": {
        "posts": {
        ".read": true,
            ".write": 
"(root.child('users').child(auth.uid).child('lastPost').val() + 60) < now"
      }
  }
}




这是我的数据库结构

{
    "posts": {
        "-KV70ppGGTEtXY4_Q4UC": {
            "author": "abcdedef-uid-ojifgoöifjgssgd",
            "description": "Thats the post description",
            "title": "Thats the post title"
        }
    },
    "users": {
        "2uy7323nTodMHcVxeEDJzoexH302": {
            "canPost": true,
            "email": "cryptic.pug@firebase.com",
            "lastPost": 14776667681,
            "profile_picture": "https://lh3.googleusercontent.com/-XdUIqdMkCWA/AAAAAAAAAAI/AAAAAAAAAAA/4252rscbv5M/photo.jpg",
            "username": "Cryptic Pug"
        }
    }
}

感谢您提供线索 Vladimir!

因为我还没有找到这种解决方案所以我想在这里正式分享答案:

    {
  "rules": {
        "posts": {
        ".read": true,
            ".write": 
"(root.child('users').child(auth.uid).child('lastPost').val() + 60000) < now"
      }
  }
}

解释:

当用户 post 时,您总是用 firebase.database.ServerValue.TIMESTAMP 的值更新数据库中的值到用户信息。 在您阅读的规则语言中,最后 Post 的时间戳从想要 post 的用户中读出(FB 规则语言中的 auth.uid )并添加 60 秒(*1000 作为 Firebase在它的时间戳中使用毫秒),这将是允许用户再次 post 的时间。然后检查当前服务器时间戳是否高于 (<) 允许用户再次 post 的时间。

希望它对你们有所帮助,Happy Coding - 做 Firebase 3 天,很棒!