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

Authentication strategy simple uses unknown scheme: bearer-access-token

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

这是我的代码:

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 插件,然后在另一台服务器 (server)

上设置授权策略

尝试

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 })
            }
        }
    });
});