Java 如何将 Firebase 与 Spring Boot REST 应用程序一起使用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39183107/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 20:56:15  来源:igfitidea点击:

How to use Firebase with Spring boot REST Application?

javarestfirebasespring-bootfirebase-authentication

提问by dickyj

I have a Spring Boot REST application that depends on the authentication done in Firebase. On the client side Firebase generates a token whereby in the Spring Boot, I need to verify the uid. But I noticed that the code is in a callback mode, so how do I implement the Spring Boot function so that it can finish the task?

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

    }

采纳答案by dickyj

Here is my own attempt to answer my own question

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

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

}

You can try below code as well

你也可以试试下面的代码

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

for More deetails URLhttps://firebase.google.com/docs/auth/admin/manage-users

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

回答by Maheshwar Ligade

To integrate Firebase with Spring, below is the sample code

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

In new Admin SDK the process is simple just use below code snippet.

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

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

For more detail visit this URL https://firebase.google.com/docs/auth/admin/manage-users

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

This is for a legacy code. First add Firbase dependency

这是针对遗留代码的。首先添加Firbase依赖

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

Sample Code

示例代码

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

}

For Complete example please gone through github below link https://github.com/savicprvoslav/Spring-Boot-starter

对于完整示例,请通过以下链接https://github.com/savicprvoslav/Spring-Boot-starter 的github