Google Java 中的 Cloud Endpoints 硬编码客户端 ID 和 useDatastoreForAdditionalConfig 的使用

Google Cloud Endpoints hard coded client id in Java and the use of useDatastoreForAdditionalConfig

我对 Google Cloud Endpoint API 中使用 Java.[=14 的客户端 ID 的硬编码性质有疑问=]

我们有多个项目的客户 ID 与特定项目相关联,并且发现我们必须创建项目特定的 GAE 工件 (WAR)。这是一个不太理想的情况,因为我们使用微服务架构,并且会出现人工制品的组合爆炸。

在尝试创建与环境无关的人工制品时,我们使用了 API 的一个记录不完整的功能,即 useDatastoreForAdditionalConfig 属性。

为了说明,而不是下面的:

@Api(
  name = "example", 
  version = "v1",
  scopes = { "example-scope" },
  clientIds = { "example-client-id" },
)

我们使用:

@Api( name = "example",
      version = "v1",
      useDatastoreForAdditionalConfig = AnnotationBoolean.TRUE
)

不过,我们听说此功能将在即将发布的版本中弃用。我的问题是,我们构建人工制品的方式是否有问题?此外,如果我们的构建过程没有任何问题,请 Google 将此视为一个问题,他们是否有任何计划在 Java 中创建项目不可知的 GAE 工件?

不幸的是,没有更好的方法来处理这种情况。 Google 文档清楚地提到了限制 here:

Note: Because the authorized clientIds must be specified at build time, you must rebuild and redeploy your API backend after adding or changing any client IDs in clientIds or audiences. (You must also update the regenerated jar file for your Android project for an Android client and regenerate the Objective-C library for iOS clients.)

您不能拥有带有外部化 clientId 的单个工件。但是,您可以使用 Maven replacer 插件自动更新 clientIds 的过程。

在@saiyr 的一些指导下,我们想出了解决这个问题的方法,并认为分享答案会有所帮助。

我们通过执行以下操作绕过了框架中的客户端 ID 断言:

import com.google.api.server.spi.Constant;
import com.google.api.server.spi.auth.GoogleOAuth2Authenticator;

@Api(
  name = "example",
  version = "v1",
  scopes = { Constant.API_EMAIL_SCOPE },
  clientIds = { Constant.SKIP_CLIENT_ID_CHECK },
  authenticators = { ClientIdAuthenticator.class,GoogleOAuth2Authenticator.class }
)

然后我们创建了一个自定义身份验证器,它负责客户端 ID 的编程断言:

public class ClientIdAuthenticator implements Authenticator { 
  @Override
  public User authenticate(HttpServletRequest httpServletRequest) {
    // Lookup config from cloud datastore for requestURI
    OAuthService service = OAuthServiceFactory.getOAuthService();
    String clientId = service.getClientId(Constant.API_EMAIL_SCOPE);
    // Assert clientId contained in datastore configuration
  }
}