【发布时间】:2017-06-20 02:44:49
【问题描述】:
我正在使用 Firebase 通过 Spring Boot 和 Thymeleaf 处理 Java Web 应用程序中的身份验证。我只启用了 Google 登录。
在前端,我设法让 Firebase 正常工作并对用户进行身份验证。所以前端没问题。
在后端验证令牌时,我尝试按照文档中的建议使用 Firebase Admin SDK。
https://firebase.google.com/docs/admin/setup
我目前使用的是最新版本。
compile('com.google.firebase:firebase-admin:4.1.1')
我正在后端初始化 Firebase,然后验证我在前端使用 Javascript 获得的 Google 令牌,并在 POST 请求中传递给后端。
https://firebase.google.com/docs/auth/admin/verify-id-tokens
问题是 Firebase 似乎没有从您必须为您的应用程序下载的凭据文件中加载凭据(如何获取凭据文件记录在上面的第一个链接中,它只是一个 JSON 文件类型、project_id、private_key 等)。
try (FileInputStream serviceAccount = new FileInputStream(credentialsFilePath)) {
FirebaseOptions options = new FirebaseOptions.Builder()
.setCredential(FirebaseCredentials.fromCertificate(serviceAccount))
.setDatabaseUrl(databaseURL)
.build();
FirebaseApp.initializeApp(options);
} catch (IOException e) {
LOG.error("Firebase credentials file not found");
throw e;
}
肯定是在找到文件,但是当我检查凭据时它们有空值,当我尝试验证令牌时,我得到了这个异常。
java.lang.IllegalArgumentException: projectId must be set
at com.google.common.base.Preconditions.checkArgument(Preconditions.java:122)
at com.google.firebase.auth.internal.FirebaseTokenVerifier.<init>(FirebaseTokenVerifier.java:50)
at com.google.firebase.auth.internal.FirebaseTokenVerifier$Builder.build(FirebaseTokenVerifier.java:212)
at com.google.firebase.auth.FirebaseAuth$2.then(FirebaseAuth.java:156)
at com.google.firebase.auth.FirebaseAuth$2.then(FirebaseAuth.java:152)
at com.google.firebase.tasks.ContinueWithCompletionListener$1.run(ContinueWithCompletionListener.java:33)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
我已通读文档并查看了他们的 GitHub 存储库中的示例,但他们似乎没有在 Web 应用程序中使用 Java 的 Firebase 的完整工作示例。
所以问题是,我怎样才能让它发挥作用?人们在这种情况下如何使用 Firebase?
谢谢。
附: 1:我没有在谷歌云平台上运行应用程序,所以我不能使用 FirebaseCredentials.applicationDefault() 让谷歌自动加载凭据。
附: 2:我不得不从 Firebase 依赖项中排除模块:'guava-jdk5',因为我也在使用 spring-data-elasticsearch,并且由于依赖项冲突而无法正常工作。但我的类路径中仍然有 guava:18.0。
【问题讨论】:
标签: java firebase firebase-authentication jwt google-signin