如何将一个hapi auth插件变成可选的
我想使用hapi jwt token auth插件https://github.com/ryanfitz/hapi-auth-jwt,但是使用可选的身份validation创build路由。 我怎样才能阻止路由返回401,而是继续执行一个空的request.auth.credentials。
我想要使用它的所有其他路由保持相同的实现返回401未经身份validation的请求。
server.register(require('hapi-auth-jwt'), function (error) { server.auth.strategy('token', 'jwt', { key: privateKey, validateFunc: validate }); //make this one allow anonymous while also reading logged in credentials server.route({ method: 'GET', path: '/', config: { auth: 'token' } }); server.route({ method: 'GET', path: '/mystuff', config: { auth: 'token' } }); }); server.start();
您可以在路由configuration中将其设置为可选:
server.route({ method: 'GET', path: '/', config: { auth: { strategy: 'token', mode: 'optional' } } });
模式可以是true
, false
, required
, optional
或者try
。 有关更多详细信息,请参阅authentication教程
除了optional
路由的身份validationconfiguration之外,还可以使用try
。
server.route({ method: 'GET', path: '/', config: { auth: { strategy: 'token', mode: 'try' } } })
这两者之间的区别
-
optional
:不需要authentication凭证,但是如果提供,则需要有效 -
try
:如果用户提供了无效的身份validation凭据,他们仍然会被接受,用户将前往路由处理程序
- 当请求来自另一个域时,如何在sailsJS后端应用程序中坚持authentication状态?
- 如何使用快递基本authentication和护照
- 如何在使用mocha / chai进行unit testing时覆盖护照authentication?
- Firebase身份validationID令牌具有不正确的“审核”声明
- 尝试发出POST请求注册新用户时出现404错误
- Firebase createUserWithEmailAndPassword方法在node.js中未定义
- 将护照openID-stategy与智威汤逊结合在一起
- 检测提供商注销passport.js?
- 如果有基本授权,如何在Node.js中使用http.client