Group/rule-based authorization approach in node.js and express.js

后端 未结 6 1593
鱼传尺愫
鱼传尺愫 2021-01-30 00:31

What are good strategies for role-based authorization in express.js? Especially with express-resource?

With Express-resource there are no handlers, so I think there are

6条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-30 00:59

    In express you can add a handler that hooks into every operator (http://expressjs.com/guide.html#passing-route control) where you can do precondition validation. Here you can retrieve the role for the user and restrict access based on the HTTP verb (PUT, DELETE, etc.) or the URL (param('op') is 'edit' or so).

    app.all('/user/:id/:op?', function(req, res, next){
      req.user = users[req.params.id];
      if (req.user) {
        next();
      } else {
        next(new Error('cannot find user ' + req.params.id));
      }
    });
    

提交回复
热议问题