Str*_*ch0 6 node.js jwt express-jwt passport.js
I am trying to authorise my JWT token with passport middleware but the strategy callback function is not getting called.
In my app.js file, I am specifying for my /users routes to use the middleware like so:
app.use('/users', passport.authenticate('jwt', { session: false }), users);
Run Code Online (Sandbox Code Playgroud)
I then have a seperate file ./passport.js (which I have required at the top of my app.js) where I specify my passport strategy:
passport.use(new JWTStrategy({
jwtFromRequest: ExtractJWT.fromAuthHeaderAsBearerToken(),
secretOrKey : 'jwt_secret_key'
},
function (jwtPayload, cb) {
console.log('jwtPayload', jwtPayload)
}
));
Run Code Online (Sandbox Code Playgroud)
I can't get the console log to run though.
I am using postman to test this and have selected Bearer Token from the authorization options. I can see that this is adding a header to my request.
When I log my request object in my node app, I can see it looks like this:
headers: {
authorization: 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfaWQiOiI1YWM0YWI2ZTk1MWJiMjE1M2NhMjc0OWUiLCJmaXJzdF9uYW1lIjoiQW5kcmV3IiwibGFzdF9uYW1lIjoiTWNDYWxsdW0iLCJlbWFpbCI6ImFtY2NhbGx1bTg5QGdtYWlsLmNvbSIsImlhdCI6MTUyMjg0NzEyNSwiZXhwIjoxNTIyODUwNzI1fQ.WH12GJHMGrGsiJNIwUG2Dx_a9cZKjw7_SW8FYlEvLmk',
accept: '*/*',
host: 'localhost:3037',
},
Run Code Online (Sandbox Code Playgroud)
So the middleware should detect the bearer token and call the middleware?
Any help would be appreciated
原来我的secretOrKey与我secretOrKey创建 JWT 令牌的位置不匹配。
IE通行证策略需要一样 secretOrKey
passport.use(new JWTStrategy({
jwtFromRequest: ExtractJWT.fromAuthHeaderAsBearerToken(),
secretOrKey : 'jwt_secret_key'
},
function (jwtPayload, cb) {
console.log('jwtPayload', jwtPayload)
}
));
Run Code Online (Sandbox Code Playgroud)
作为
const secretOrKey = 'jwt_secret_key'
const token = jwt.sign(payload, secretOrKey, { expiresIn });
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4049 次 |
| 最近记录: |