簡體   English   中英

如何將firebase身份驗證與Google App引擎端點集成

[英]How to integrate firebase authentication with google app engine endpoints

我正在為移動應用程序編寫后端服務器。 后端在谷歌應用引擎上運行,用Java編寫。

我希望用戶能夠使用Facebook等聯合身份登錄。

我看到谷歌通過firebase身份驗證為移動應用程序支持這種身份驗證。 將firebase身份驗證與我當前的應用引擎端點集成的最佳方法是什么?

我已經使用了雲平台的數據存儲區,並且不希望使用firebase數據庫,只使用身份驗證方法。

謝謝。

我也在尋找答案。 到目前為止,我最好的5c是

  • 使用FireBase從控制台設置登錄方法等
  • 使用適用於Web的FireBase UI(測試版)或iOS / Android的“聯合身份提供程序集成”來設置身份驗證流程
  • 在Web / iOS / Android客戶端上檢索令牌/身份驗證詳細信息,並將其傳遞到您的Cloud端點,例如,HTTP請求標頭
  • 將javax.servlet.http.HttpServletRequest注入到端點方法中(只需添加一個參數,Google就會自動注入請求對象)
  • 創建一個Endpoint將為每個請求(需要身份驗證)調用的方法,該方法將處理您作為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");
        }
    }

您應該可以在應用前使用Google Cloud Endpoints作為身份驗證代理。 端點支持通過配置OpenAPI模板驗證Firebase身份驗證令牌

# 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編寫驗證令牌的身份驗證中間件:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM