I want to implement the server authentication method described here in paragraph 1 .
- Using a Firebase app secret: All authentication methods can accept a Firebase app secret instead of a JWT token. This will grant the server complete read and write access to the entire Firebase database. This access will never expire unless it is revoked via the App Dashboard.
我需要指导如何做到这一点。我不想使用任何 auth 变量,因为我不会对用户进行身份验证,而是通过某个单一的静态 key 对我的服务器进行身份验证。
那么我的安全规则会是什么样子?
这是我到目前为止所拥有的。
安全规则.json{
"rules": {
".read": true,
".write": "secret == 'mykey'"
}
}
我如何在服务器端 HTTP 请求中实现这一点?我会创建一个名为 secret
的 header ,其值 mykey
如下所示:
{"secret": "mykey"}
最佳答案
要在 HTTP 请求中使用 key ,请将其传递到 URL 的 auth 参数中。例如
curl 'https://yours.firebaseio.com/.json?auth=<your_secret>'
当您使用您的 key 通过 Firebase 进行身份验证时,生成的 session 将以管理员身份运行。它对整个 Firebase 数据库具有完全读/写访问权限,就像访问 Firebase 仪表板时一样。因此您不需要在安全规则中授予任何权限。
如果您希望能够检测服务器,则应使用自定义 token 而不是您的 key 。创建自定义 token 时,您可以准确确定 auth
变量中包含的内容。例如
{
"uid": "myserver"
}
现在您可以在安全规则中检查该特定 uid:
{
"rules": {
".read": true,
".write": "auth.uid = 'myserver'"
}
}
您可以使用此 jsfiddle 创建自定义 token :http://jsfiddle.net/firebase/XDXu5/
关于security - 使用 Firebase 应用 key 对服务器进行身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34876641/