Google Cloud终结了Java中的硬编码客户端ID以及useDatastoreForAdditionalConfig的使用

时间:2016-03-31 09:43:40

标签: java google-app-engine google-cloud-endpoints

我对使用Java的 Google Cloud Endpoint API中的客户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
)

但是我们听说这个功能将在即将发布的版本中弃用。我的问题是,我们建造人工制品的方式有问题吗?此外,如果我们的构建过程没有任何问题,谷歌是否认为这是一个问题,他们是否有计划在Java中创建项目无关的GAE文物?

2 个答案:

答案 0 :(得分:1)

在@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
  }
}

答案 1 :(得分:0)

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

  

注意:因为必须在构建时指定授权的clientIds   时间,您必须在添加或后重建和重新部署API后端   更改clientIds或受众中的任何客户端ID。 (你也必须   为您的Android项目更新重新生成的jar文件   Android客户端并重新生成iOS的Objective-C库   客户端。)

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