javascript - 身份验证策略简单使用未知方案 : bearer-access-token

标签 javascript node.js hapi.js

我正在使用 hapi-auth-bearer-token 插件通过 hapijs 进行 api 身份验证。

这是我的代码:

apiServer.register(require('hapi-auth-bearer-token'), function (err) {

    server.auth.strategy('simple', 'bearer-access-token', {
        allowQueryToken: true,              // optional, true by default
        allowMultipleHeaders: false,        // optional, false by default
        accessTokenName: 'access_token',    // optional, 'access_token' by default
        validateFunc: function( token, callback ) {

            // For convenience, the request object can be accessed
            // from `this` within validateFunc.
            var request = this;

            // Use a real strategy here,
            // comparing with a token from your database for example
            if(token === "1234"){
                //## user object to be looked up here
                callback(null, true, { token: token })
            } else {
                callback(null, false, { token: token })
            }
        }
    });
});

这是我遇到的错误:

Error: Authentication strategy simple uses unknown scheme: bearer-access-token
    at Object.exports.assert (/Users/jamshidnafisi/Documents/srvs-node/node_modules/hapi/node_modules/hoek/lib/index.js:723:11)
    at internals.Auth.strategy (/Users/jamshidnafisi/Documents/srvs-node/node_modules/hapi/lib/auth.js:44:10)
    at internals.Plugin._applyChild (/Users/jamshidnafisi/Documents/srvs-node/node_modules/hapi/lib/plugin.js:471:19)
    at Object.auth.strategy (/Users/jamshidnafisi/Documents/srvs-node/node_modules/hapi/lib/plugin.js:69:18)
    at /Users/jamshidnafisi/Documents/srvs-node/index.js:78:17
    at done (/Users/jamshidnafisi/Documents/srvs-node/node_modules/hapi/node_modules/items/lib/index.js:30:25)
    at Object.exports.register (/Users/jamshidnafisi/Documents/srvs-node/node_modules/hapi-auth-bearer-token/lib/index.js:73:5)
    at /Users/jamshidnafisi/Documents/srvs-node/node_modules/hapi/lib/plugin.js:254:14
    at iterate (/Users/jamshidnafisi/Documents/srvs-node/node_modules/hapi/node_modules/items/lib/index.js:35:13)
    at Object.exports.serial (/Users/jamshidnafisi/Documents/srvs-node/node_modules/hapi/node_modules/items/lib/index.js:38:9)
    at internals.Plugin.register (/Users/jamshidnafisi/Documents/srvs-node/node_modules/hapi/lib/plugin.js:236:11)
    at Object.<anonymous> (/Users/jamshidnafisi/Documents/srvs-node/index.js:76:11)
    at Module._compile (module.js:460:26)
    at Object.Module._extensions..js (module.js:478:10)

消息是简单的英语,但我不明白我必须在代码中添加什么才能解决问题。

最佳答案

看起来您正在一台服务器 (apiServer) 上注册 hapi-auth-bearer-token 插件,然后在另一台服务器 (服务器)

尝试

apiServer.register(require('hapi-auth-bearer-token'), function (err) {

    apiServer.auth.strategy('simple', 'bearer-access-token', {
        allowQueryToken: true,              // optional, true by default
        allowMultipleHeaders: false,        // optional, false by default
        accessTokenName: 'access_token',    // optional, 'access_token' by default
        validateFunc: function( token, callback ) {

            // For convenience, the request object can be accessed
            // from `this` within validateFunc.
            var request = this;

            // Use a real strategy here,
            // comparing with a token from your database for example
            if(token === "1234"){
                //## user object to be looked up here
                callback(null, true, { token: token })
            } else {
                callback(null, false, { token: token })
            }
        }
    });
});

关于javascript - 身份验证策略简单使用未知方案 : bearer-access-token,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31551416/

相关文章:

javascript - 使用 css 或 javascript 放置 100% 高度和 100% 宽度的视频

javascript - pouchdb 和 couchbase 同步网关

javascript - 如何在 JavaScript 中通过异步函数使用数据库事务?

node.js - 尝试使用 Mocha 测试 Node.js 服务器进程

javascript - 访问expressJs静态HTML中的nodejs函数

typescript /Hapi : Property 'example' does not exist on type 'PluginProperties'

node.js - 如何在 hapi.js 中自定义验证错误响应?

node.js - 使用 nodejs 和/或 hapijs 提供静态文件 - 路由

javascript - 使用 div 标签和 Bootstrap 类更新 DOM

javascript - devops-rest APi - $expand 不适用于 "Work Items - Get Work Items Batch"