5

hapi 及其 auth-cookie 插件的示例并不多,但这是我迄今为止尝试保护路由的示例。请注意,我见过的大多数示例都使用旧版本的 hapi,这似乎并不完全适用于这种情况,我希望我只是遗漏了一些简单的东西:

var Hapi = require('hapi');
var Mongoose = require('mongoose');

Mongoose.connect('mongodb://localhost/rfmproducetogo');

var server = new Hapi.Server(8080, "localhost");

server.pack.register([{
    plugin: require("lout")
}, {
    plugin: require('hapi-auth-cookie')
}, {
    plugin: require("./plugins/togo")
}, {
    plugin: require("./plugins/auth")
}], function(err) {
    if (err) throw err;
    server.auth.strategy('session', 'cookie', {
        password: 'shhasecret',
        cookie: 'wtfisthisfor',
        isSecure: false,
        redirectTo: false
    });
    server.start(function() {
        console.log("hapi server started @ " + server.info.uri);
    });
});

在我的多哥插件中,我有这个路由设置来使用会话

exports.create = function(plugin) {
    plugin.route({
        method: 'POST',
        path: '/togo/add',
        handler: function(request, reply) {
            produce = new Produce();
            produce.label = request.payload.label;
            produce.price = request.payload.price;
            produce.uom = request.payload.uom;
            produce.category = request.payload.category;

            produce.save(function(err) {
                if (!err) {
                    reply(produce).created('/togo/' + produce._id);
                } else {
                    reply(err);
                }

            });
        },
        config: {
            auth: 'session'
        }
    });
};

我看到的错误是这样的:

/home/adam/Projects/bushhog/node_modules/hapi/node_modules/hoek/lib/index.js:421
    throw new Error(msgs.join(' ') || 'Unknown error');
          ^
Error: Unknown authentication strategy: session in path: /togo/add
    at Object.exports.assert (/home/adam/Projects/bushhog/node_modules/hapi/node_modules/hoek/lib/index.js:421:11)
    at /home/adam/Projects/bushhog/node_modules/hapi/lib/auth.js:123:14
    at Array.forEach (native)
    at internals.Auth._setupRoute (/home/adam/Projects/bushhog/node_modules/hapi/lib/auth.js:121:24)
    at new module.exports.internals.Route (/home/adam/Projects/bushhog/node_modules/hapi/lib/route.js:118:43)
    at /home/adam/Projects/bushhog/node_modules/hapi/lib/router.js:110:25
    at Array.forEach (native)
    at /home/adam/Projects/bushhog/node_modules/hapi/lib/router.js:107:17
    at Array.forEach (native)
    at internals.Router.add (/home/adam/Projects/bushhog/node_modules/hapi/lib/router.js:104:13)

运行节点 0.10.28、hapijs 6.x、hapi-auth-cookie 1.02

4

2 回答 2

1

当您尝试在身份验证策略实际可用之前使用它时,会出现此问题。

您已经通过将功能拆分为具有给定范围的单个小型插件来遵循良好的应用程序设置。


更新:这是针对该问题的专门教程,如何修复“未知身份验证策略”</a>


设置身份验证和依赖身份验证的插件的一个好方法是创建一个额外的“身份验证插件”,它可以添加您想要的策略,并且可以用作其他插件的依赖项。

hapi 身份验证插件示例

exports.register = function (server, options, next) {

  // declare/register dependencies
  server.register(require('hapi-auth-cookie'), err => {

    /**
     * Register authentication strategies to hapi server
     *
     * We’re using hapi-auth-cookie plugin to store user information on
     * client side to remember user data on every website visit
     *
     * For sure, we could and will add more authentication strategies.
     * What’s next: JWT (we highly welcome pull requests to add JWT functionality!)
     */
    server.auth.strategy('session', 'cookie', {
      password: 'ThisIsASecretPasswordThisIsASecretPassword',
      cookie: 'hapi-rethink-dash',
      redirectTo: '/login',
      isSecure: false
    });

    server.log('info', 'Plugin registered: cookie authentication with strategy »session«')

    next()

  })

}

exports.register.attributes = {
  name: 'authentication',
  version: '1.0.0'
}

在您的设置中,/plugins/togo您将其设置authentication plugin为依赖项(使用server.dependency([array-of-deps])),这意味着 hapi 先注册 auth 插件,然后再注册依赖的插件。

你像这样注册你的插件:

server.register([{
    plugin: require('./plugins/authentication')
}, {
    plugin: require("./plugins/togo")
}], function(err) { 
  // handle callback
})

查看hapi-rethinkdb-dash以获取详细示例。

希望有帮助!

于 2017-03-14T07:32:21.877 回答
0

请记住,如果您像 Marcus Poehls 那样在插件中使用 server.dependency,您还需要注册该依赖项

server.register([{
    plugin: require('hapi-auth-cookie')
},{
    plugin: require('./plugins/authentication')
}, {
    plugin: require("./plugins/togo")
}], function(err) { 
  // handle callback
})
于 2017-05-17T10:04:43.660 回答