【发布时间】:2018-03-13 15:12:49
【问题描述】:
我正在使用 zend 3 框架和 mongodb。使用mongodb/mongodb库连接到mongodb数据库。
如何在 zend 和 mongodb 中添加验证,以便只有经过身份验证的用户才能使用 zend rest api 在 mongodb 中执行 CRUD 操作?
在 mongo shell 中,我使用以下查询向 db 添加了身份验证,效果很好。
use admin;
db.createUser(
{
user: "admin",
pwd: "password",
roles: [ { role: "root", db: "admin" } ]
}
);
mongo --port 27017 -u "admin" -p "password" --authenticationDatabase "admin"
use test;
db.createUser(
{
user: "testUser",
pwd: "password",
roles: [ "readWrite", "dbAdmin" ]
}
);
db.auth("testUser", "password");
现在在 Zend 模型中,我正在使用以下代码,无需 db 身份验证即可正常工作。
$mongoClient = new \MongoDB\Client();
$collection = $mongoClient->selectDatabase($dbName)->selectCollection($collectionName);
$cursor = $collection->findOne(['_id' => $_id]);
现在如何在执行上述查询之前将用户凭据传递给\MongoDB\Client() 以验证用户身份?
【问题讨论】:
-
你好。到目前为止,您尝试了什么?
-
@edigu,我已经用我的代码更新了问题。
-
结果得到了准确的答案!恭喜!