代码之家  ›  专栏  ›  技术社区  ›  JDavis

除了使用express的节点的localhost之外的基本http身份验证

  •  0
  • JDavis  · 技术社区  · 8 年前

    我正在尝试使用Express为不在本地主机上运行的客户端设置节点应用程序的身份验证。

    基本上,我有一个运行rpi的节点,我使用chromium作为设备的gui。问题是你可以在网络上的任何浏览器中键入设备的IP地址,它将提供相同的内容。这在某些情况下是需要的,但我想用密码保护它。

    这是我的设置使用 express-basic-auth :

    const express = require('express'),
          basicAuth = require('express-basic-auth');
    
    const app = module.exports = express();
    const server = require('http').createServer(app);
    
    // Hook Socket.io into Express
    const io = require('socket.io').listen(server);
    
    // Configuration
    app.use( basicAuth(
    {
      users: { admin: 'secret' },
      challenge: true
    }))
    
    app.use( express.static('./my_path/') );
    
    // Socket.io Communication
    io.sockets.on('connection', require('./modules/socket'));
    
    // Start server
    server.listen(3000, () => console.log("Express server listening on port %d in %s mode", server.address().port, app.settings.env));
    

    不过,这是可行的,我不想对localhost进行身份验证。

    我可以从这样的请求中得到主机:

    app.use( ( req ) =>
    {
      debug( req.headers.host );
    })
    

    我对node和express的了解还不够,无法将它们组合在一起。

    也许是因为 快速基本身份验证 我缺少的或不同的授权中间件,我可以使用。

    3 回复  |  直到 8 年前
        1
  •  0
  •   Rohit Saxena    8 年前

    请尝试以下代码:

    // Configuration
    (req, res, next) => {
      if(req.headers.host != 'localhost')
        app.use({
          users: { admin: 'secret' },
          challenge: true
        });
      next();
    }
    

    当本地主机试图使用网站时,将“localhost”更改为req.headers.host状态。

    basicauth()中的三个点表示您在那里拥有的所有其余代码。next()用于告诉代码继续运行。

    如果每个到达后端的请求都通过安装文件“落下”,这将有助于可视化。您添加到此文件中的每一项操作都与请求要通过的另一项操作类似。

        2
  •  0
  •   JDavis    8 年前

    我最后只是用 basic-auth 并设置 WWW-Authenticate 响应头。

    const express = require('express'),
          basicAuth = require('basic-auth');
    
    const app = module.exports = express();
    const server = require('http').createServer(app);
    
    // Hook Socket.io into Express
    const io = require('socket.io').listen(server);
    
    // Configuration
    app.use( ( req, res, next ) =>
    {
      // Require authentication when the connection is not from localhost.
      if ( req.headers.host == 'localhost:3000' )
        return next();
    
      var authentication = basicAuth( req );
    
      if ( authentication && authentication.name == 'admin' && authentication.pass == 'secret' )
        return next();
    
      res.set('WWW-Authenticate', 'Basic');
    
      return res.status(401).send();
    })
    
    app.use( express.static('./my_path/') );
    
    // Socket.io Communication
    io.sockets.on('connection', require('./modules/socket'));
    
    // Start server
    server.listen(3000, () => console.log("Express server listening on port %d in %s mode", server.address().port, app.settings.env));
    
        3
  •  -1
  •   Deadron    8 年前

    我认为最简单的解决方案是在应用程序前面有一个web服务器来管理http身份验证。然后,当你是本地的,你可以绕过Web服务器,直接击中应用程序。

    https://docs.nginx.com/nginx/admin-guide/security-controls/configuring-http-basic-authentication/

    否则,您将不得不编写代码来检查入站请求是否来自本地设备,可能需要检查请求上的IP地址并将其与当前分配的IP地址进行比较。