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