【问题标题】:Node.js http basic authNode.js http 基本身份验证
【发布时间】:2011-11-12 16:31:53
【问题描述】:

是否可以像在 Apache 中一样在 Node.js 中进行基本身份验证?

http://doc.norang.ca/apache-basic-auth.html

我知道如果使用 Express 或 Connect,我可以添加中间件功能并进行用户验证,但我试图限制整个区域(我不需要从数据库中验证用户,只需定义几个用户)- 我使用的是 Ubuntu。

https://github.com/kaero/node-http-digest

这是我可以做的,但我不确定“暴露”或直接在代码中写入用户和密码是否足够安全。

非常感谢。

【问题讨论】:

    标签: javascript authentication ubuntu node.js basic-authentication


    【解决方案1】:

    Passport 提供了一种干净的机制来实现基本身份验证。我在我的 Node.js Express 应用程序中使用它来保护我的基于 Angularjs 的 UI 以及我的 RESTful API。要在您的应用中启动并运行护照,请执行以下操作:

    • npm 安装护照

    • npm install passport-http(包含基本身份验证的“BasicStrategy”对象)

    • 打开您的 app.js 并添加以下内容:

      var passport = require('passport')    
      var BasicStrategy = require('passport-http').BasicStrategy
      
      passport.use(new BasicStrategy(
        function(username, password, done) {
          if (username.valueOf() === 'yourusername' &&
            password.valueOf() === 'yourpassword')
            return done(null, true);
          else
            return done(null, false);
        }
      ));
      
      // Express-specific configuration section
      // *IMPORTANT*
      //   Note the order of WHERE passport is initialized
      //   in the configure section--it will throw an error
      //   if app.use(passport.initialize()) is called after
      //   app.use(app.router) 
      app.configure(function(){
        app.use(express.cookieParser());
        app.use(express.session({secret:'123abc',key:'express.sid'}));
        app.set('views', __dirname + '/views');
        app.set('view engine', 'jade');
        app.set('view options', {
          layout: false
        });
        app.use(express.bodyParser());
        app.use(express.methodOverride());
        app.use(express.static(__dirname + '/public'));
        app.use(passport.initialize());
        app.use(app.router);
        app.use(logger);
      });
      
      // Routes
      
      app.get('/', passport.authenticate('basic', { session: false }), routes.index);
      app.get('/partials/:name', routes.partials);
      
      // JSON API
      
      app.get('/api/posts', passport.authenticate('basic', { session: false }), api.posts);
      app.get('/api/post/:id', passport.authenticate('basic', { session: false }), api.post)
      // --Repeat for every API call you want to protect with basic auth--
      
      app.get('*', passport.authenticate('basic', { session: false }), routes.index);
      

    【讨论】:

      【解决方案2】:

      放这个

      app.use(express.basicAuth(function(user, pass) {
        return user === 'test' && pass === 'test';
      }));
      

      行前到

      app.use(app.router);
      

      使用 http 基本身份验证保护所有路由

      【讨论】:

        【解决方案3】:

        看看: user authentication libraries for node.js?

        它不能 100% 回答您的问题 - 但也许它会有所帮助。

        【讨论】:

        • 嗨@nivoc,感谢您的回复,是的,可能中间件是要走的路,我还没有看到这种情况下的任何其他解决方案。
        【解决方案4】:

        我认为不错的选择可能是http-auth 模块

        // Authentication module.
        var auth = require('http-auth');
        var basic = auth.basic({
            realm: "Simon Area.",
            file: __dirname + "/../data/users.htpasswd" // gevorg:gpass, Sarah:testpass ...
        });
        
        // Application setup.
        var app = express();
        app.use(auth.connect(basic));
        
        // Setup route.
        app.get('/', function(req, res){
          res.send("Hello from express - " + req.user + "!");
        });
        

        【讨论】:

        • 我认为您是http-auth 的维护者的免责声明在这里比较合适。
        猜你喜欢
        • 2011-08-22
        • 1970-01-01
        • 2021-03-21
        • 2011-05-05
        • 1970-01-01
        • 1970-01-01
        • 2020-09-04
        • 2011-02-11
        • 2013-09-01
        相关资源
        最近更新 更多