Spring Boot集成Firebase Messaging突发认证凭证无效问题求助
sendAll()时出现认证凭证无效错误 我已在Spring Boot项目中完成Firebase Messaging的配置,相关代码如下:
import com.google.auth.oauth2.GoogleCredentials; import com.google.firebase.FirebaseApp; import com.google.firebase.FirebaseOptions; import com.google.firebase.messaging.FirebaseMessaging; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import java.io.IOException; import java.io.InputStream; @Configuration public class FirebaseConfiguration { @Autowired FirebaseProperties firebaseProperties; @Bean GoogleCredentials googleCredentials() { try { if (firebaseProperties.getServiceAccount() != null) { try (InputStream is = firebaseProperties.getServiceAccount().getInputStream()) { return GoogleCredentials.fromStream(is); } } else { // Use standard credentials chain. Useful when running inside GKE return GoogleCredentials.getApplicationDefault(); } } catch (IOException ioe) { throw new RuntimeException(ioe); } } @Bean FirebaseApp firebaseApp(GoogleCredentials credentials) { FirebaseOptions options = FirebaseOptions.builder() .setCredentials(credentials) .build(); return FirebaseApp.initializeApp(options); } @Bean FirebaseMessaging firebaseMessaging(FirebaseApp firebaseApp) { return FirebaseMessaging.getInstance(firebaseApp); } }
pom.xml中的依赖配置:
<dependency> <groupId>com.google.firebase</groupId> <artifactId>firebase-admin</artifactId> <version>9.1.1</version> <scope>compile</scope> </dependency>
该配置一直运行正常,但今日突然出现报错,错误信息为:
Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential.
错误在调用FirebaseMessaging的sendAll()方法时触发,我并未修改任何配置,Firebase状态页面显示全部正常。
更新1:
经排查,该问题仅在使用本地Firebase Authentication Emulator时出现,我设置了环境变量:FIREBASE_AUTH_EMULATOR_HOST="my.computer.ip.address:9099"
原因分析
你使用的Firebase Admin SDK版本(9.1.1)存在已知问题:设置FIREBASE_AUTH_EMULATOR_HOST环境变量时,该配置会全局作用于所有Firebase服务,导致Firebase Messaging的请求被错误路由到Authentication模拟器。而Auth模拟器无法处理Messaging请求,因此返回"认证凭证无效"的错误。
修复方法
方法1:临时设置Auth模拟器环境变量,用完恢复
仅在执行Firebase Auth相关操作的代码块中设置环境变量,操作完成后立即恢复原有配置,避免干扰Messaging服务:
// 保存原有环境变量值 String originalEmulatorHost = System.getenv("FIREBASE_AUTH_EMULATOR_HOST"); try { // 临时设置Auth模拟器地址 System.setProperty("FIREBASE_AUTH_EMULATOR_HOST", "my.computer.ip.address:9099"); // 执行Firebase Auth相关操作(如用户认证、令牌验证等) } finally { // 恢复原有环境变量 if (originalEmulatorHost != null) { System.setProperty("FIREBASE_AUTH_EMULATOR_HOST", originalEmulatorHost); } else { System.clearProperty("FIREBASE_AUTH_EMULATOR_HOST"); } }
方法2:升级Firebase Admin SDK版本
Firebase团队在9.2.0及以上版本中修复了这个全局配置问题,将Auth模拟器的作用范围限制在Auth服务内,不会影响Messaging。修改pom.xml中的依赖版本:
<dependency> <groupId>com.google.firebase</groupId> <artifactId>firebase-admin</artifactId> <version>9.2.0</version> <!-- 可替换为更高稳定版本 --> <scope>compile</scope> </dependency>
方法3:通过FirebaseOptions单独配置Auth模拟器
在初始化FirebaseApp时,针对Auth服务单独配置模拟器地址,不使用全局环境变量,避免干扰其他服务:
@Bean FirebaseApp firebaseApp(GoogleCredentials credentials) { FirebaseOptions options = FirebaseOptions.builder() .setCredentials(credentials) .setAuthEmulatorHost("my.computer.ip.address:9099") // 仅对Auth服务生效 .build(); return FirebaseApp.initializeApp(options); }
内容的提问来源于stack exchange,提问作者wonderingdev

