Spring Cloud GCP 憑證

2024-01-09 17:31 更新

CredentialsProvider是一個功能接口,可返回憑據(jù)以認證和授權(quán)對Google Cloud Client庫的調(diào)用。

public interface CredentialsProvider {
  Credentials getCredentials() throws IOException;
}

Spring Cloud GCP啟動器會自動配置CredentialsProvider。它使用spring.cloud.gcp.credentials.location屬性找到Google服務(wù)帳戶的OAuth2私鑰。請記住,此屬性是Spring資源,因此可以從許多 不同的位置( 例如文件系統(tǒng),類路徑,URL等)獲取憑證文件。下一個示例在文件系統(tǒng)中指定憑證位置屬性。

spring.cloud.gcp.credentials.location=file:/usr/local/key.json

或者,您可以通過直接指定spring.cloud.gcp.credentials.encoded-key屬性來設(shè)置憑據(jù)。該值應(yīng)為JSON格式的base64編碼的帳戶私鑰。

如果未通過屬性指定憑據(jù),則啟動程序?qū)L試從許多地方發(fā)現(xiàn)憑據(jù)

  1. GOOGLE_APPLICATION_CREDENTIALS環(huán)境變量指向的憑據(jù)文件
  2. Google Cloud SDK gcloud auth application-default login命令提供的憑據(jù)
  3. Google App Engine內(nèi)置憑據(jù)
  4. Google Cloud Shell內(nèi)置憑據(jù)
  5. Google Compute Engine內(nèi)置憑據(jù)

如果您的應(yīng)用程序在Google App Engine或Google Compute Engine上運行,則在大多數(shù)情況下,您應(yīng)該省略spring.cloud.gcp.credentials.location屬性,而應(yīng)讓Spring Cloud GCP Starter獲得那些環(huán)境的正確憑據(jù)。在App Engine Standard上,使用 App Identity服務(wù)帳戶憑據(jù) ;在App Engine Flexible上,使用Flexible服務(wù)帳戶憑據(jù);在Google Compute Engine上,使用Compute Engine默認服務(wù)帳戶。

以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號