【问题标题】:chaining request filters through plugins in hapi.js通过 hapi.js 中的插件链接请求过滤器
【发布时间】:2015-09-16 04:59:20
【问题描述】:

我需要在我对 hapi.js rest api 的所有请求中验证客户端密钥和 jsonwebtoken 标头。

我目前正在使用 hapi-auth-jwt 插件来处理 json Web 令牌 - 现在我还想放入一个处理程序,在上游检查 api 标头中的有效客户端密钥 - 在它执行任何操作之前Web 令牌检查和其他所有内容 - 如果不包含有效的 client-api-key,它可以快速返回 401。

我应该将其作为 hapi 中的插件来执行吗?如果是这样,我如何设置运行插件的顺序 - 仅仅是我注册插件的顺序吗?

如何设置插件以拦截所有 http 请求 - 我应该将其设为身份验证方案吗?

exports.register = function (server, options, next) { 
// do I somehow set a default request handler here somehow?
}

【问题讨论】:

    标签: hapijs


    【解决方案1】:

    您可以在 Hapi 请求生命周期中为可用的extension points 注册一个extension function

    在您的情况下,由于您希望在进行身份验证之前验证请求是否具有有效的 client-api-key,因此可以为 onRequestonPreAuth 事件注册扩展功能。

    exports.register = function (server, options, next) { 
    
        server.ext('onRequest', function (request, reply){
    
            //Validate the request object here.
    
            if (valid) reply.continue();
            else reply(Boom.unauthorized('Invalid API Key'));
        });
    
        next();
    }
    

    【讨论】:

    • 对于 Hapi v17:if (valid) return reply.continue; else throw Boom.unauthorized('Invalid API Key');
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多