Firebase数据库的匿名身份验证权限被拒绝

时间:2017-07-23 19:40:43

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

我有firebase数据库,它正常工作,直到规则设置为:

 "rules": {
      ".read": true,
      ".write": false
  }

但是现在我需要启动我的应用程序,所以我不希望我的数据库对所有人开放。这就是我将数据库规则更改为:

的原因
{
  "rules": {
    ".read": "auth != null",
    ".write": "auth != null"
  }
}

我从我的androd应用程序设置了匿名身份验证,但权限被拒绝错误一直显示。顺便说一句,Anonymouse身份验证在模拟器中运行良好。

这是我实施它的方式:

 public void fireBaseAuthenticate(final Context context) {
     mAuth = FirebaseAuth.getInstance();
     mAuth.signInAnonymously()
         .addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    if (task.isSuccessful()) {
                       // debugger comes here, so Authentication is successful 
                    } else {
                       // If sign in fails, display a message to the user.

                    }

                }
    });

}

public void load(final Context context) {

    fireBaseAuthenticate(context);
    database = FirebaseDatabase.getInstance();
    databaseReference = database.getReference();
    databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot categorySnaps : dataSnapshot.getChildren()) {
                RetailChain retailChain = new RetailChain();
                retailChain.setName(categorySnaps.getKey());
                for (DataSnapshot subMenuSnaps : categorySnaps.getChildren()) {
                    for (DataSnapshot itemSnaps : subMenuSnaps.getChildren()) {
                        for (DataSnapshot productSnaps : itemSnaps.getChildren()) {
                            Product product = productSnaps.getValue(Product.class);
                            productList.add(product);
                        }
                    }
                }
                retailChain.setProducts(productList);
                retailChainList.add(retailChain);
            }      
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            Log.w("database error: ", "onCancelled", databaseError.toException());
        }
    });

}

添加singleValueEvent的侦听器后,OnCancelled被调用。 那么为什么许可被拒绝了? 修改 错误日志:

com.google.firebase.database.DatabaseException: Firebase Database error: 
    Permission denied
    at com.google.firebase.database.DatabaseError.toException(Unknown Source)
    at home.gio.calorieplanner.main.Main$3.onCancelled(Main.java:205)
    at com.google.firebase.database.Query$1.onCancelled(Unknown Source)
    at com.google.android.gms.internal.zzbpx.zza(Unknown Source)
    at com.google.android.gms.internal.zzbqv.zzZT(Unknown Source)
    at com.google.android.gms.internal.zzbra$1.run(Unknown Source)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:5417)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

1 个答案:

答案 0 :(得分:1)

设置规则"auth != null"时,意味着用户在完成登录之前无法访问数据库。

登录过程是asynchronous,这意味着不会立即调用完整的侦听器。

要解决您的问题,只需在完整的侦听器中移动读取数据库的代码。

public void load(final Context context) {

  fireBaseAuthenticate(context);

}

public void fireBaseAuthenticate(final Context context) {
  mAuth = FirebaseAuth.getInstance();
  mAuth.signInAnonymously()
     .addOnCompleteListener(new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {
                if (task.isSuccessful()) {
                   // load the data
                   loadFromDB();
                } else {
                   // If sign in fails, display a message to the user.

                }

            }
  });

}


private void loadFromDB(){
  database = FirebaseDatabase.getInstance();
  databaseReference = database.getReference();
  databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
      @Override
      public void onDataChange(DataSnapshot dataSnapshot) {
        for (DataSnapshot categorySnaps : dataSnapshot.getChildren()) {
            RetailChain retailChain = new RetailChain();
            retailChain.setName(categorySnaps.getKey());
            for (DataSnapshot subMenuSnaps : categorySnaps.getChildren()) {
                for (DataSnapshot itemSnaps : subMenuSnaps.getChildren()) {
                    for (DataSnapshot productSnaps : itemSnaps.getChildren()) {
                        Product product = productSnaps.getValue(Product.class);
                        productList.add(product);
                    }
                }
            }
            retailChain.setProducts(productList);
            retailChainList.add(retailChain);
        }      
      }

      @Override
      public void onCancelled(DatabaseError databaseError) {
        Log.w("database error: ", "onCancelled", databaseError.toException());
      }
  });

}