简体   繁体   English

在App Engine标准Java中初始化Firebase Admin

[英]Initializing Firebase Admin in App Engine standard Java

I'm trying to use Firebase Auth in App engine standard Java to verify tokens from Android. 我正在尝试在App引擎标准Java中使用Firebase Auth来验证来自Android的令牌。

However, when I try to initialize Firebase admin, I get an Exception. 但是,当我尝试初始化Firebase管理员时,我得到了一个例外。

here is the exception: 这是例外:

java.lang.NoSuchMethodError: 
com.google.common.base.Preconditions.checkState
(ZLjava/lang/String;Ljava/lang/Object;)V
at com.google.firebase.FirebaseApp.checkNotDeleted (FirebaseApp.java:352)
at com.google.firebase.FirebaseApp.getOptions (FirebaseApp.java:267)
at com.google.firebase.FirebaseApp$TokenRefresher.<init> (FirebaseApp.java:434)
at com.google.firebase.FirebaseApp$TokenRefresher$Factory.create (FirebaseApp.java:535)
at com.google.firebase.FirebaseApp.<init> (FirebaseApp.java:113)
at com.google.firebase.FirebaseApp.initializeApp (FirebaseApp.java:201)
at com.google.firebase.FirebaseApp.initializeApp (FirebaseApp.java:188)
at com.google.firebase.FirebaseApp.initializeApp (FirebaseApp.java:175)

Most likely, because of conflict in Guava versions 最有可能的是,由于Guava版本的冲突

here is my gradle: 这是我的傻瓜:

dependencies {
    appengineSdk 'com.google.appengine:appengine-java-sdk:1.9.60'
    compile 'com.google.appengine.tools:appengine-gcs-client:0.7'
    compile ('com.google.firebase:firebase-admin:5.7.0') {
        exclude group: 'com.google.guava'
    }
    compile 'com.google.guava:guava:23.0'
    compile 'javax.servlet:servlet-api:2.5'
}

Here is the code: 这是代码:

InputStream in = filterConfig.getServletContext()
            .getResourceAsStream("/WEB-INF/PRIVATE_KEY_JSON.json");

FirebaseOptions options = null;
try {
     options = new FirebaseOptions.Builder()
            .setCredentials(GoogleCredentials.fromStream(in))
            .build();
} catch (IOException e) {
    e.printStackTrace();
}
FirebaseApp.initializeApp(options);

the solution is to add the jar file in the link 解决方案是在链接中添加jar文件

https://github.com/firebase/firebase-admin-java/pull/125 https://github.com/firebase/firebase-admin-java/pull/125

Since the question is related to initializing firebase admin SDK to app engine java environment, so there is a possibility that someone met with the exception: 由于该问题与将firebase管理SDK初始化为app engine java环境有关,因此有可能遇到异常:

java.lang.IllegalStateException: FirebaseApp with name [DEFAULT] already exist. java.lang.IllegalStateException:名称为[DEFAULT]的FirebaseApp已存在。

if all your dependencies are correct as described in above answers, then still you may get this exception. 如果您的所有依赖关系都是正确的,如上面的答案所述,那么您仍然可能会遇到此异常。

Answer to the exception is "You must initialize firebase sdk only for once" or you need to provide unique app name during initializing. 对例外的回答是“您必须仅初始化firebase sdk一次”,或者您需要在初始化期间提供唯一的应用名称。

Now where to put your initialization code that executes only for once so that before using firebase resource , sdk must be initialized. 现在在哪里放置只执行一次的初始化代码,以便在使用firebase资源之前,必须初始化sdk。

Answer is using ServletContextListener docs 答案是使用ServletContextListener 文档

If you have custom logic that you want to run before any of your servlets is invoked: 如果您想在调用任何servlet之前运行自定义逻辑:

Register a ServletContextListener in your web.xml file. 在web.xml文件中注册ServletContextListener。

<listener>
  <listener-class>com.company.MyListener</listener-class>
</listener>

Supply a class alongside your servlet and filter code: 在servlet和过滤器代码旁边提供一个类:

public class MyListener implements ServletContextListener {
  public void contextInitialized(ServletContextEvent event) {
    // This will be invoked as part of a warmup request, or
    // the first user request if no warmup request was invoked.
  }
  public void contextDestroyed(ServletContextEvent event) {
    // App Engine does not currently invoke this method.
  }
}

The ServletContextListener runs during a warmup request. ServletContextListener在预热请求期间运行。 If there is no warmup request, it runs upon the first request to a new instance. 如果没有预热请求,它将在第一次请求到新实例时运行。 This might result in loading requests 这可能会导致加载请求

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM