1
我們使用hapijs和oauth服務器進行身份驗證。我們需要在hapijs中實施基於角色的授權。以下方式對hapijs來說不錯。使用Hapijs和oauth 2的API授權流程
註冊認證方案
server.auth.scheme('custom', function (server, options) { return { authenticate: function (request, reply) { // calling oauth flow for roles match } });
註冊認證策略&添加auth,在server.route角色
server.auth.strategy('default', 'custom'); server.route({ method: 'GET', path: API_Path, config: { roles: ['ADMIN', 'USER'], auth : 'default' }, handler: function (request, reply) { return reply.act({ role: 'admin', cmd: 'getInfo', id: request.params.id }); } });