【问题标题】:Realtime database authentication: failed: DatabaseError: Permission denied实时数据库身份验证:失败:DatabaseError:Permission denied
【发布时间】:2017-05-27 20:29:12
【问题描述】:

我想使用security quickstart guide中的这些规则

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

虽然我不明白auth.uid 是从哪里读取的?

当我执行以下操作时,我可以让它存储信息,这样我就可以确认问题出在auth.id。我不确定是否需要将其作为参数或其他东西传递?恐怕这就是我迷路的地方。

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

这是我用来测试它的当前 java 代码:

FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference();
User u = new User("Jack", "jack@email.co.uk", "12345678"); // Very simple class holding this user information
myRef.child("users").child(mFirebaseUser.getUid()).setValue(u);

我一直在查看有关 firebases 实时数据库的信息,但每次他们进行身份验证时,他们只会说“现在我们将关闭身份验证,但请确保您在生产中使用它!”。

【问题讨论】:

  • 要了解安全规则中的auth 变量,请参阅firebase.google.com/docs/database/security/…。一旦用户登录并包含uid 属性,它就会自动填充,该属性是登录用户的唯一标识符。
  • 您共享的代码未对用户进行身份验证,因此此规则将失败$uid === auth.uid。要了解如何登录用户,请参阅firebase.google.com/docs/auth
  • 是的,抱歉,所以我有一个 LoginActivity 来验证设置此活动的意图。我相信问题可能是我在不同的活动中对用户进行身份验证,并且它正在以某种方式失去其“身份验证”状态
  • Firebase 身份验证状态在活​​动之间保持不变。但mFirebaseUser 可能不是。没有看到minimal, complete code that reproduces the problem,很难确定。

标签: java firebase firebase-realtime-database firebase-authentication firebase-security


【解决方案1】:

好吧,您不需要作为参数传递,firebase 会为您完成。让我试着解释一下,在您在控制台中创建用户并登录您的应用程序后,firebase 将为每个用户提供不同的uid(类似于此ca07912b-7e65-41ad-8342-e87cf6f6ceb2)。

所以在您登录后,它将存储在您的智能手机中,并且您在数据库 firebase 中执行的每个请求都会分析您的安全规则,所以当您说只有 uid 的所有者 X可以访问/users/**X**/..,那么如果你没有登录,你就没有权限在这个路径上读写。

【讨论】:

    【解决方案2】:

    我是个白痴,

    我在代码上方几行处调用了FirebaseAuth.getInstance().signOut();,而这些代码最初是为了调试目的而放入的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-26
      • 2018-11-02
      • 1970-01-01
      • 2018-01-15
      • 1970-01-01
      • 2014-01-24
      相关资源
      最近更新 更多