You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Firebase Realtime Database通配符规则配置后权限报错如何解决?

Firebase Realtime Database 通配符规则权限报错排查

业务需求

  • 实时数据库根节点下保留1层级(后续会新增2、3等同级节点)
  • 1节点下的子节点名称对应用户的Firebase唯一ID,仅允许用户读写自身ID所属节点内的所有数据,支持用户创建不存在的自身ID对应节点
  • 当前使用Firebase匿名登录能力

初始配置与报错现象

数据库规则配置

{
  "rules": {
    "1": {
      "$key": {
        ".read": "auth != null && auth.uid == $key",
        ".write": "auth != null && auth.uid == $key"
      }
    }
  }
}

数据库访问代码

Task<String> getIdTask = FirebaseInstallations.getInstance().getId()
FirebaseDatabase.getInstance()
                .getReference()
                .child("1")
                .child(installationId)

报错信息

Listen at /1/cKYZwWrlRmSof79rtfuX82 failed: DatabaseError: Permission denied

问题根因

错误使用Firebase Installations ID作为用户节点名,而规则中校验的auth.uid对应Firebase Auth 体系的用户ID,二者不属于同一身份体系,取值不匹配导致权限校验失败。

修复方案

替换获取用户唯一标识的代码,改用Firebase Auth返回的uid作为节点名称:

FirebaseAuth.getInstance().getCurrentUser().getUid();

内容的提问来源于stack exchange,提问作者Anonymous

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.28 18:54:05