【发布时间】:2021-05-04 22:18:02
【问题描述】:
当我尝试在 Spring Boot 后端应用程序中验证 Firebase jwt 令牌时,我收到以下错误:
无法验证 Firebase ID 令牌的签名。看 https://firebase.google.com/docs/auth/admin/verify-id-tokens 为 有关如何检索 ID 令牌的详细信息。
在客户端 (Flutter) 中,我按如下方式记录 jwt:
GoogleSignInAccount googleSignInAccount = await _googleSignIn.signIn();
GoogleSignInAuthentication googleSignInAuthentication = await googleSignInAccount.authentication;
AuthCredential credential = GoogleAuthProvider.credential(
accessToken: googleSignInAuthentication.accessToken,
idToken: googleSignInAuthentication.idToken,
);
UserCredential authResult = await _auth.signInWithCredential(credential);
_user = authResult.user;
logger.i(await _user.getIdToken()); // Print jwt
我通过授权标头将记录的 jwt 作为不记名令牌发送到我的后端。
使用Spring安全(没关系),我只执行以下检查:
FirebaseToken decoded = FirebaseAuth.getInstance().verifyIdToken(token);
我的 firebase 应用程序初始化配置非常标准(设置了指向 config.json 的环境变量):
@Primary
@Bean
public void firebaseInit() throws IOException {
FirebaseOptions options = FirebaseOptions.builder()
.setCredentials(GoogleCredentials.getApplicationDefault())
.build();
if (FirebaseApp.getApps().isEmpty()) {
FirebaseApp.initializeApp(options);
}
}
调试后,RSASignature类(包sun.security.rsa)中抛出如下方法:
@Override
protected boolean engineVerify(byte[] sigBytes) throws SignatureException {
if (publicKey == null) {
throw new SignatureException("Missing public key");
}
try {
if (sigBytes.length != RSACore.getByteLength(publicKey)) {
throw new SignatureException("Signature length not correct: got " +
sigBytes.length + " but was expecting " +
RSACore.getByteLength(publicKey));
}
sigBytes 长度为 113,而预期为 256。
也许我做错了什么......
【问题讨论】:
标签: java firebase spring-boot flutter firebase-authentication