【问题标题】:How can I set Firebase Database Security Rules for Android App如何为 Android 应用设置 Firebase 数据库安全规则
【发布时间】:2016-09-20 18:33:51
【问题描述】:

我正在制作 android 应用程序,它将从用户那里收集有用的信息。这是代码

Firebase postRef = mRef.child("marks");
Map<String, String> marks = new HashMap<String, String>();
marks.put("Name", fname);
marks.put("Phone", fphone);
marks.put("Year", fyear);
marks.put("Email", femail);
String uid=postRef.getKey();
Toast.makeText(im.this,"Thanks!",Toast.LENGTH_LONG).show();

这将导致从我的 Android 应用程序的每个用户那里获取数据。我应该如何设置只有经过身份验证的用户才能访问其信息的特定规则。不是其他用户信息。

我在 Firebase 规则中试过这个:

{
  "rules":{
  "$uid":{
    ".read":"auth.uid==$uid",
    ".write":"auth.uid==$uid",
    ".validate":"newData.hasChildren(['uid','Email','Name'])"
    }
  }

}

但它不起作用。请告诉我具体的解决方案。

【问题讨论】:

  • 规则看起来很好,以确保用户只能访问自己的数据。您共享的代码不会读取或写入数据库,因此不会失败。有什么问题?
  • 非常感谢弗兰克查找!在实际数据库中,没有 UID 字段。这是日志猫。 '09-21 00:36:03.971 31112-31223/com.example.amey.ilsalpha V/RenderScript: 0x559eaba9c0 启动线程,CPU 6 09-21 00:36:04.413 31112-31465/com.example.amey .ilsalpha W/RepoOperation: setValue at /marks/-KS7zP1oKI0WmcikjO1G failed: FirebaseError: Permission denied'

标签: android firebase firebase-realtime-database firebase-security


【解决方案1】:

如果我阅读了您的问题并正确评论,您将您的数据放在“标记”节点下。这意味着您还必须将其包含在您的安全规则中,如下所示:

{
"rules":{
  "marks":{
    "$uid":{
      ".read":"auth.uid==$uid",
      ".write":"auth.uid==$uid",
      ".validate":"newData.hasChildren(['uid','Email','Name'])"
      }
    }
  }
}

【讨论】:

  • 谢谢安德烈!但它仍然不起作用!虽然它对阅读有很大帮助。但是需要写解决方案。
  • @coolamz 您应该添加用于编写问题的代码。
  • 抱歉!它的第一个问题在这里!这是当前代码: { "rules":{ "marks":{ "$uid":{ ".read":"auth.uid==$uid", ".write":true, ".validate": "newData.hasChildren(['Email','Name'])" } } } }
猜你喜欢
  • 2019-07-03
  • 2021-10-17
  • 2016-09-16
  • 2019-05-28
  • 2020-08-08
  • 2018-12-15
  • 1970-01-01
  • 2022-01-03
相关资源
最近更新 更多