【问题标题】:firestore: PERMISSION_DENIED: Missing or insufficient permissionsfirestore:PERMISSION_DENIED:权限缺失或不足
【发布时间】:2018-03-17 08:09:58
【问题描述】:

我收到错误消息

获取documents.com.google.firebase.firestore.FirebaseFirestoreException: PERMISSION_DENIED:权限缺失或不足。

else 语句中的以下代码

db.collection("users")
    .get()
    .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
        @Override
        public void onComplete(@NonNull Task<QuerySnapshot> task) {
             if (task.isSuccessful()) {
                 for (DocumentSnapshot document : task.getResult()) {
                     s(document.getId() + " => " + document.getData());
                 }
             } else {
                 s("Error getting documents."+ task.getException());
             }
         }
     });

【问题讨论】:

标签: android firebase google-cloud-firestore


【解决方案1】:

进入数据库 -> 规则 ->

用于开发:

allow read, write: if false; 更改为 true;

注意:它只是用于开发目的的快速解决方案,因为它会关闭所有安全性。因此,不建议用于生产。

用于生产:

如果从 firebase 进行身份验证:Change allow read, write: if false;request.auth != null;

【讨论】:

  • 请注意,它允许所有人在没有任何授权的情况下读取、写入您的数据库。
  • 这是一个可怕的解决方案,这实际上只是禁用了安全性。改为阅读此内容:firebase.google.com/docs/firestore/security/get-started
  • 就像@DuncanLuk 所说,这是一个糟糕的解决方案。我什至不会称其为解决方案
  • 快速入门的最佳解决方案。实际上,安全问题可以稍后解决。
  • 这不是解决方案。 “我的安全规则有问题,让我们完全禁用它们吧!”
【解决方案2】:

转到数据库 -> 规则

然后更改以下规则

service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if false;
    }
  }
}

到下面

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

【讨论】:

  • 这是个坏主意,这使得任何经过身份验证的用户都可以写入所有文档,甚至是属于其他用户的内容,应该只能由管理员写入或根本不可写入的内容。请保留第一段代码,因为它可以防止未实现的安全规则。
  • 请注意,如果您让人们注册(例如使用 Google SSO),他们将自动有权访问您的所有数据。
  • 我想允许我的认证用户(少数)访问所有文档,所以这个秘籍非常适合我的情况。
【解决方案3】:

所以就我而言,我有以下数据库规则:

service cloud.firestore {
  match /databases/{database}/documents {
    match /stories/{story} {
      function isSignedIn() {
        return request.auth.uid != null;
      }
    
      allow read, write: if isSignedIn() && request.auth.uid == resource.data.uid
    }
  }
}

如您所见,story 文档上有一个 uid 字段来标记所有者。

然后在我的代码中查询所有故事(Flutter):

Firestore.instance
          .collection('stories')
          .snapshots()

它失败了,因为我已经通过不同的用户添加了一些故事。 要修复它,您需要向查询添加条件:

Firestore.instance
          .collection('stories')
          .where('uid', isEqualTo: user.uid)
          .snapshots()

更多详情:https://firebase.google.com/docs/firestore/security/rules-query

编辑:来自链接

规则不是过滤器 在编写查询以检索文档时,请保持 请记住,安全规则不是过滤器——查询是全部或 没有。为了节省您的时间和资源,Cloud Firestore 评估了一个 查询其潜在结果集而不是实际字段 所有文档的值。如果查询可能返回 客户无权阅读的文件,整个 请求失败。

【讨论】:

  • 用户对象是什么?
  • 我收到Invalid variable name: requestrequest 应该是某个地方的参数吗?
【解决方案4】:

以上投票的答案对您的数据库的健康是危险的。您仍然可以使您的数据库仅用于读取而不用于写入:

  service cloud.firestore {
    match /databases/{database}/documents {
     match /{document=**} {
       allow read: if true;
       allow write: if false;
      }
   }
}

【讨论】:

  • 启用权限的正确方法是什么,但并不适合所有人?
【解决方案5】:

如果您尝试使用 Java Swing 应用程序。

  1. 转到Firebase Console > Project Overview > Project Settings

  2. 然后转到服务帐户选项卡,然后单击生成新私钥。

  3. 你会得到一个 .json 文件,把它放在一个已知的路径中

  4. 然后转到我的电脑属性、高级系统设置、环境变量。

  5. 使用您的 json 文件路径创建新的路径变量 GOOGLE_APPLICATION_CREDENTIALS 值。

【讨论】:

  • 这也是我的问题,docs 上有相关信息。
【解决方案6】:

在指定安全规则后,我也遇到了“权限缺失或权限不足”错误。原来这些规则默认不是递归的!即,如果您编写了类似的规则

match /users/{userId} {
  allow read, write: if request.auth != null && request.auth.uid == userId;
}

该规则不适用于/users/{userId} 下的任何子集合。这就是我出错的原因。

我通过将规则指定为:

match /users/{userId}/{document=**} {
  allow read, write: if request.auth != null && request.auth.uid == userId;
}

relevant section of the documentation了解更多信息。

【讨论】:

    【解决方案7】:

    如果有人登陆这里尝试使用服务帐户访问 Firestore:

    我通过在 GCP 的 IAM 设置中授予服务帐户 Service Account User 角色以及 Cloud Datastore User 角色解决了这个问题。

    【讨论】:

    • 如果您的用户是服务帐户,则不需要服务帐户用户。
    【解决方案8】:

    确保您的数据库不是空的,也不是您的查询是针对不存在的集合

    【讨论】:

    • 这个异常与空集合或db无关,是权限问题
    • 此异常与空Db等无关。安全规则和身份验证问题。
    • 我和至少其他 3 人在这种情况下有这个例外。如果对您没有帮助,请继续下一个解决方案
    【解决方案9】:

    npm i --save firebase @angular/fire

    在 app.module 中确保您已导入

    import { AngularFireModule } from '@angular/fire';
    import { AngularFirestoreModule } from '@angular/fire/firestore';
    

    进口

    AngularFireModule.initializeApp(environment.firebase),
        AngularFirestoreModule,
        AngularFireAuthModule,
    

    确保您拥有实时数据库规则

    {
      /* Visit  rules. */
      "rules": {
        ".read": true,
        ".write": true
      }
    }
    

    在 Cloud Firestore 规则中确保您有

    rules_version = '2';
    service cloud.firestore {
      match /databases/{database}/documents {
        match /{document=**} {
          allow read, write: if true;
        }
      }
    }
    

    【讨论】:

      【解决方案10】:

      此外,如果代码中的集合引用与 firebase 上的集合名称不匹配,您可能会收到此错误。

      例如,firebase 上的集合名称是 users,但您使用 db.collection("Users")db.collection("user") 引用它

      它也区分大小写。

      希望这对某人有所帮助

      【讨论】:

      • 集合不是隐式创建的吗?在您的示例中,“用户”和“用户”集合将在被引用时创建。
      • @DevTJ 当它是 addset 请求时你是正确的,但从问题来看它是 get 请求。我以前经历过这种情况
      【解决方案11】:

      Firebase Admin 出现此错误,解决方案是正确配置 Firebase Admin following this link

      【讨论】:

        【解决方案12】:

        进入 Firestore 的数据库 > 规则:

        rules_version = '2';
            service cloud.firestore {
              match /databases/{database}/documents {
                match /{document=**} {
                  allow read, get: if true;
                  allow write: if false;
              }
            }
        }
        

        更多信息:https://firebase.google.com/docs/rules/rules-language?authuser=0

        目前 100% 工作!?

        【讨论】:

        【解决方案13】:

        检查服务帐户是否添加到IAM &amp; Admin https://console.cloud.google.com/iam-admin/iam 中并具有适当的角色,例如编辑器

        【讨论】:

          【解决方案14】:

          此时,即 2020 年 6 月,默认情况下,Firebase 是按时间定义的。 确定满足您需求的时间。

          allow read, write: if request.time < timestamp.date(2020, 7, 10);
          

          请注意:您的数据库仍然对任何人开放。我建议,请阅读文档并以对您有用的方式配置数据库。

          【讨论】:

            【解决方案15】:

            问题是您在用户通过身份验证之前尝试将数据读取或写入实时数据库或 Firestore。请尝试检查您的代码的范围。 希望对您有所帮助!

            【讨论】:

              【解决方案16】:

              时间限制可能已过

              rules_version = '2';
              service cloud.firestore {
                match /databases/{database}/documents {
              
                  // This rule allows anyone on the internet to view, edit, and delete
                  // all data in your Firestore database. It is useful for getting
                  // started, but it is configured to expire after 30 days because it
                  // leaves your app open to attackers. At that time, all client
                  // requests to your Firestore database will be denied.
                  //
                  // Make sure to write security rules for your app before that time, or else
                  // your app will lose access to your Firestore database
                  match /{document=**} {
                    allow read, write: if request.time < timestamp.date(2020,7, 1);
                  }
                }
              }
              

              在这一行有现在的更改日期

               allow read, write: if request.time < timestamp.date(2020,7, 1);
              

              【讨论】:

                【解决方案17】:

                对我来说,这是日期的问题。更新了,问题就解决了。

                允许读/写:

                 if request.time < timestamp.date(2020, 5, 21);
                

                编辑:如果您仍然感到困惑并且无法找出问题所在,请查看您的 firebase 控制台中的规则部分。

                【讨论】:

                  【解决方案18】:

                  转到 firebase 中的规则并编辑规则.....(提供时间戳或设置为 false) 我的解决方案。

                  service cloud.firestore {
                    match /databases/{database}/documents {
                      match /{document=**} {
                        allow read, write: if request.time < timestamp.date(2021, 8, 18);
                      }
                    }
                  }
                  

                  【讨论】:

                    【解决方案19】:

                    转到firebase控制台=>云firestore数据库并添加允许用户读写的规则。

                    => 允许读、写

                    【讨论】:

                      【解决方案20】:

                      也许你需要删除日期

                      rules_version = '2';
                      service cloud.firestore {
                        match /databases/{database}/documents {
                          match /{document=**} {
                            allow read, write: if
                                request.time < timestamp.date(2021, 12, 12);
                          }
                        }
                      }
                      

                      【讨论】:

                        【解决方案21】:

                        转到 Apple 证书、标识符和配置文件: 选择您的密钥上传火力库并进行检查: 访问 DeviceCheck 和 AppAttest API 以获取您关联的数据

                        enter image description here

                        【讨论】:

                          【解决方案22】:

                          https://console.firebase.google.com

                          开发->数据库->规则->设置读、写->真

                          【讨论】:

                            【解决方案23】:

                            对于这种情况,您的规则应该是这样的,但是消息说这不是建议的方式,但是如果您这样做,您可以在没有权限错误的情况下进行插入

                            rules_version = '2';
                            service cloud.firestore {
                              match /databases/{database}/documents {
                                match /{document=**} {
                                  allow read, write;
                                }
                              }
                            }
                            

                            【讨论】:

                              猜你喜欢
                              • 2019-10-24
                              • 2018-09-12
                              • 2020-10-14
                              • 2020-09-25
                              • 2020-10-01
                              • 2018-08-24
                              • 2017-02-20
                              相关资源
                              最近更新 更多