java - 在 Android Firebase 中实现基于用户的安全性

标签 java android firebase firebase-realtime-database firebase-authentication

当以用户身份登录我的应用程序时,我希望该用户能够访问他们自己的特定维护记录。据推测,这可以使用用户使用 Firebase Auth 注册应用程序后生成的 UID 来完成。

维护 Activity

   private boolean updateMaintenance(String title, String desc, String id, String primary, String secondary, String property) {

        DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference("maintenance").child(id);
        Maintenance maintenance = new Maintenance (id, title, desc, primary, secondary, property);

        databaseReference.setValue(maintenance);

        Toast.makeText(this, "Maintenance Updated Updated Successfully", Toast.LENGTH_LONG).show();

        return true;
    }

    private void addMaintenance(){

        String title = editTextTitle.getText().toString().trim();
        String desc = editTextDesc.getText().toString().trim();
        String primary = spinnerPrimary.getSelectedItem().toString();
        String secondary = spinnerSecondary.getSelectedItem().toString();
        String property = spinnerProperty.getSelectedItem().toString();

        if(!TextUtils.isEmpty(title)){

            String id = databaseMaintenance.push().getKey();

            Maintenance maintenance = new Maintenance (id, title, desc, primary, secondary, property);

            databaseMaintenance.child(id).setValue(maintenance);

            Toast.makeText(this, "Maintenance Added", Toast.LENGTH_LONG).show();

        } else {

            Toast.makeText(this, "You must enter a maintenance record", Toast.LENGTH_LONG).show();
        }

    }

登录 Activity

这就是我目前所实现的。目前,我可以使用 Firebase 存储的凭据登录,但是,始终会显示 Firebase 数据库中保存的维护记录。无论谁登录。

Auth.signInWithEmailAndPassword(email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
        @Override
        public void onComplete(@NonNull Task<AuthResult> task) {

            if(task.isSuccessful()){

                Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
            } else {
                Toast.makeText(getApplicationContext(), task.getException().getMessage(), Toast.LENGTH_SHORT).show();
            }

        }
    });

数据库规则

我已将数据库规则设置如下:

    {
  "rules": {
    "users": {
      "$uid": {
        ".write": "$uid === auth.uid",
        ".read": "$uid === auth.uid"
      }
    }
  }
}

当前 Firebase 数据库

这些是以任何用户身份登录时显示的维护案例。

enter image description here

不确定此处是否需要我的 MaintenanceActivity 代码,如果需要,则需要哪些部分。

对此的任何帮助都将受到极大的赞赏,我已经在这方面坚持了很长时间了。

非常感谢。

最佳答案

仅获取 firebase 中登录的用户记录..

首先在 Firebase 控制台中添加安全规则。

  match /databases/{database}/documents {
    match /maitenance/{userId} {
      allow read, write: if request.auth.uid == userId;
    }
  }

然后,您需要将用户id节点添加为维护节点的子节点,用户维护记录应该是登录用户id节点的子节点。

最后,在读取操作中传递用户id来获取用户id的维护记录。

  firestoreDB.collection("maintenance")
            .whereEqualTo("userId", userId)
        .get()

请查看如何user specific data is saved in and fetched from firestore database 。您也可以对实时数据库执行相同的操作。

关于java - 在 Android Firebase 中实现基于用户的安全性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49284825/

相关文章:

java - 如何让Android应用程序保持活力

android - 使用 AWS Amplify 和 Android 通过 Cognito 验证 REST API

google-app-engine - 未找到端点颁发者属性。

firebase - 错误存储/未知上传图片到 Firebase 存储

firebase - 如何使用flutter从Firebase数据库获取值

java - 如何有效地对抽象列表进行嵌套迭代?

java - 传递国家/地区名称时获取所有州/地区/城市

java - 批量插入现有数据 : Preventing JPA to do a select before every insert

java - AbstractCollection 的目的是什么

android - 如何在我的 Android 布局中创建叠加按钮