将 Cloud Endpoints API Explorer 锁定到特定的 Gmail 帐户

Lock down Cloud Endpoints API Explorer to a specific Gmail account

如何锁定 Cloud Endpoints 中的 API 资源管理器,使其仅适用于特定的 Gmail 帐户(例如您的 console.developers.google.com 电子邮件)?

有人告诉我可以在我的 Cloud Endpoints 项目的 web.xml 文件中使用安全约束,如下所示:

<security-constraint>
        <web-resource-collection>
           <web-resource-name>admin</web-resource-name>
            <url-pattern>/admin/*</url-pattern>
        </web-resource-collection>
        <auth-constraint>
            <role-name>admin</role-name>
        </auth-constraint>
    </security-constraint>

但在 Using Auth with Endpoints docs 中它说:

"You must specify Endpoints auth following the directions provided on this page. Note that you cannot set a user login requirement following the instructions provided under Security and Authentication to configure the web.xml file, because this will result in a deployment failure."

所以你不能那样做...

有人知道如何在 API 资源管理器中将 Oauth2 身份验证限制为特定的 gmail 帐户吗?因为默认情况下,您可以使用 any gmail 帐户登录,这将使您通过 Oauth2 身份验证,然后您可以执行任何使用 Oauth2 和 com.google.appengine.api.users.User 的 API 方法他们方法中的用户参数。

如果您真的想停止阻止除一封电子邮件外的其他操作,使用 "Users API" 可以让您在代码中获取经过身份验证的用户的电子邮件。

之后,检查该电子邮件是否是您授权的唯一电子邮件,这实际上会阻止其他人访问它。

您应该能够检索用户的身份验证令牌并验证其客户端 ID 是否是 API Explorer 客户端 ID。客户端 ID 在 com.google.api.server.spi.Constant.API_EXPLORER_CLIENT_ID 中。身份验证令牌将通过 Authorization header 或 access_tokenbearer_token 查询参数传递。通过将 HttpServletRequest 参数添加到 API 方法,可以访问 header 或查询参数。

要验证 ID 令牌,您需要使用 Google 的 API 客户端库:

GoogleIdTokenVerifier verifier = new GoogleIdTokenVerifier.Builder(
    Client.getInstance().getHttpTransport(),
    Client.getInstance().getJsonFactory()).build();
GoogleIdToken token = verifier.verify(stringToken);
String clientId = token.getPayload().getAuthorizedParty();

请参阅 Cloud Endpoints: Control who can execute API through API Explorer 以了解限制谁可以通过 API Explorer 执行您的 API 的方法。