Android Firebase数据库错误:权限被拒绝
我正在从事一个需要用户电子邮件和pwd认证的android项目。详细信息存储在firebase数据库中。每当我尝试使用电子邮件和密码再次登录时,就会出现此问题。在我的logcat中,错误消息是:Android Firebase数据库错误:权限被拒绝,android,firebase,firebase-realtime-database,firebase-authentication,Android,Firebase,Firebase Realtime Database,Firebase Authentication,我正在从事一个需要用户电子邮件和pwd认证的android项目。详细信息存储在firebase数据库中。每当我尝试使用电子邮件和密码再次登录时,就会出现此问题。在我的logcat中,错误消息是: W/SyncTree: Listen at / failed: DatabaseError: Permission denied 请看下面我的代码: public class LoginUser extends AppCompatActivity { private RelativeLayout r
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<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> 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) {
}
});
}
公共类登录用户扩展AppCompative活动{
私人亲戚;
私人编辑文本等电子邮件,等密码;
私人按钮登录;
私人消防队;
私有FirebaseAuth.AuthStateListener AuthStateListener;
私有数据库参考数据库参考;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u login\u user);
mAuth=FirebaseAuth.getInstance();
databaseReference=FirebaseDatabase.getInstance().getReference();
databaseReference.keepSynced(true);
relativeLayout=(relativeLayout)findViewById(R.id.activity\u login\u user);
et_email=(EditText)findViewById(R.id.emailField);
et_password=(EditText)findViewById(R.id.pwdField);
loginBtn=(按钮)findViewById(R.id.loginBtn);
loginBtn.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
initLogin();
}
});
authStateListener=新的FirebaseAuth.authStateListener(){
@凌驾
AuthStateChanged上的公共void(@NonNull FirebaseAuth FirebaseAuth){
如果(firebaseAuth.getCurrentUser()!=null){
initLogin();
}
否则{
startActivity(新的意图(logiuser.this,RegisterFireBase.class));
}
}
};
}
@凌驾
受保护的void onStart(){
super.onStart();
mAuth.addAuthStateListener(authStateListener);
}
@凌驾
受保护的void onStop(){
super.onStop();
if(mAuth!=null){
mAuth.removeAuthStateListener(authStateListener);
}
}
私有void initLogin(){
字符串email=et_email.getText().toString().trim();
String pass=et_password.getText().toString().trim();
如果(!TextUtils.isEmpty(电子邮件)和&!TextUtils.isEmpty(通过)){
mAuth.signInWithEmailAndPassword(email,pass).addOnCompleteListener(这是新的OnCompleteListener()){
@凌驾
未完成的公共void(@NonNull任务){
checkForUser();
}
});
}
否则{
Toast.makeText(这是“某些字段为空”,Toast.LENGTH_SHORT.show();
}
}
私有void checkForUser(){
最后一个字符串userId=mAuth.getCurrentUser().getUid();
databaseReference.addValueEventListener(新的ValueEventListener(){
@凌驾
公共void onDataChange(DataSnapshot DataSnapshot){
if(dataSnapshot.hasChild(userId)){
Intent loginIntent=新Intent(logiuser.this,firerprofile.class);
登录。添加标志(意图。标志\活动\清除\顶部);
startActivity(登录);
Snackbar.make(relativeLayout,“登录成功”,Snackbar.LENGTH_LONG.show();
}
}
@凌驾
已取消的公共void(DatabaseError DatabaseError){
}
});
}
}
什么原因可能导致这种情况?可能的原因是:您没有对数据库的读写访问权限。 要启用读写访问,请执行以下操作:
转到firebase控制台并在数据库上启用读写操作 Firebase控制台->数据库(开发)->规则
转到数据库控制台上的规则选项卡。如果您尚未明确授予用户.read访问权限,则权限将被拒绝 Firebase文档中的此链接非常棒: 该页上的这两个注释特别有趣: 注意:默认情况下不允许访问。如果在路径或路径上方未指定.write或.read规则,则访问将被拒绝 注意:较浅的安全规则覆盖较深路径的规则。子规则只能向父节点已声明的内容授予附加权限。它们不能撤消读或写权限
查看权限被拒绝的节点,并使用规则选项卡上的模拟器测试不同用户安全上下文(未验证、已验证等)的规则。对firebase数据库进行一些更改
如果不需要,请不要将应用程序公开。 如谷歌文档所述,您可以在firebase>数据库>规则上执行这些规则
// These rules grant access to a node matching the authenticated
// user's ID from the Firebase auth token
{
"rules": {
"users": {
"$uid": {
".read": "$uid === auth.uid",
".write": "$uid === auth.uid"
}
}
}
}
或者只允许经过身份验证的用户
// These rules require authentication
{
"rules": {
".read": "auth != null",
".write": "auth != null"
}
}
公开应用程序允许任何人编写和阅读你的应用程序。。。我认为任何应用程序都不应该这样使用它 问题在于firebase数据库有两个同名项目,其中一个项目的规则甚至没有启用
请尝试更改firebase规则,如下所示。我以前遇到过这个问题。 我的问题在于数据库规则:
{
“规则”:{
“.read”:“true”,
.write:“正确”
}
}
转到firebase控制台,启用对数据库的读写操作
Firebase控制台->数据库(开发)->规则
rules_version = '2';
service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
allow read, write: if request.auth != null;
}
}
}
如果您使用的是旧版本,请添加以下规则
{
"rules": {
".read": "true",
".write": "true"
}
}
大多数答案只是建议让任何人都可以访问数据库进行阅读和编辑。这对于粗略的测试来说是可以接受的,但对于任何严重的测试来说肯定不是 Google Firebase允许配置允许和拒绝访问。请阅读此处的官方文档:
(确保选择正确类型的Firebase数据库) 对于任何需要身份验证的内容,您都需要设置Firebase身份验证: 以下是一些基本示例: 无访问权限(默认) 仅限经过身份验证的用户 只读公共,仅由所有者写入
除了错误之外,我们没什么可说的了
rules_version = '2';
service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
allow read, write: if request.auth != null;
}
}
}
{
"rules": {
".read": "true",
".write": "true"
}
}
{
"rules": {
".read": false,
".write": false
}
}
{
"rules": {
".read": "auth.uid != null",
".write": "auth.uid != null"
}
}
{
// Allow anyone to read data, but only authenticated content owners can
// make changes to their data
"rules": {
"some_path": {
"$uid": {
".read": true,
// or ".read": "auth.uid != null" for only authenticated users
".write": "auth.uid == $uid"
}
}
}
}