gpt4 book ai didi

node.js - Passport JWT 策略提取选项

转载 作者:行者123 更新时间:2023-12-05 05:13:13 25 4
gpt4 key购买 nike

使用 Passport JWT 策略,我通过 params 传递 token ,并像这样提取 token ExtractJWT.fromUrlQueryParameter('secret_token')

但有时我通过 header 传递 token ,我想像这样提取它 ExtractJWT.fromHeader('secret_token')

我如何检查它是如何被传递下来的并使用正确的动态提取方法

这是我的代码:

passport.use(new JWTstrategy({
secretOrKey: process.env.AUTH_SECRET,
jwtFromRequest: ExtractJWT.fromUrlQueryParameter('secret_token')

}, async (token, done) => {
try {
//Pass the user details to the next middleware
return done(null, token.user);
} catch (error) {
done(error);
}
}));

谢谢!我已经研究了很长时间....

最佳答案

使用ExtractJwt.fromExtractors()方法

var jwtStrategy = new JwtStrategy({
// this will try to extract from Query parm, header and Authheader
jwtFromRequest: ExtractJwt.fromExtractors([ExtractJwt.fromUrlQueryParameter("secret_token"), ExtractJwt.fromHeader("secret_token"), ExtractJwt.fromAuthHeaderAsBearerToken()]),
//here we have defined all possible extractors in an array
secretOrKey: process.env.AUTH_SECRET
}, async (payload, done) => {
...
});

关于node.js - Passport JWT 策略提取选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53880503/

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