Spring Boot中FirebaseAuth的signInWithEmailAndPassword方法未定义问题咨询
我想要验证给定邮箱和密码的Firebase用户密码是否正确,原以为最简单的方式是执行登录操作(若有其他方法请告知)。目前我可以通过以下代码访问项目及用户UID等信息:
FirebaseAuth mAuth = FirebaseAuth.getInstance(); UserRecord test = mAuth.getUserByEmail(email);
但当我尝试调用:
mAuth.signInWithEmailAndPassword(email, password) .addOnCompleteListener...
时,出现错误提示:The method signInWithEmailAndPassword(String, String) is undefined for the type FirebaseAuthJava(67108964)。根据文档,该方法应该是有效的。
我的导入语句如下:
import com.google.firebase.FirebaseApp; import com.google.firebase.FirebaseOptions; import com.google.firebase.auth.FirebaseAuth; import com.google.firebase.auth.FirebaseAuthException; import com.google.firebase.auth.UserRecord;
pom.xml中仅引入了以下Firebase依赖:
<dependency> <groupId>com.google.firebase</groupId> <artifactId>firebase-admin</artifactId> <version>6.2.0</version> </dependency>
补充说明
我已了解无法通过Firebase Admin实现此需求,请问是否有其他可用的SDK来达成目标?
核心原因
你用的是Firebase Admin SDK,这是服务端专属工具,主打以管理员权限操作Firebase资源(比如批量管理用户、读写数据库),根本不提供客户端的用户登录/密码验证方法。signInWithEmailAndPassword是Firebase客户端SDK(Android、iOS、Web、Java客户端版)的专属方法,所以在Admin SDK里找不到它。
可行方案
1. 切换到Firebase客户端Java SDK
如果你的代码运行在客户端环境(桌面APP、嵌入式设备等),可以引入Firebase客户端的Auth SDK,替换掉Admin依赖:
<dependency> <groupId>com.google.firebase</groupId> <artifactId>firebase-auth</artifactId> <version>22.3.1</version> <!-- 建议用最新稳定版 --> </dependency>
引入后就能正常调用signInWithEmailAndPassword,通过登录成功与否判断密码是否正确。
2. 调用Firebase Auth REST API
如果你的代码跑在服务端,不想引入客户端SDK,直接用REST接口验证更合适:
发送POST请求到 https://identitytoolkit.googleapis.com/v1/accounts:signInWithPassword?key=你的Firebase项目API_KEY,请求体JSON格式如下:
{ "email": "待验证的用户邮箱", "password": "待验证的密码", "returnSecureToken": true }
- 密码正确时:返回包含
idToken、localId的用户信息响应; - 密码错误/邮箱不存在时:返回对应错误码(比如
INVALID_PASSWORD、EMAIL_NOT_FOUND)。
内容的提问来源于stack exchange,提问作者Matt102065

