即使在Firebase中拒绝权限,我也可以阅读来自Android的数据

发布于 2025-02-03 21:05:49 字数 4053 浏览 5 评论 0原文

我有一个与Firestore连接的Android应用程序。我已经完全限制了数据库权限,例如:

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

creardatos函数效果很好,(尝试创建数据时它显示了一个错误),但是读取数据的功能,event> 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();
            }
        });
    }
}

i have an android application connected to firestore. I have totally restricted database permissions, like this:

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

The crearDatos function works perfectly, (it shows me an error when trying to create a data), but the function to read data, EventChangeListener reads the data even when the permissions are denied. Why can this be? Do you see in my code any reason why it can work like this?
On the other hand, I have a javascript application that also connects to the same firestore database, and it works perfectly. I mean that it denies me both reading and writing, so the problem must be in the android application.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

¢蛋碎的人ぎ生 2025-02-10 21:05:49

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

如果您认为您的应用必须立即响应安全规则,则应通过在查询选项参数中的查询中指定“服务源”来防止应用程序使用本地事件。

In the case that a previous query cached some documents locally, those local documents might be used to serve a future request even if the security rules on the server have changed.

If you feel your app must respond immediately to security rules, you should prevent your app from using local events by specifying a "serve source" in your query in the query options parameter.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文