Android Firebase 数据库错误: Permission denied
Android Firebase 数据库错误: Permission denied
我正在开发一个Android项目,需要进行用户邮箱和密码认证。详细信息存储在Firebase数据库中。问题出现在我尝试使用邮箱和密码再次登录时。在我的logcat中,错误信息是:
W/SyncTree: Listen at / failed: DatabaseError: Permission denied
请看下面的代码:
public class LoginUser extends AppCompatActivity { private RelativeLayout relativeLayout; private EditText et_email, et_password; private Button loginBtn; private FirebaseAuth mAuth; private FirebaseAuth.AuthStateListener authStateListener; private DatabaseReference databaseReference; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login_user); mAuth = FirebaseAuth.getInstance(); databaseReference = FirebaseDatabase.getInstance().getReference(); databaseReference.keepSynced(true); relativeLayout = (RelativeLayout) findViewById(R.id.activity_login_user); et_email = (EditText) findViewById(R.id.emailField); et_password = (EditText) findViewById(R.id.pwdField); loginBtn = (Button) findViewById(R.id.loginBtn); loginBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { initLogin(); } }); authStateListener = new FirebaseAuth.AuthStateListener() { @Override public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) { if (firebaseAuth.getCurrentUser() != null){ initLogin(); } else { startActivity(new Intent(LoginUser.this,RegisterFireBase.class)); } } }; } @Override protected void onStart() { super.onStart(); mAuth.addAuthStateListener(authStateListener); } @Override protected void onStop() { super.onStop(); if (mAuth != null){ mAuth.removeAuthStateListener(authStateListener); } } private void initLogin() { String email = et_email.getText().toString().trim(); String pass = et_password.getText().toString().trim(); if (!TextUtils.isEmpty(email) && !TextUtils.isEmpty(pass)){ mAuth.signInWithEmailAndPassword(email,pass).addOnCompleteListener(this, new OnCompleteListener() { @Override public void onComplete(@NonNull Task task) { checkForUser(); } }); } else { Toast.makeText(this, "Some fields are empty", Toast.LENGTH_SHORT).show(); } } private void checkForUser() { final String userId = mAuth.getCurrentUser().getUid(); databaseReference.addValueEventListener(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { if (dataSnapshot.hasChild(userId)){ Intent loginIntent = new Intent(LoginUser.this, FireProfile.class); loginIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(loginIntent); Snackbar.make(relativeLayout,"Log In Successful",Snackbar.LENGTH_LONG).show(); } } @Override public void onCancelled(DatabaseError databaseError) { } }); }
}
是什么原因导致了这个错误?
(Android Firebase Database Error: Permission denied)这个问题的出现的原因是因为应用程序的Firebase数据库权限设置不正确,导致用户无法读取或写入数据。为了解决这个问题,你可以根据以下方法进行设置。
首先,确保你的应用程序不是公开的,除非有必要。根据Google文档的描述,你可以在Firebase控制台的数据库规则中设置以下规则:
`// 这些规则授予与经过身份验证的用户的ID匹配的节点的访问权限
{
"rules": {
"users": {
"$uid": {
".read": "$uid === auth.uid",
".write": "$uid === auth.uid"
}
}
}
}`
上述规则将确保只有与经过身份验证的用户的ID匹配的节点才能被读取和写入。
另外,你也可以设置只允许经过身份验证的用户访问数据库的规则:
`// 这些规则要求进行身份验证
{
"rules": {
".read": "auth != null",
".write": "auth != null"
}
}`
上述规则将要求用户进行身份验证后才能读取和写入数据库。
允许应用程序公开访问会使任何人都能读取和写入你的应用程序,因此我认为没有任何应用程序应该这样使用。
通过正确设置Firebase数据库的权限规则,你可以解决(Android Firebase Database Error: Permission denied)这个问题。
问题原因:如果在数据库控制台上没有显式授予用户的.read访问权限,则会被拒绝访问。
解决方法:前往数据库控制台的Rules选项卡。在需要访问权限被拒绝的节点上检查并使用Rules选项卡上的模拟器来测试不同用户安全上下文(非认证的、认证的等)的规则。
此外,Firebase文档中提供了一个非常好的链接,可以了解有关数据库安全的更多信息:https://firebase.google.com/docs/database/security/securing-data
在该页面上有两个特别重要的注释:
注意:默认情况下访问是禁止的。如果在路径上未指定.write或.read规则,访问将被拒绝。
注意:较浅的安全规则会覆盖较深路径上的规则。子规则只能授予父节点已声明的附加权限,不能撤销读取或写入权限。