fromAuthHeaderAsBearerToken 在节点中不起作用

Posted

技术标签:

【中文标题】fromAuthHeaderAsBearerToken 在节点中不起作用【英文标题】:fromAuthHeaderAsBearerToken is not working in NODE 【发布时间】:2018-10-23 08:54:32 【问题描述】:

我在 Node 中对护照身份验证做了以下事情。

1) 我正在使用 jwtFromRequest :ExtractJwt.fromAuthHeaderAsBearerToken()

module.exports = function(passport)
    var opts = ;
    opts.jwtFromRequest = ExtractJwt.fromAuthHeaderAsBearerToken();
    opts.secretOrKey = config.secret;
    console.log('Inside passport');
    //opts.issuer = 'accounts.examplesoft.com';
    //opts.audience = 'yoursite.net';
    passport.use(new JwtStrategy(opts, function(jwt_payload, done) 
        console.log('Payload :: '+jwt_payload._doc);
        User.getUserById(id: jwt_payload._doc._id, function(err, User) 
            if (err) 
                return done(err, false);
            
            if (User) 
                return done(null, User);
             else 
                return done(null, false);
                // or you could create a new account
            
        );
    ));

2) 调用方法如下:

userExpressRoutes.route('/profile')
    .get(passport.authenticate('jwt',  session: false ), function (req, res)   );

3) 在 Ppostman 中设置标题,如:Authorization:Bearer token

Post Authentication Header

4) 它正在给undefined有效载荷

Payload :: undefined
TypeError: Cannot read property '_id' of undefined

这里缺少什么来获取 Jwt_payload?

有人可以帮我吗?

【问题讨论】:

User.getUserById($**_id**: jwt_payload._doc._id, function(err, User) 是_id还是id @SanjaySinghBhandari 我尝试了 id 和 _id ,但它不起作用 如果你这样做console.log('jwt_payload =', jwt_payload);,你会得到什么?例如,也许应该是doc 而不是_doc @DavidKnipe 嗨大卫,我在控制台中得到 jwt_payload =undefined。这就是问题所在。我收到未定义的有效载荷。 @SanketLathiya 不,你不是,你得到的是jwt_payload._doc = undefined。如果 jwt_payload 未定义,则错误消息将是“无法读取未定义的属性 '_doc'”。 【参考方案1】:

一些工作组合

For - fromHeader

ExtractJwt.fromHeader('authorization'),

Authorization : eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJyb2xlIjoiQ2xpZW50IiwiX2lkIjoiNWUzN2NkMGI4YTAxNjEwNWNhMmFjZjYwIiwiZW1haWwiOiJwcmFqYWt0YUBnbWFpbC5jb20iLCJwYXNzd29yZCI6IiQyYiQxMCRzWXN4MGcyWGsybWdSTHNaZXBEYkV1MklRcGhVOURkNnczeTBHaUxMWHJVeW5aazlUR0xKSyIsIl9fdiI6MCwiaWF0IjoxNTgwNzE5ODE3LCJleHAiOjE1ODA3Mjk4OTd9.38x2wztqJWz9EH8_lN0ca-L-8mTQvW36iF2bfGk_ydg

For - fromHeader

ExtractJwt.fromHeader('HelloTom'),

HelloTom : eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJyb2xlIjoiQ2xpZW50IiwiX2lkIjoiNWUzN2NkMGI4YTAxNjEwNWNhMmFjZjYwIiwiZW1haWwiOiJwcmFqYWt0YUBnbWFpbC5jb20iLCJwYXNzd29yZCI6IiQyYiQxMCRzWXN4MGcyWGsybWdSTHNaZXBEYkV1MklRcGhVOURkNnczeTBHaUxMWHJVeW5aazlUR0xKSyIsIl9fdiI6MCwiaWF0IjoxNTgwNzE5ODE3LCJleHAiOjE1ODA3Mjk4OTd9.38x2wztqJWz9EH8_lN0ca-L-8mTQvW36iF2bfGk_ydg

For - fromAuthHeaderAsBearerToken

ExtractJwt.fromAuthHeaderAsBearerToken(),

Authorization : bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJyb2xlIjoiQ2xpZW50IiwiX2lkIjoiNWUzN2NkMGI4YTAxNjEwNWNhMmFjZjYwIiwiZW1haWwiOiJwcmFqYWt0YUBnbWFpbC5jb20iLCJwYXNzd29yZCI6IiQyYiQxMCRzWXN4MGcyWGsybWdSTHNaZXBEYkV1MklRcGhVOURkNnczeTBHaUxMWHJVeW5aazlUR0xKSyIsIl9fdiI6MCwiaWF0IjoxNTgwNzE5ODE3LCJleHAiOjE1ODA3Mjk4OTd9.38x2wztqJWz9EH8_lN0ca-L-8mTQvW36iF2bfGk_ydg

For - fromAuthHeaderWithScheme

ExtractJwt.fromAuthHeaderWithScheme('JWT'),

Authorization : JWT eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJyb2xlIjoiQ2xpZW50IiwiX2lkIjoiNWUzN2NkMGI4YTAxNjEwNWNhMmFjZjYwIiwiZW1haWwiOiJwcmFqYWt0YUBnbWFpbC5jb20iLCJwYXNzd29yZCI6IiQyYiQxMCRzWXN4MGcyWGsybWdSTHNaZXBEYkV1MklRcGhVOURkNnczeTBHaUxMWHJVeW5aazlUR0xKSyIsIl9fdiI6MCwiaWF0IjoxNTgwNzE5ODE3LCJleHAiOjE1ODA3Mjk4OTd9.38x2wztqJWz9EH8_lN0ca-L-8mTQvW36iF2bfGk_ydg

For - fromAuthHeaderWithScheme

ExtractJwt.fromAuthHeaderWithScheme('HelloJerry'),

Authorization : HelloJerry eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJyb2xlIjoiQ2xpZW50IiwiX2lkIjoiNWUzN2NkMGI4YTAxNjEwNWNhMmFjZjYwIiwiZW1haWwiOiJwcmFqYWt0YUBnbWFpbC5jb20iLCJwYXNzd29yZCI6IiQyYiQxMCRzWXN4MGcyWGsybWdSTHNaZXBEYkV1MklRcGhVOURkNnczeTBHaUxMWHJVeW5aazlUR0xKSyIsIl9fdiI6MCwiaWF0IjoxNTgwNzE5ODE3LCJleHAiOjE1ODA3Mjk4OTd9.38x2wztqJWz9EH8_lN0ca-L-8mTQvW36iF2bfGk_ydg

【讨论】:

【参考方案2】:

我通过下面的代码 sn-p 解决了这个问题。谢谢大家的支持...

const JwtStrategy = require('passport-jwt').Strategy;
const ExtractJwt = require('passport-jwt').ExtractJwt;
const User = require('../models/User');
const config = require('../config/DB');

module.exports = function(passport)
  let opts = ;
  opts.jwtFromRequest = ExtractJwt.fromAuthHeaderAsBearerToken();
  opts.secretOrKey = config.secret;
  passport.use(new JwtStrategy(opts, (jwt_payload, done) => 
    User.findById(jwt_payload.data._id, (err, User) => 
      if(err)
        return done(err, false);
      

      if(User)
        return done(null, User);
       else 
        return done(null, false);
      
    );
  ));

然后在请求标头中像这样传递令牌

Authorization:bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJyb2xlIjoiQ2xpZW50IiwiX2lkIjoiNWUzN2NkMGI4YTAxNjEwNWNhMmFjZjYwIiwiZW1haWwiOiJwcmFqYWt0YUBnbWFpbC5jb20iLCJwYXNzd29yZCI6IiQyYiQxMCRzWXN4MGcyWGsybWdSTHNaZXBEYkV1MklRcGhVOURkNnczeTBHaUxMWHJVeW5aazlUR0xKSyIsIl9fdiI6MCwiaWF0IjoxNTgwNzE4NjQxLCJleHAiOjE1ODA3Mjg3MjF9.T8n1YWRSHfr_1caZ51TbT4VdnBx2uXg1x2JOJC-TBL0

【讨论】:

我还在代码中生成令牌时添加了 [令牌:Bearer $token]。【参考方案3】:

它对我有用:

我在请求中更改了授权标头: eyJhbGciOiJIUzI1NiIsI...(jwt 令牌)

Bearer eyJhbGciOiJIUzI1NiIsI...

【讨论】:

以上是关于fromAuthHeaderAsBearerToken 在节点中不起作用的主要内容,如果未能解决你的问题,请参考以下文章