gpt4 book ai didi

node.js - 如何将 hapi auth 插件变成可选插件

转载 作者:搜寻专家 更新时间:2023-10-31 23:05:03 27 4
gpt4 key购买 nike

我想使用 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com