我需要在对 hapi.js Rest api 的所有请求中验证客户端 key 以及 jsonwebtoken header 。
我目前正在使用 hapi-auth-jwt 插件来处理 json Web token - 现在我还想放入一个处理程序来检查上游 api header 中的有效客户端 key - 在执行任何操作之前Web token 检查和其他所有内容 - 以便在未包含有效的 client-api-key 时可以快速返回 401。
我应该将其作为 hapi 中的插件吗?如果是这样,我如何设置插件运行的顺序 - 这只是我注册插件的顺序吗?
如何设置插件来拦截所有 http 请求 - 我应该将其设为身份验证方案吗?
exports.register = function (server, options, next) {
// do I somehow set a default request handler here somehow?
}
最佳答案
您可以注册extension function可用 extension points在 Hapi 请求生命周期中。
在您的情况下,由于您希望在进行身份验证之前验证请求是否具有有效的 client-api-key,因此可以为 onRequest
或 onPreAuth
注册扩展函数> 事件。
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.js - 通过 hapi.js 中的插件链接请求过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31120618/