You need to enable JavaScript to run this app. 无ServiceAccount时Spring Boot(Kotlin)用第三方配置初始化Firebase Admin SDK
优惠活动
大模型
产品
解决方案
定价
更多

无ServiceAccount时Spring Boot(Kotlin)用第三方配置初始化Firebase Admin SDK

核心实现前提
  • 明确权限边界:没有ServiceAccount凭证时,无法使用firebase-admin组件的高权限初始化逻辑,必须改用Firebase客户端SDK,模拟Web/移动端的公开访问流程对接。
  • 访问权限依赖第三方配置的Firebase安全规则:只要规则放开了目标数据节点的匿名/公开读权限,仅靠提供的前端格式配置即可完成数据拉取,和你运行在Spring Boot服务端还是前端环境无关。
  • 不要尝试通过拼接参数伪造ServiceAccount凭证,这类调用会直接被Google鉴权层拦截。
具体实现步骤(Spring Boot + Kotlin)

1. 引入对应依赖

不要引入需要ServiceAccount的firebase-admin包,在项目的build.gradle.kts中添加Firebase客户端SDK相关依赖:

dependencies {
    // 协程对Firebase异步任务的适配支持
    implementation("org.jetbrains.kotlinx:kotlinx-coroutines-play-services:1.7.3")
    // Realtime Database 客户端SDK,初始化无需ServiceAccount
    implementation("com.google.firebase:firebase-database:20.3.0")
    implementation("com.google.firebase:firebase-core:21.1.1")
    // 若对接的是Firestore,可替换为对应客户端包
    // implementation("com.google.firebase:firebase-firestore:24.10.0")
}

2. 配置参数映射

将拿到的firebaseConfig参数配置到Spring Boot的application.yml中,避免硬编码:

firebase:
  api-key: 提供的apiKey值
  auth-domain: 提供的authDomain值
  database-url: 提供的databaseURL值
  project-id: 提供的projectId值
  storage-bucket: 提供的storageBucket值
  messaging-sender-id: 提供的messagingSenderId值
  app-id: 提供的appId值
  measurement-id: 提供的measurementId值

编写对应的配置绑定类:

@ConfigurationProperties(prefix = "firebase")
data class FirebaseClientProperties(
    val apiKey: String,
    val authDomain: String,
    val databaseUrl: String,
    val projectId: String,
    val storageBucket: String,
    val messagingSenderId: String,
    val appId: String,
    val measurementId: String
)

在启动类上开启配置绑定支持:

@SpringBootApplication
@EnableConfigurationProperties(FirebaseClientProperties::class)
class Application
fun main(args: Array<String>) = runApplication<Application>(*args)

3. 无ServiceAccount的SDK初始化

编写Spring配置类,用客户端模式完成SDK初始化,全程不需要读取ServiceAccount.json文件:

@Configuration
class FirebaseClientConfig(
    private val properties: FirebaseClientProperties
) {
    @Bean
    fun firebaseApp(): FirebaseApp {
        // 构建和前端逻辑完全一致的初始化选项,不设置任何ServiceAccount凭证
        val options = FirebaseOptions.builder()
            .setApiKey(properties.apiKey)
            .setApplicationId(properties.appId)
            .setDatabaseUrl(properties.databaseUrl)
            .setProjectId(properties.projectId)
            .setStorageBucket(properties.storageBucket)
            .build()
        // 初始化客户端实例
        return FirebaseApp.initializeApp(options)
    }

    @Bean
    fun databaseReference(firebaseApp: FirebaseApp): DatabaseReference {
        // 获取根节点引用,供业务层注入使用
        return FirebaseDatabase.getInstance(firebaseApp).reference
    }
}

4. 业务层数据读取

在需要拉取数据的Service中直接注入DatabaseReference,按照客户端SDK的规则读取数据即可:

@Service
class FirebaseDataService(
    private val dbRef: DatabaseReference
) {
    // 示例:拉取指定路径节点下的数据
    suspend fun getNodeData(path: String): Any? {
        val dataRef = dbRef.child(path)
        val task = dataRef.get()
        // 通过协程适配获取异步任务返回值
        return task.await().value
    }
}
常见注意事项
  • 若调用时报401/403权限错误,基本是第三方配置的Firebase安全规则没有放开当前调用的公开权限,和代码逻辑无关,需要联系数据所有者调整规则后再测试
  • 客户端SDK的调用本质是走Firebase的公开REST接口,调用频次会受Firebase免费层的匿名请求配额限制,不要用这种方式承载高频生产业务
  • 如果需要在调用时带上已认证的用户身份,可在初始化时传入对应的Firebase ID Token,依然不需要ServiceAccount凭证

内容的提问来源于stack exchange,提问作者Hansen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.11.19 00:36:03