【问题标题】:how to verify header exist?如何验证标头是否存在?
【发布时间】:2017-09-05 15:35:53
【问题描述】:

使用 Node.js,我可以创建一个用户并通过 jwt.sign() 分配一个令牌。这似乎奏效了。错误是当我尝试验证用户是否已登录时。我尝试验证标头是否存在,但 req.headers.authorization 给我未定义。

//登录,这似乎工作正常。

module.exports.login = function(req, res) {
    console.log('logging in a Auth_user')
    console.log(req.body)

      models.Auth_user.findOne({
    where: {email: req.body.email}
  }).then(function(user) {
        // console.log(user)
        console.log(user.email)
        console.log(user.first_name)
        console.log(user.password)

        if (user == null){
            console.log('no user found with email')
            // res.redirect('/users/sign-in')
        }

        bcrypt.compare(req.body.password, user.password, function(err, result) {

        if (result == true){
            console.log('password is valid')
            var token = jwt.sign({ username: user.email }, 'passwordgoeshere', { expiresIn: 600 });

            return res.send()

            res.status(200).json({success: true, token: token});
            res.redirect('/holders')

        }
        else{
            console.log('password incorrect')
                    res.redirect('/home')
                }
    });
  })

};

//认证,这是我无法验证标头的地方

module.exports.authenticate = function(req, res, next) {
console.log('authenticating')
console.log(req.headers.authorization)

  var headerExists = req.headers.authorization;

  if (headerExists) {
    var token = req.headers.authorization.split(' ')[1]; //--> Authorization Bearer xxx
    jwt.verify(token, 'passwordgoeshere', function(error, decoded) {
      if (error) {
        console.log(error);
        res.status(401).json('Unauthorized');
      } else {
        req.user = decoded.username;
        var authenticated = true;
        next();
      }
    });
  } else {
    res.status(403).json('No token provided');
  }
};

【问题讨论】:

  • 您能否显示将Authorization 标头发送到服务器的客户端代码? FWIW,这是您必须在代码中执行的操作,而不是浏览器会自动执行的操作。

标签: node.js authentication token jwt


【解决方案1】:

使用这个

req.header("Access-Control-Allow-Origin", "*");
req.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Authorization, Content-Type, Accept");

var token = req.body.token || req.query.token || req.headers['x-access-token'] || req.headers['Authorization'] || req.headers['authorization'];

【讨论】:

    【解决方案2】:

    使用jwt.verify(req.headers['authorization'], process.env.SECRET_KEY); 控制台这个,你会得到你的令牌

    【讨论】:

      【解决方案3】:

      使用req.headers['authorization']req.header('authorization')

      您还必须检查 authorization 标头是否暴露在您的 Nodejs 身份验证服务器的 Access-Control-Allow-Headers 中,以便您的客户端能够发送它。

      示例代码

      app.use(function(req, res, next) {
        res.header("Access-Control-Allow-Origin", "*");
        res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Authorization, Content-Type, Accept");
        next();
      });
      

      https://developer.mozilla.org/fr/docs/Web/HTTP/Headers/Access-Control-Allow-Headers

      【讨论】:

      • req.headers['authorization']req.headers.authorization 相同。
      • 是的。你也可以使用req.header('authorization')
      • 但这就是问题所在:req.headers.authorization 不存在。
      • 那么一定不能被认证服务器暴露
      • 我的猜测是客户端没有发送它。另外,Authorization 不是响应头而是请求头,所以Access-Control-Expose-Headers 不适用。
      猜你喜欢
      • 1970-01-01
      • 2012-08-14
      • 1970-01-01
      • 2020-08-01
      • 1970-01-01
      • 2019-06-21
      • 2018-07-02
      • 2019-11-19
      • 2015-11-29
      相关资源
      最近更新 更多