Firebase:无法验证 Firebase ID 令牌的签名

Tra*_*ace 5 java firebase spring-boot firebase-authentication flutter

当我尝试在 Spring Boot 后端应用程序中验证 Firebase jwt 令牌时,出现以下错误:

无法验证 Firebase ID 令牌的签名。有关如何检索 ID 令牌的详细信息,请参阅 https://firebase.google.com/docs/auth/admin/verify-id-tokens 。

在客户端(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
Run Code Online (Sandbox Code Playgroud)

我通过授权标头将记录到后端的 jwt 作为不记名令牌发送。

使用 Spring security(没关系),我只需执行以下检查:

FirebaseToken decoded = FirebaseAuth.getInstance().verifyIdToken(token);
Run Code Online (Sandbox Code Playgroud)

我的 firebase 应用程序初始化配置非常标准(设置了指向 config.json 的 env 变量):

@Primary
@Bean
public void firebaseInit() throws IOException {
    FirebaseOptions options = FirebaseOptions.builder()
            .setCredentials(GoogleCredentials.getApplicationDefault())
            .build();
    if (FirebaseApp.getApps().isEmpty()) {
        FirebaseApp.initializeApp(options);
    }
}
Run Code Online (Sandbox Code Playgroud)

调试后,类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));
        }
Run Code Online (Sandbox Code Playgroud)

sigBytes 长度是 113,而预期是 256。
也许我做错了什么......

Tra*_*ace 4

天哪...我在 dart 中使用的记录器决定只限制 jwt 字符串,因此 jwt 不完整。
现在我收到一条消息“禁止”,高兴极了。但之前的错误已经解决了。

编辑“禁止”是 Spring Boot 的一个小问题(向权限添加角色)的结果。
现在它按预期工作。