2

语境

我在 Google AppEngine 灵活环境中有一些服务通过 API 进行通信,同时我使用 IAP 来管理用户访问。我必须使用 Bearer JWT 令牌以编程方式对每个服务进行身份验证。

问题

在我的 Java 应用程序中,我使用 google 提供的代码来验证 IAP 服务,您可以在此处找到:https ://cloud.google.com/iap/docs/authentication-howto#iap_make_request-java

问题出在这段代码中:

GoogleCredentials credentials =
    GoogleCredentials.getApplicationDefault().createScoped(Collections.singleton(IAM_SCOPE));
// service account credentials are required to sign the jwt token
if (credentials == null || !(credentials instanceof ServiceAccountCredentials)) {
  throw new Exception("Google credentials : service accounts credentials expected");
}
return (ServiceAccountCredentials) credentials;

createScoped方法正在返回一段ComputeEngineCredentials时间,代码需要一个ServiceAccountCredentials响应对象。

欢迎所有建议。提前感谢有史以来最伟大的社区。

4

1 回答 1

0

你实际上并没有提到你所面临的困难的性质。App Engine 柔性环境服务帐号是在 GCP 项目中自动创建的。您尝试创建的 App Engine 柔性环境服务帐户是独立于App Engine 默认服务帐户的服务帐户。“返回 (ServiceAccountCredentials) 凭据;” 语句使用强制转换返回 ServiceAccountCredentials;应该没问题。

行:GoogleCredentials.getApplicationDefault().createScoped(Collections.singleton(IAM_SCOPE) 创建一个 GoogleCredentials 对象,它不需要 JSON 服务帐户文件即可成功。

于 2018-10-01T21:22:39.110 回答