我可以从android读取数据,即使在firebase中权限被拒绝



我有一个android应用程序连接到firestore。我有完全受限的数据库权限,像这样:

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

crearDatos函数工作完美,(它显示了一个错误,当试图创建一个数据),但函数读取数据,EventChangeListener读取数据,即使权限被拒绝。为什么会这样呢?你在我的代码中看到为什么它可以这样工作的任何原因吗?另一方面,我有一个javascript应用程序,它也连接到同一个firestore数据库,它工作得很好。我的意思是它拒绝我读和写,所以问题一定是在android应用程序。

public class MainActivity extends AppCompatActivity {
EditText titulo,contenido;
Button guardar,logout;
FirebaseFirestore mFirestore;
RecyclerView recycler;
adapter a;
ArrayList<Articulo> articulos=new ArrayList<>();
FirebaseAuth mAuth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mFirestore=FirebaseFirestore.getInstance();
titulo=findViewById(R.id.titulo);
contenido=findViewById(R.id.contenido);
guardar=findViewById(R.id.guardar);
recycler=findViewById(R.id.recycler);
mAuth=FirebaseAuth.getInstance();
logout=findViewById(R.id.logout);
guardar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
crearDatos();
}
});
logout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mAuth.signOut();
Intent intent = new Intent(MainActivity.this, login.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
});
mFirestore=FirebaseFirestore.getInstance();
recycler.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
//Query query= mFirestore.collection("Articulos");
a=new adapter(articulos);
recycler.setAdapter(a);
EventChangeListener();
}
private void crearDatos()
{
String t=titulo.getText().toString();
String c=contenido.getText().toString();
Map<String,Object> map=new HashMap<>();
map.put("titulo",t);
map.put("contenido",c);
map.put("fecha", new Date().getTime());
//mFirestore.collection("Articulos").document().set(map);
mFirestore.collection("Articulos").add(map).addOnSuccessListener(new OnSuccessListener<DocumentReference>() {
@Override
public void onSuccess(DocumentReference documentReference) {
Toast.makeText(getApplicationContext(), "El articulo se creo correctamente", Toast.LENGTH_LONG).show();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(getApplicationContext(), "Hubo un error al crear el articulo", Toast.LENGTH_LONG).show();
}
});
}
public void EventChangeListener()
{
mFirestore.collection("Articulos").addSnapshotListener(new EventListener<QuerySnapshot>() {
@Override
public void onEvent(@Nullable QuerySnapshot value, @Nullable FirebaseFirestoreException error) {
if (error != null)
{
Log.e("Firestore Error",error.getMessage());
return;
}
for (DocumentChange dc : value.getDocumentChanges()) {
Log.i("estoy aqui", "aqui estoy");
if (dc.getType() == DocumentChange.Type.ADDED) {
articulos.add(dc.getDocument().toObject(Articulo.class));
}
//a.notifyDataSetChanged();
}
a.notifyDataSetChanged();
}
});
}
}

如果以前的查询在本地缓存了一些文档,那么即使服务器上的安全规则发生了变化,这些本地文档也可能用于将来的请求。

如果你觉得你的应用必须立即响应安全规则,你应该通过指定"serve source"来阻止你的应用使用本地事件。在你的查询选项参数。

最新更新