2016-11-27 56 views
3

我想爲我的java應用程序使用Firebase創建一個用戶身份驗證表單。連接到實時數據庫的依賴關係是可用的,並且Firebase Admin有充分的文件記錄here針對java應用程序的Firebase用戶身份驗證(不是Android)

但是目前Firebase Admin僅支持用戶驗證Node.js並且它記錄爲here

這是我的測試代碼。

public class Login { 
    private JPanel jPanel; 

    public static void main(String[] args) { 
//  Show My Form 
     JFrame jFrame = new JFrame("Login"); 
     jFrame.setContentPane(new Login().jPanel); 
     jFrame.pack(); 
     jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 
     jFrame.setVisible(true); 

//  Firebase 
     FirebaseOptions options = null; 
     try { 
      options = new FirebaseOptions.Builder() 
        .setServiceAccount(new FileInputStream("xxx.json")) 
        .setDatabaseUrl("https://xxx.firebaseio.com/") 
        .build(); 
     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } 

     if (options != null) { 
      FirebaseApp.initializeApp(options); 
      DatabaseReference ref = FirebaseDatabase.getInstance().getReference("Clip"); 
      ref.addValueEventListener(new ValueEventListener() { 
       public void onDataChange(DataSnapshot dataSnapshot) { 
        System.out.println("ClipText = [" + dataSnapshot.getValue() + "]"); 
       } 

       public void onCancelled(DatabaseError databaseError) { 

       } 
      }); 
     } 
    } 
} 

任何人都可以請指導我如何創建(例如使用電子郵件&密碼創建用戶,登錄)我的Java應用程序用戶認證?

注意:我正在使用Gradle。

回答

1

立足的REST API文檔放在這裏:https://firebase.google.com/docs/reference/rest/auth/ 我創造了這樣的一個單從電子郵件和密碼獲得一個有效的令牌,並使用getAccountInfo方法來驗證它:

import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.io.OutputStream; 
import java.io.OutputStreamWriter; 
import java.net.HttpURLConnection; 
import java.net.URL; 

import com.google.gson.JsonElement; 
import com.google.gson.JsonObject; 
import com.google.gson.JsonParser; 

public class FireBaseAuth { 

    private static final String BASE_URL = "https://www.googleapis.com/identitytoolkit/v3/relyingparty/"; 
    private static final String OPERATION_AUTH = "verifyPassword"; 
    private static final String OPERATION_REFRESH_TOKEN = "token"; 
    private static final String OPERATION_ACCOUNT_INFO = "getAccountInfo"; 

    private String firebaseKey; 

    private static FireBaseAuth instance = null; 

    protected FireBaseAuth() { 
     firebaseKey = "YOUR FIREBASE KEY HERE"; 
    } 

    public static FireBaseAuth getInstance() { 
     if(instance == null) { 
     instance = new FireBaseAuth(); 
     } 
     return instance; 
    } 

    public String auth(String username, String password) throws Exception { 

     HttpURLConnection urlRequest = null; 
     String token = null; 

     try { 
      URL url = new URL(BASE_URL+OPERATION_AUTH+"?key="+firebaseKey); 
      urlRequest = (HttpURLConnection) url.openConnection(); 
      urlRequest.setDoOutput(true); 
      urlRequest.setRequestProperty("Content-Type", "application/json; charset=UTF-8"); 
      OutputStream os = urlRequest.getOutputStream(); 
      OutputStreamWriter osw = new OutputStreamWriter(os, "UTF-8"); 
      osw.write("{\"email\":\""+username+"\",\"password\":\""+password+"\",\"returnSecureToken\":true}"); 
      osw.flush(); 
      osw.close(); 

      urlRequest.connect(); 

      JsonParser jp = new JsonParser(); //from gson 
      JsonElement root = jp.parse(new InputStreamReader((InputStream) urlRequest.getContent())); //Convert the input stream to a json element 
      JsonObject rootobj = root.getAsJsonObject(); //May be an array, may be an object. 

      token = rootobj.get("idToken").getAsString(); 

     } catch (Exception e) { 
      return null; 
     } finally { 
      urlRequest.disconnect(); 
     } 

     return token; 
    } 

    public String getAccountInfo(String token) throws Exception { 

     HttpURLConnection urlRequest = null; 
     String email = null; 

     try { 
      URL url = new URL(BASE_URL+OPERATION_ACCOUNT_INFO+"?key="+firebaseKey); 
      urlRequest = (HttpURLConnection) url.openConnection(); 
      urlRequest.setDoOutput(true); 
      urlRequest.setRequestProperty("Content-Type", "application/json; charset=UTF-8"); 
      OutputStream os = urlRequest.getOutputStream(); 
      OutputStreamWriter osw = new OutputStreamWriter(os, "UTF-8"); 
      osw.write("{\"idToken\":\""+token+"\"}"); 
      osw.flush(); 
      osw.close(); 
      urlRequest.connect(); 

      JsonParser jp = new JsonParser(); //from gson 
      JsonElement root = jp.parse(new InputStreamReader((InputStream) urlRequest.getContent())); //Convert the input stream to a json element 
      JsonObject rootobj = root.getAsJsonObject(); //May be an array, may be an object. 

      email = rootobj.get("users").getAsJsonArray().get(0).getAsJsonObject().get("email").getAsString(); 

     } catch (Exception e) { 
      return null; 
     } finally { 
      urlRequest.disconnect(); 
     } 

     return email; 

    } 

} 

這不是讓你用戶創建用戶但假設他們已經在Firebase上創建了