我得到了错误

gettingdocuments.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());
             }
         }
     });

当前回答

这里的变化 将false设为true

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

并发布新的规则

其他回答

进入数据库-> 规则- - - >

发展:

更改允许读,写:如果为false;真正的;

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

生产:

如果从firebase验证:更改允许读,写:如果为false;请求。Auth != null;

对我来说,问题是AppCheck在我的Firestore控制台也被激活了。 所以我必须按照指南中所述的应用程序检查颤振指南

https://firebase.google.com/docs/app-check/flutter/debug-provider?hl=it&authuser=0

打开androidDebugProvider: true,从控制台复制调试令牌,并将其粘贴到Firestore部分(AppCheck—> app—> add调试令牌),它立即工作。

所以在我的情况下,我有以下DB规则:

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
    }
  }
}

正如您所看到的,故事文档上有一个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会评估一个 根据其潜在结果集而不是实际字段查询 所有文档的值。查询是否可能返回 客户端没有权限读取的文档,全部删除 请求失败。

另一个原因是AppCheck。我在一个新项目上启用了它(创建~ 2022年5月),但还没有完成集成步骤,导致“缺少或权限不足”错误。

要解决此问题,首先完成Firebase中AppCheck部分中列出的AppCheck设置步骤。我在我的web应用程序中使用了ReCAPTCHA提供者,你需要复制ReCAPTCHA公钥来在你的代码库中使用。

接下来,在你初始化firebase应用的任何地方添加AppCheck初始化代码。我的在React中是这样的:

  import { initializeAppCheck, ReCaptchaV3Provider } from 'firebase/app-check';

  // ......

  // Initialize Firebase
  const app = initializeApp(firebaseConfig);
  const analytics = getAnalytics(app);

  self['FIREBASE_APPCHECK_DEBUG_TOKEN'] = true;
  // Pass your reCAPTCHA v3 site key (public key) to activate(). Make sure this
  // key is the counterpart to the secret key you set in the Firebase console.
  initializeAppCheck(app, {
    provider: new ReCaptchaV3Provider('PASTE KEY HERE'),

    // Optional argument. If true, the SDK automatically refreshes App Check
    // tokens as needed.
    isTokenAutoRefreshEnabled: true,
  });

注意FIREBASE_APPCHECK_DEBUG_TOKEN行在浏览器控制台中打印了一个调试令牌,你需要将它复制回AppCheck下的Firebase控制台中以完成设置,之后你可以注释/删除该行。

这解决了我的问题。

进一步的信息:

https://firebase.google.com/docs/app-check/web/recaptcha-provider https://firebase.google.com/docs/app-check/web/debug-provider?authuser=0&hl=en

此时,即2020年6月,默认情况下firebase是按时间定义的。 为满足自己的需要而安排好时间。

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

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