我对使用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文物?
答案 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)