【发布时间】:2017-01-22 23:26:30
【问题描述】:
我想阻止添加时间戳早于现有时间戳的数据。示例数据库:
{
"latest" : {
"517" : {
"PARAM1" : {
"timestamp" : 11492,
"value" : 6593
}
}
}
}
Firebase 安全规则:
{
"rules": {
".read": "auth != null",
".write": "auth != null",
"latest": {
"$id": {
"$param": {
"timestamp": { ".validate": "newData.val() >= data.val()" }
}
}
}
}
}
现在,有趣的部分。当我检查从模拟器中添加不同的数据时 - 它按预期工作。较小的时间戳被拒绝,更高的被添加。但它不会对从 Java 代码中添加相同的数据产生任何影响。
应该拒绝的样本数据:
/latest/517
{
"PARAM1" : {
"timestamp" : -5,
"value" : 643
}
}
如Admin Database API 部分所述,我将com.google.firebase:firebase-admin:4.0.3 与纯Java 结合使用。
POJO:
public class TimeValue {
public long timestamp;
public double value;
public TimeValue(long timestamp, double value) {
super();
this.timestamp = timestamp;
this.value = value;
}
}
插入:
Map<String, TimeValue> timeValue = new HashMap<String, TimeValue>();
timeValue.put("PARAM1", new TimeValue(-5L, 643));
final FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference idRef = database.getReference("latest/517");
idRef.setValue(timeValue);
为什么会这样?这没有任何意义。
【问题讨论】:
标签: java firebase firebase-realtime-database firebase-security