【问题标题】:Firebase chat app setValue failed error with a public database?Firebase 聊天应用程序 setValue 在公共数据库中失败?
【发布时间】:2018-10-02 23:23:46
【问题描述】:

我有一个使用 Firebase 的聊天应用,它一直在使用

setValue at x 失败:DatabaseError: 权限被拒绝

每次输入信息都会出错。

我已经将我的数据库设置为公开:

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

这是我的聊天参考中的内容吗?

private void displayChat() {

    ListView listOfMessage = findViewById(R.id.list_of_message);

    Query query = FirebaseDatabase.getInstance().getReference();
    FirebaseListOptions<Chat> options = new FirebaseListOptions.Builder<Chat>()
            .setLayout(R.layout.list_item)
            .setQuery(query, Chat.class)
            .build();

    adapter = new FirebaseListAdapter<Chat>(options) {
        @Override
        protected void populateView(View v, Chat model, int position) {
            //Get reference to the views of list_item.xml
            TextView messageText, messageUser, messageTime;
            messageText = v.findViewById(R.id.message_text);
            messageUser = v.findViewById(R.id.message_user);
            messageTime = v.findViewById(R.id.message_time);

            messageText.setText(model.getMessageText());
            messageUser.setText(model.getMessageUser());
            messageTime.setText(DateFormat.format("dd-MM-yyyy (HH:mm:ss)", model.getMessageTime()));
        }
    };
    listOfMessage.setAdapter(adapter);
}

【问题讨论】:

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


【解决方案1】:

您的代码正在使用 Firebase 实时数据库,但您正在更改 Cloud Firestore 的安全规则。虽然这两个数据库都是 Firebase 的一部分,但它们完全不同,一个的服务器端安全规则不适用于另一个。

当您进入 Firebase 控制台中的数据库面板时,您很可能会看到 Cloud Firestore rules

如果您使用的是Cloud Firestore rules in the Firebase console,则可以通过单击顶部的 Cloud Firestore BETA,然后从列表。

您也可以通过点击this link直接进入实时数据库的安全规则。

与您所拥有的实时数据库相匹配的安全规则是:

{
  "rules": {
    ".read": "auth.uid !== null",
    ".write": "auth.uid !== null"
  }
}

这将授予任何经过身份验证的用户对整个数据库的完全读写访问权限。阅读我对这个问题的回答,了解更多关于此类规则的安全/风险权衡:Firebase email saying my realtime database has insecure rules

【讨论】:

    【解决方案2】:

    改变这个

    request.auth.uid != null

    request.auth.uid == null
    

    或在开始对话之前定义适当的身份验证机制,其中用户由 userID 定义

    【讨论】:

      猜你喜欢
      • 2020-09-26
      • 2018-06-01
      • 2017-11-16
      • 2016-09-27
      • 2016-06-28
      • 2018-10-13
      • 2017-09-30
      相关资源
      最近更新 更多