这个问题看起来很有趣。我也在研究。我提供了一些有用的链接。
https://jwt.io/ -- 您可以在此处找到适用于 Android 的第三方 JWT 库。因为 Firebase 使用基于 SON Web 令牌 (JWT) 的身份验证。
https://firebase.google.com/docs/auth/admin/verify-id-tokens#verify_id_tokens_using_a_third-party_jwt_library
我们无法在 Firebase Admin SDK 中创建自定义令牌。但我们可以从 Firebase 身份验证服务器创建自定义令牌。说明在这里:https://firebase.google.com/docs/auth/admin/create-custom-tokens
String uid = "some-uid";
HashMap<String, Object> additionalClaims = new HashMap<String, Object>();
additionalClaims.put("premiumAccount", true);
FirebaseAuth.getInstance().createCustomToken(uid, additionalClaims)
.addOnSuccessListener(new OnSuccessListener<String>() {
@Override
public void onSuccess(String customToken) {
// Send token back to client
/* Here, you get the custom token. You can simply store this token in String variable, and later pass as customToken */
}
});
此链接“https://github.com/jwtk/jjwt”提供用于创建自定义 JWT 令牌的 API。创建自定义令牌后,您可以使用
FirebaseAuth.getInstance().signInWithCustomToken(mCustomToken)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
// Sign in success, update UI with the signed-in user's information
Log.d(TAG, "signInWithCustomToken:success");
FirebaseUser user = mAuth.getCurrentUser();
updateUI(user);
} else {
// If sign in fails, display a message to the user.
Log.w(TAG, "signInWithCustomToken:failure", task.getException());
Toast.makeText(CustomAuthActivity.this, "Authentication failed.",
Toast.LENGTH_SHORT).show();
updateUI(null);
}
}
});
注意:我之前没有实现过这个。我是对此非常感兴趣的人之一。当你完全解决这个问题时,我会按照你的回答。