- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
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);
});
});
在我的 togo 插件中,我有这个路由设置来使用 session
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)
运行 Node 0.10.28,hapijs 6.x,hapi-auth-cookie 1.02
最佳答案
当您尝试在实际可用之前使用身份验证策略时,会出现此问题。
您已经通过将功能拆分为具有给定范围的单个小插件来遵循良好的应用程序设置。
更新:这是针对该问题的专门教程,how to fix „unknown authentication strategy“
设置身份验证和依赖于身份验证的插件的一个好方法是创建一个额外的“身份验证插件”,添加您想要的策略,并可以用作其他插件的依赖项。
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获取详细示例。
希望对您有所帮助!
关于node.js - hapi-auth-cookie 无法加载 session 策略,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24440791/
我想向 hapi 服务器添加插件,该服务器具有多个连接,例如监听不同的 ip。 是否可以向所有配置的服务器添加插件? 或者如何循环所有服务器以将插件添加到所有服务器? 最佳答案 默认情况下,插件会在调
如何验证可选参数使用 hapi 和 joi path: '/users/{limit?}', limit 是可选的,但如果存在则应为整数。 最佳答案 您可以使用 Joi.number().intege
我想允许我的前端将值的哈希值推送到后端。哈希可以具有任何(JSON 合法)键,但每个键的值必须符合特定模式。可以这样想: extended: { tubers: { potato: {we
如何使用 Hapi 18.1 设置响应延迟,如果 API 响应缓慢,我想看到沙漏旋转。在早期版本中,我使用了如下所示的 setTimeout()。 server.route({ method:
我使用的是hapi,而不是express。 super 测试还应该有效吗? 如果是这样,有没有一种快速的方法来更改我的代码以使其运行? 我的测试看起来像这样,基于 documentation : im
根据文档https://hapijs.com/api/16.0.1#route-configuration路由方法可能不是“HEAD”类型,但我不明白为什么我不能覆盖该行为。 最佳答案 HEAD 路由
基本上,我的应用有一个登录/注册登录页面。用户登录或注册后,我不希望他们能够再访问此路由。我不确定如何使用 Hapi.js 实现这一目标。登录页面不使用任何身份验证策略,因此它不知道用户是否登录。 最
我查看了为 FHIR 创建配置文件的文档,并且使用 Forge 创建了一些我认为可以有效描述配置文件的 xml。我在文档中找不到它实际说明我需要向 RESTful api 发出什么请求才能实际添加新配
我在 hapi.js 中使用访问 token 时遇到问题。我无法理解如何使用该 token 进行身份验证。我正在关注这篇文章 dwyl/hapi-auth-jwt2 .我正在使用 mongodb 作为
我刚刚开始学习hapi.js 。我正在尝试理解身份验证的代码。这是如何运作的。为此,我关注了hapi-auth-jwt2 。 之后,当我从 postman 调用 API 时,我没有得到任何输出。 这是
正在使用 @hapi/hapi : ^20.0.0 Node .js:v10.19.0 得到以下错误堆栈 | #meetings = null; | ^ | | SyntaxError
我一年前使用以下版本的 hapi 和 hapi-swagger 创建了一个 REST API。 hapi: "8.5.1", hapi-swagger: "0.7.3" 为了注册 hapi-swagg
我们目前使用 HAPI 和 HAPI-AUTH-BASIC 进行身份验证。 配置路由时,是否可以检查用户是否已通过身份验证,如果未通过身份验证,则将其重定向到自定义登录页面? 每当我们将身份验证设置为
我需要在 server.onPreHandler ext 方法中设置一些响应 header 。有两种情况,当用户向我的路线终点发送 API 请求时,我需要发生这种情况。1)在成功的场景中,我需要设置标
我正在构建一个简单的日志应用程序,并尝试实现模块化以使代码更具可读性。然而,当我启动我的应用程序时遇到了一些麻烦。我有两个文件:index.js,其中有我的服务器配置;routes.js,其中有我的路
可以将代理对象添加到 Hapi 上的预处理程序中,例如 pre:[{ assign: 'callProxy' method: { proxy:{}
尝试验证数组在一种情况下是否有零个或多个字符串,以及在另一种情况下是否有零个或多个对象,与 Joi 文档作斗争:( validate: { headers: Joi.object({
我正在尝试将 hapi.js 与 mysql 连接起来。但是在定义一个 server.route 的时候。 处理程序 没有返回值。 server.route({ method:'GET
我希望在数据库响应之前向我的用户显示加载图标,然后将其重定向到正确的页面。 我的代码是这样的: // handler function handler(request, reply) { repl
在 hapi.js 中,我可以使用将请求从一条路由重定向到另一条路由 response.redirect('/home') 如何通过重定向发送一些数据?我尝试在标题中设置它 response.redi
我是一名优秀的程序员,十分优秀!