这是我的身份验证策略的配置:

var JWT_STRATEGY_CONFIG = {
  jwtFromRequest: ExtractJwt.fromAuthHeaderWithScheme('MyBearer'),
  secretOrKey: SECRET,
  issuer : ISSUER,
  audience: AUDIENCE,
  passReqToCallback: false
};


我想添加第二个提取器,如果第一个失败,那么我想尝试第二个。为了说明我的想法,这是我想做的事情:

var JWT_STRATEGY_CONFIG = {
  jwtFromRequest: [
    ExtractJwt.fromAuthHeaderWithScheme('MyBearer'),
    ExtractJwt.fromUrlQueryParameter('authorization')
  ],
  secretOrKey: SECRET,
  issuer : ISSUER,
  audience: AUDIENCE,
  passReqToCallback: false
};

最佳答案

您还可以在ExtractJwt.fromExtractors函数中传递多个提取器



     var JWT_STRATEGY_CONFIG = {
      secretOrKey: SECRET,
      issuer : ISSUER,
      audience: AUDIENCE,
      passReqToCallback: false,
     jwtFromRequest:ExtractJwt.fromExtractors([ExtractJwt.fromBodyField('auth_token'),ExtractJwt.fromUrlQueryParameter('auth_token')]),

};

08-25 11:18