【问题标题】:node.js Express with HTTP-AUTH带有 HTTP-AUTH 的 node.js Express
【发布时间】:2013-03-13 12:49:22
【问题描述】:

我在使用 http-auth 模块创建基于 express 的 node.js 应用程序时遇到问题。

可以为这个 http-auth 库创建中间件我有这个代码:

    //Configure Middlewares
logger.configure(function() {

  //All output has content type json
  logger.use(function(req, res, next) {
    res.contentType('application/json');
    next();
  });

  //Create digest auth middleware
  logger.use(function(req, res, next){
    digest.apply();
    next();
  });
});

应用此功能后,当我连接到站点时出现此错误:

TypeError: Cannot read property 'headers' of undefined

有没有办法解决这个问题或使用其他方法?

我需要对整个应用程序进行摘要身份验证。

【问题讨论】:

  • logger 是 express 应用的实例。
  • 只需使用适当的模式将http-authconnect 集成。

标签: node.js express http-authentication digest-authentication http-auth


【解决方案1】:

我认为 apply() 期待请求对象(因此它无法读取标头);

试试这个语法:

digest.apply(req, res, function(username) {

});   

【讨论】:

  • 这可能有效,但是当我想去任何路线时,我的回复都是空的。
  • apply 函数是异步的,因此您可能必须在apply 回调中调用next()。当然,你所描述的可能是无关的。
  • 此答案对http-auth 模块不再有效。
【解决方案2】:

你应该把它和correct pattern一起使用:

// Authentication module.
var auth = require('http-auth');
var digest = auth.digest({
  realm: "Simon Area.",
  file: __dirname + "/../data/users.htdigest"
});

// Configure Middlewares
logger.configure(function() {
  // Create digest auth middleware
  logger.use(auth.connect(digest));

  // All output has content type json.
  logger.use(function(req, res, next) {
    res.contentType('application/json');
    next();
  });    
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-05-25
    • 2012-09-28
    • 2011-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-23
    相关资源
    最近更新 更多