【问题标题】:How to integrate firebase authentication with google app engine endpoints如何将 Firebase 身份验证与谷歌应用引擎端点集成
【发布时间】:2016-05-19 18:07:16
【问题描述】:

我正在为移动应用程序编写后端服务器。 后端在谷歌应用引擎上运行并用 Java 编写。

我希望用户能够使用联合身份登录,例如 facebook。

我看到谷歌通过 Firebase 身份验证支持移动应用的这种身份验证。将 Firebase 身份验证与我当前的应用引擎端点集成的最佳方式是什么?

我已经使用云平台的数据存储,不想使用firebase数据库,只使用身份验证方法。

谢谢。

【问题讨论】:

标签: android google-app-engine firebase firebase-authentication


【解决方案1】:

我也在寻找这个问题的答案。到目前为止,我最好的 5c 是

  • 使用 FireBase 从控制台设置登录方法等
  • 使用适用于 Web 的 FireBase UI(测试版)或适用于 iOS/Android 的“联合身份提供程序集成”来设置身份验证流程
  • 在您的 Web/iOS/Android 客户端上检索令牌/身份验证详细信息,并将其作为例如 HTTP 请求标头传递给您的云端点
  • 将 javax.servlet.http.HttpServletRequest 注入您的端点方法(只需添加一个参数,Google 就会自动注入请求对象)
  • 创建一个您的端点将为每个请求(需要身份验证)调用的方法,该方法将处理您作为 HTTP 请求标头传递的凭据的验证
  • 使用 FireBase Java SDK 调用 FireBase 以验证凭据(为此,您需要从 Firebase 控制台导出 json 配置)并使用它们加载 SDK,例如,在您的一个 servlet 中:李>

@Override
    public void init(ServletConfig config) {
        try{
        InputStream in = config.getServletContext().getResourceAsStream("/WEB-INF/firebase-privatekey.json");
        FirebaseOptions options = new FirebaseOptions.Builder()
                .setServiceAccount(in)
                .setDatabaseUrl("YOUR_DATABASE_URL")
                .build();
        FirebaseApp.initializeApp(options);
        log.info("Authentication enabled");
        }
        catch(Throwable t) {
            t.printStackTrace();
            log.warning("AUTHENTICATION DISABLED. Only public resources will be available");
        }
    }

【讨论】:

【解决方案2】:

您应该能够在您的应用程序前使用Google Cloud Endpoints 作为身份验证代理。端点supports validating Firebase Authentication tokens 通过配置您的 OpenAPI 模板:

# Configure Firebase as an AuthN provider
securityDefinitions:
    firebase:
      authorizationUrl: ""
      flow: "implicit"
      type: "oauth2"
      # Replace YOUR-PROJECT-ID with your project ID in the issuer and audiences fields
      x-google-issuer: "https://securetoken.google.com/YOUR-PROJECT-ID"
      x-google-audiences: "YOUR-PROJECT-ID"
      x-google-jwks_uri: "https://www.googleapis.com/service_accounts/v1/metadata/x509/securetoken@system.gserviceaccount.com"

# Add Firebase as an authN provider to specific endpoints...
security:
  - firebase: []

或者,您可以使用Firebase Admin SDK 编写validates your tokens 的身份验证中间件:

FirebaseAuth.getInstance().verifyIdToken(idToken)
    .addOnSuccessListener(new OnSuccessListener<FirebaseToken>() {
        @Override
        public void onSuccess(FirebaseToken decodedToken) {
            String uid = decodedToken.getUid();
            // ...
        }
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-13
    • 1970-01-01
    • 1970-01-01
    • 2018-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    • 2021-12-20
    相关资源
    最近更新 更多