小编典典

如何在Spring Boot REST Application中使用Firebase?

spring-boot

我有一个Spring Boot REST应用程序,它依赖于Firebase中完成的身份验证。在客户端,Firebase生成令牌,借此在Spring
Boot中,我需要验证uid。但是我注意到代码处于回调模式,因此如何实现Spring Boot函数,使其可以完成任务?

    @RequestMapping(value = "/api/restCall", method = RequestMethod.POST, consumes = "application/json", produces = "application/json")
    public Object restCall(@RequestBody Parameters requestBody) throws Exception {

    // idToken comes from the client app (shown above)
        String idToken = requestBody.getToken();

        Task<FirebaseToken> task = FirebaseAuth.getInstance().verifyIdToken(idToken)
            .addOnSuccessListener(new OnSuccessListener<FirebaseToken>() {
                @Override
                public void onSuccess(FirebaseToken decodedToken) {
                    String uid = decodedToken.getUid();
                    // process the code here
                }
        }); 
        // how do I return here, since the code is in the onSuccess?
        // do I return as a DeferredResult?

    }

阅读 610

收藏
2020-05-30

共1个答案

小编典典

这是我自己尝试回答我自己的问题

@RequestMapping(value = "/api/restCall", method = RequestMethod.POST, consumes = "application/json", produces = "application/json")
public Object restCall(@RequestBody Parameters requestBody,@RequestHeader(value = FIREBASETOKEN, required = true) String idToken) throws Exception {

    // idToken comes from the HTTP Header
    FirebaseToken decodedToken = FirebaseAuth.getInstance().verifyIdTokenAsync(idToken).get();
    final String uid = decodedToken.getUid();

    // process the code here
    // once it is done
    return object;

}

您也可以尝试以下代码

FirebaseAuth.getInstance().deleteUser(uid);
System.out.println("Successfully deleted user.");

获取更多详细信息URL
https://firebase.google.com/docs/auth/admin/manage-
users

2020-05-30