我最近开始探索 firebase 作为我的 angular JS 单页网站的身份验证解决方案,它看起来很完美。但是,从安全 Angular 来看,我不太确定在我的应用程序中保留客户端的逻辑。
假设我有一个客户在我的网站上注册并应该完成他的个人资料的检查“isProfileCompleted”。我将数据保存在由 UID 键控的 JSON 中,仅对客户具有独占写入权限。
问题是,现在客户端对他的数据有写访问权限,他可以通过简单地修改浏览器中的 javascript 轻松绕过客户端验证检查。此外,客户可以轻松地将他的 account_type 更新为作者/版主,因为这是他的数据。 firebase 是否提供了解决此问题的方法?
如果不清楚,请告诉我,所以我会尝试进一步详细说明。
谢谢。
最佳答案
您可以使用安全规则保护您的数据。
Firebase 安全规则是一种基于表达式(真值是否为真/假)的规则语言,它存在于 Firebase 服务器上并验证当前用户是否可以访问您的数据。
采用以下数据结构:
{
// the users of the app
"users": {
"1": {
"name": "Sanjay",
"isProfileCompleted": true
},
"2": {
"name": "David",
"isProfileCompleted": false
}
}
}
默认情况下,任何人都可以读取或写入数据到您的 Firebase 数据库。要解决此问题,您可以编写安全规则。
安全规则本质上是对数据结构的注释:
{
"rules": {
"users": { // /users is read only
".read": true,
".write": false
}
}
}
安全规则允许您访问一组服务器变量以检查您的规则。最常用的一个是
auth
变量,它可以让您检查当前经过身份验证的用户。您还可以使用 $
创建通配符变量,它充当路由参数创建。{
"rules": {
"users": {
// users can read and write their own data, but no one else.
"$uid": {
".read": "auth.uid == $uid",
".write": "auth.uid == $uid"
}
}
}
}
您甚至可以编写规则来验证数据的结构。
{
"rules": {
"users": {
// users can read and write their own data, but no one else.
"$uid": {
".read": "auth.uid == $uid",
".write": "auth.uid == $uid",
".validate": "newData.hasChildren(['name', 'isProfileCompleted']),
"name": {
".validate": "newData.isString()"
},
"isProfileCompleted": {
".validate": "newData.isBoolean()"
}
}
}
}
}
但是 Bolt compiler is a better solution for this ,因为它允许您创建类型来定义架构。
您可以在 Firebase App Dashboard 中编写您的安全规则,也可以通过 Firebase CLI 上传它们。
关于angularjs - 如何防止客户修改 Firebase 数据(在没有后端的 Web 应用程序中)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34017161/