在没有身份验证过程的情况下在服务器上使用 firebase Java 库
Using firebase Java library on server without authentication process
我正在操作的服务器用于生成 Firebase 令牌并从 firebase 读取和写入值,我使用具有不同 UID 前缀的不同类型令牌的 REST API。
但是,由于服务器中添加了许多功能,我决定使用 Firebase java 库,现在迁移以前为 REST API 编写的代码。
问题是,我如何使用带有令牌的 Firebase Java 库来省略身份验证过程?
身份验证过程异步运行,因此它可以应用于客户端应用程序,但不能应用于服务器。
认证失败怎么办?或者如果花费太多时间怎么办?
之前的代码只是为每个请求使用授权令牌,如下所示:
https://abcd.firebaseio.com/ns/blahblah/event?auth=token
所以它不需要任何身份验证过程。
希望你能理解我糟糕的英语。
谢谢!
根据Firebase documentation for authentication server-side processes,有3种认证方式:
- Using a Firebase app secret
- Using a secure JWT with the optional admin claim set to true
- Using a secure JWT designed to give access to only the pieces of data a server needs to touch
您目前正在从身份验证服务器获取安全的 JWT,因此您使用的是选项 2 或 3。
另一种方法是使用您的 Firebase 应用程序的所谓应用程序机密。您可以在应用程序的仪表板中找到该值,它可以作为当前令牌的直接替代品。
但是我强烈反对这种方法。珍妮在这里写了一份非常详尽的反对理由清单:
您不能仅用 secret
使用 Java 客户端库来替换整个身份验证过程。您需要生成一个 JWT。身份验证持续 24 小时,因此您可以保存生成的 JWT 直到它失败,或者您只需在 24 小时之前再次进行身份验证,并再次保留该 JWT。
为了处理身份验证是一个异步过程这一事实,您还必须使用 CountdownLatch
或 Semaphore
来防止您的程序在身份验证过程收到响应之前退出来自 Firebase。
CountdownLatch
A synchronisation aid that allows one or more threads to wait until a set of operations being performed in other threads completes.
import com.firebase.client.AuthData;
import com.firebase.client.Firebase;
import com.firebase.client.FirebaseError;
import com.firebase.security.token.TokenGenerator;
import java.util.concurrent.CountDownLatch;
public class Main {
public static void main(String[] args) {
Map<String, Object> payload = new HashMap<String, Object>();
payload.put("uid", "uniqueId1");
payload.put("some", "arbitrary");
payload.put("data", "here");
TokenGenerator tokenGenerator = new TokenGenerator("<YOUR_FIREBASE_SECRET>");
String token = tokenGenerator.createToken(payload);
Firebase fb = new Firebase("https://<YOUR-FIREBASE-APP>.firebaseio.com");
CountDownLatch done = new CountDownLatch(1);
fb.authWithCustomToken(token, new Firebase.AuthResultHandler() {
@Override
public void onAuthenticationError(FirebaseError error) {
System.err.println("Login Failed! " + error.getMessage());
done.countDown();
}
@Override
public void onAuthenticated(AuthData authData) {
System.out.println("Login Succeeded!");
// Save your JWT to keep using it for 24 hours
done.countDown();
}
});
done.await();
}
}
信号量
它用于控制正在使用资源的并发线程数。您可以将其视为使用资源的门票。您在创建票证时设置可用票证的数量,当调用 acquire() 时没有票证剩余,您的流程将等待一张可用票证(在 release() 调用中)。在这段代码中,它被创建为零 "tickets" 可用:
import com.firebase.client.AuthData;
import com.firebase.client.Firebase;
import com.firebase.client.FirebaseError;
import com.firebase.security.token.TokenGenerator;
import java.util.concurrent.Semaphore;
public class Main {
public static void main(String[] args) {
Map<String, Object> payload = new HashMap<String, Object>();
payload.put("uid", "uniqueId1");
payload.put("some", "arbitrary");
payload.put("data", "here");
TokenGenerator tokenGenerator = new TokenGenerator("<YOUR_FIREBASE_SECRET>");
String token = tokenGenerator.createToken(payload);
Firebase fb = new Firebase("https://<YOUR-FIREBASE-APP>.firebaseio.com");
Semaphore semaphore = new Semaphore(0);
fb.authWithCustomToken(token, new Firebase.AuthResultHandler() {
@Override
public void onAuthenticationError(FirebaseError error) {
System.err.println("Login Failed! " + error.getMessage());
semaphore.release();
}
@Override
public void onAuthenticated(AuthData authData) {
System.out.println("Login Succeeded!");
// Save your JWT to keep using it for 24 hours
semaphore.release();
}
});
semaphore.acquire();
}
}
我正在操作的服务器用于生成 Firebase 令牌并从 firebase 读取和写入值,我使用具有不同 UID 前缀的不同类型令牌的 REST API。
但是,由于服务器中添加了许多功能,我决定使用 Firebase java 库,现在迁移以前为 REST API 编写的代码。
问题是,我如何使用带有令牌的 Firebase Java 库来省略身份验证过程?
身份验证过程异步运行,因此它可以应用于客户端应用程序,但不能应用于服务器。
认证失败怎么办?或者如果花费太多时间怎么办?
之前的代码只是为每个请求使用授权令牌,如下所示:
https://abcd.firebaseio.com/ns/blahblah/event?auth=token
所以它不需要任何身份验证过程。
希望你能理解我糟糕的英语。 谢谢!
根据Firebase documentation for authentication server-side processes,有3种认证方式:
- Using a Firebase app secret
- Using a secure JWT with the optional admin claim set to true
- Using a secure JWT designed to give access to only the pieces of data a server needs to touch
您目前正在从身份验证服务器获取安全的 JWT,因此您使用的是选项 2 或 3。
另一种方法是使用您的 Firebase 应用程序的所谓应用程序机密。您可以在应用程序的仪表板中找到该值,它可以作为当前令牌的直接替代品。
但是我强烈反对这种方法。珍妮在这里写了一份非常详尽的反对理由清单:
您不能仅用 secret
使用 Java 客户端库来替换整个身份验证过程。您需要生成一个 JWT。身份验证持续 24 小时,因此您可以保存生成的 JWT 直到它失败,或者您只需在 24 小时之前再次进行身份验证,并再次保留该 JWT。
为了处理身份验证是一个异步过程这一事实,您还必须使用 CountdownLatch
或 Semaphore
来防止您的程序在身份验证过程收到响应之前退出来自 Firebase。
CountdownLatch
A synchronisation aid that allows one or more threads to wait until a set of operations being performed in other threads completes.
import com.firebase.client.AuthData;
import com.firebase.client.Firebase;
import com.firebase.client.FirebaseError;
import com.firebase.security.token.TokenGenerator;
import java.util.concurrent.CountDownLatch;
public class Main {
public static void main(String[] args) {
Map<String, Object> payload = new HashMap<String, Object>();
payload.put("uid", "uniqueId1");
payload.put("some", "arbitrary");
payload.put("data", "here");
TokenGenerator tokenGenerator = new TokenGenerator("<YOUR_FIREBASE_SECRET>");
String token = tokenGenerator.createToken(payload);
Firebase fb = new Firebase("https://<YOUR-FIREBASE-APP>.firebaseio.com");
CountDownLatch done = new CountDownLatch(1);
fb.authWithCustomToken(token, new Firebase.AuthResultHandler() {
@Override
public void onAuthenticationError(FirebaseError error) {
System.err.println("Login Failed! " + error.getMessage());
done.countDown();
}
@Override
public void onAuthenticated(AuthData authData) {
System.out.println("Login Succeeded!");
// Save your JWT to keep using it for 24 hours
done.countDown();
}
});
done.await();
}
}
信号量
它用于控制正在使用资源的并发线程数。您可以将其视为使用资源的门票。您在创建票证时设置可用票证的数量,当调用 acquire() 时没有票证剩余,您的流程将等待一张可用票证(在 release() 调用中)。在这段代码中,它被创建为零 "tickets" 可用:
import com.firebase.client.AuthData;
import com.firebase.client.Firebase;
import com.firebase.client.FirebaseError;
import com.firebase.security.token.TokenGenerator;
import java.util.concurrent.Semaphore;
public class Main {
public static void main(String[] args) {
Map<String, Object> payload = new HashMap<String, Object>();
payload.put("uid", "uniqueId1");
payload.put("some", "arbitrary");
payload.put("data", "here");
TokenGenerator tokenGenerator = new TokenGenerator("<YOUR_FIREBASE_SECRET>");
String token = tokenGenerator.createToken(payload);
Firebase fb = new Firebase("https://<YOUR-FIREBASE-APP>.firebaseio.com");
Semaphore semaphore = new Semaphore(0);
fb.authWithCustomToken(token, new Firebase.AuthResultHandler() {
@Override
public void onAuthenticationError(FirebaseError error) {
System.err.println("Login Failed! " + error.getMessage());
semaphore.release();
}
@Override
public void onAuthenticated(AuthData authData) {
System.out.println("Login Succeeded!");
// Save your JWT to keep using it for 24 hours
semaphore.release();
}
});
semaphore.acquire();
}
}