【问题标题】:'Insufficient permissions to access resource': ACL Middleware with Mongoose + Express“访问资源的权限不足”:使用 Mongoose + Express 的 ACL 中间件
【发布时间】:2017-06-28 22:27:21
【问题描述】:

我正在尝试为基本路由身份验证设置ACL middleware。在此示例中,我只是想确保用户(在本例中为我自己)拥有“管理员”权限。

我的路线如下所示:

// define the home page route
router.get('/', acl.middleware(1, '594a984a9815beb8219dca22', 'admin'), function (req, res) {
    acl.userRoles('594a984a9815beb8219dca22', function(err, roles){
      res.send(roles)
    })
})

使用中间件,我得到消息:

HttpError: Insufficient permissions to access resource

如果我在没有中间件的情况下运行这条路线,usersRole() 函数确实会返回

['admin']

作为响应。我不知道 ACL 还需要做什么才能将我识别为管理员。我显然具有“管理员”角色。

更新

这是连接代码:

var dbConn = mongoose.connect(configDB.url); // connect to our database
acl = new acl(new acl.mongodbBackend(mongoose.connection.db))

我的 config.url 是:

module.exports = {
  'url' : 'mongodb://127.0.0.1:27017/NodeMongo'
}

【问题讨论】:

标签: node.js express acl


【解决方案1】:

问题与连接到 mongoose 的异步特性有关。在路由中,已经建立了 mongoose/acl 连接,但不是在此之前(例如,中间件函数调用)。

感谢this example by Alex Mueller,我终于找到了正确的方法

本质上,您需要从以下回调中初始化 acl 对象:

mongoose.connection.on('connected', function(test) {
  require('./authorization').init();
});

然后在您的路由文件中,访问完全初始化的 acl 对象。请参考示例。很容易跟上。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-03
    • 1970-01-01
    • 1970-01-01
    • 2015-05-15
    • 1970-01-01
    • 2012-10-16
    相关资源
    最近更新 更多