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

标签 java rest firebase spring-boot firebase-authentication

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

在客户端 Firebase 生成一个 token ,在 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?
}

onSuccess 之后如何返回? DeferredResult?

最佳答案

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

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

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

欲了解更多详情,请访问此网址 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 使用 CRUD 操作完成 BlogPost: https://medium.com/techwasti/spring-boot-firebase-crud-b0afab27b26e

关于java - 如何将 Firebase 与 Spring Boot REST 应用程序一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39183107/

相关文章:

java - 是什么让模型更改通过 JFace 数据绑定(bind)传播?

java - 如何让 main 方法等待 Swing 计时器完成执行?

python - 通过 REST API 查询 JIRA 以及查询中可能存在错误值

rest - flutter http post “type ' int'不是类型转换中 'String'类型的子类型”

ruby-on-rails - 我正在使用 `fcm` gem 发送推送通知,它适用于 Android,但不适用于 IOS

java - 如何等待后台进程在android中完成

android - 在 Android 的 webservice Rest 中读取 Json。 FileNotFoundException异常

firebase - Flutter Firebase 性能 - 未处理的异常

ios - 如何在 Xcode 中重命名 Firebase 'User' 对象?

Java 客户端与两台服务器一起工作