【问题标题】:Skip basicAuth for one route为一条路线跳过 basicAuth
【发布时间】:2011-12-24 10:01:01
【问题描述】:

我正在为一个简单的应用程序使用 node、express 和 connect,并按如下方式实现了基本的 HTTP 身份验证(为简洁起见,省略了很多代码):

var express = require('express'),
connect = require('connect');

app.configure = function(){
  // other stuff...
  app.use(connect.basicAuth('username', 'password'));
  // other stuff...
};

我尝试过谷歌搜索,甚至尝试实现我自己的身份验证,但我不知道如何只为一条路线跳过此身份验证。

如果有人对此提供任何帮助,我将不胜感激?

【问题讨论】:

    标签: javascript node.js express connect http-authentication


    【解决方案1】:

    如果您不想对所有路由使用身份验证,则应将 auth 函数作为中间件添加到每个单独的路由,如下所示:

    app.get('/mysecretpage', basicAuth, function (req, res) {
      console.log('you have to be auth to see this page');
    }); 
    

    这是一条没有身份验证的常规路线:

    app.get('/sample', function (req, res) {
      console.log('everybody see this page');
    }); 
    

    此链接也可能对您有用:how to implement login auth in node.js

    【讨论】:

    • 感谢您的回复 - 我正在玩这个,但我将如何在我的中间件函数中实现身份验证?您链接到的页面显示了对用户会话的检查,但我只想在其中进行基本的 HTTP 身份验证。这就是我所拥有的:basicAuth = function(req, res, next) { if (settings.auth.enabled) { connect.basicAuth(settings.auth.user, settings.auth.pass); next(); } else { next(); } };
    • 像这样:app.get('/secret', connect.basicAuth('username', 'password'), function (req, res) { ... });
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-04
    • 1970-01-01
    相关资源
    最近更新 更多