如何以编程方式对订阅者进行身份验证并为 Spring 云传入应用程序凭据 json

How to programmatically authenticate Subscriber and pass in application credentials json for Spring Cloud

我在 Java 中有一个使用 spring-cloud-gcp-starter-pubsub 的订户应用程序,但我不想使用 [=] 向 Google 云进行身份验证21=] 文件系统上的文件;我们正在从不同的安全位置(不在磁盘上)获取文件,并希望以编程方式对订阅者进行身份验证。

目前我们的应用程序依赖于标准的 Spring Cloud magic,它会根据磁盘上的 JSON 文件自动进行身份验证;我们使用默认创建的 PubSubSubscriberTemplate 创建订阅者:

@Bean
public Subscriber subscriber(PubSubSubscriberTemplate template) {
    return template.subscribeAndConvert(...);
}

假设应用程序已经从安全的外部来源检索到 json 凭据,我们如何以编程方式对订阅者进行身份验证?

提供一个实现 CredentialsProvider 接口的 bean——它将在需要凭据的地方进行延迟评估。

参考文档:https://googlecloudplatform.github.io/spring-cloud-gcp/3.2.1/reference/html/index.html#credentials