作者热门文章
- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我想使用 hapi jwt token 身份验证插件 https://github.com/ryanfitz/hapi-auth-jwt但使用可选身份验证创建路由。我怎样才能防止路由返回 401,而是继续使用 null request.auth.credentials 执行。
我希望使用它的所有其他路由保持相同的实现,即在未经过身份验证的请求上返回 401。
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();
最佳答案
你可以在路由配置中将其设置为可选:
server.route({
method: 'GET',
path: '/',
config: {
auth: {
strategy: 'token',
mode: 'optional'
}
}
});
模式可以是true
, false
, required
, optional
, or try
>。查看authentication tutorial了解更多详情。
关于node.js - 如何将 hapi auth 插件变成可选插件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28238814/
我是一名优秀的程序员,十分优秀!