如何将 Firebase 与 Spring Boot REST 应用程序配合使用?

我有一个Spring Boot REST应用程序,它依赖于在Firebase中完成的身份验证。

在客户端,Firebase会生成一个令牌,在Spring Boot中,我需要验证.UID

但是代码处于回调模式,那么我如何实现函数以便它可以完成任务?

@RequestMapping(value = "/api/restCall", method = RequestMethod.POST, 
             consumes = "application/json", produces = "application/json")
public Object restCall(@RequestBody Parameters requestBody) throws Exception {
    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();
                }
            });
    return "???"; // what return here?
}

我该如何返回 ? ?onSuccessDeferredResult


答案 1

要将Firebase与Spring集成,下面是示例代码

在新的 Admin SDK 中,只需使用以下代码片段,该过程就很简单。

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

有关更多详细信息,请访问此 URL https://firebase.google.com/docs/auth/admin/manage-users

这适用于旧代码。首先添加 Firbase 依赖项

<dependency>
    <groupId>com.google.firebase</groupId>
    <artifactId>firebase-server-sdk</artifactId>
    <version>3.0.1</version>
</dependency>

示例代码

@Component
public class FirebaseAuthenticationProvider implements AuthenticationProvider {

    @Autowired
    @Qualifier(value = UserServiceImpl.NAME)
    private UserDetailsService userService;

    public boolean supports(Class<?> authentication) {
        return (FirebaseAuthenticationToken.class.isAssignableFrom(authentication));
    }

    public Authentication authenticate(Authentication authentication) throws AuthenticationException {
        if (!supports(authentication.getClass())) {
            return null;
        }

        FirebaseAuthenticationToken authenticationToken = (FirebaseAuthenticationToken) authentication;
        UserDetails details = userService.loadUserByUsername(authenticationToken.getName());
        if (details == null) {
            throw new FirebaseUserNotExistsException();
        }

        authenticationToken = new FirebaseAuthenticationToken(details, authentication.getCredentials(),
                details.getAuthorities());

        return authenticationToken;
    }

}

对于完整的示例,请通过github下面的链接 https://github.com/savicprvoslav/Spring-Boot-starter 完整的BlogPost与CRUD操作:https://medium.com/techwasti/spring-boot-firebase-crud-b0afab27b26e


答案 2

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

@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.");

更多 deetails URL https://firebase.google.com/docs/auth/admin/manage-users


推荐