如何在Spring Boot REST应用程序中使用Firebase?

dic*_*kyj 23 java rest firebase spring-boot firebase-authentication

我有一个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? 

    }
Run Code Online (Sandbox Code Playgroud)

Mah*_*ade 18

以下是将Spring集成到Firebase的示例代码

在新的Admin SDK中,只需在代码片段下方使用该过程即可.

FirebaseAuth.getInstance().deleteUser(uid);
System.out.println("Successfully deleted user.");
Run Code Online (Sandbox Code Playgroud)

有关详细信息,请访问以下网址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>
Run Code Online (Sandbox Code Playgroud)

示例代码

@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;
    }

}
Run Code Online (Sandbox Code Playgroud)

有关完整示例,请访问下面的链接 https://github.com/savicprvoslav/Spring-Boot-starter

  • 现在不推荐使用此API,而是使用新的Firebase Admin-SDK.您可能想要更新代码. (2认同)

dic*_*kyj 9

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

@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;

}
Run Code Online (Sandbox Code Playgroud)

  • 这不是春天的方式. (5认同)