【问题标题】:How to authenticate with node.js Passport through a get-request?如何通过 get-request 使用 node.js Passport 进行身份验证?
【发布时间】:2012-05-09 19:49:22
【问题描述】:

我正在使用 Passport-Local (https://github.com/jaredhanson/passport-local) 对 node.js 进行身份验证。到目前为止,这个例子就像一个魅力,因为用户正在通过 post-webform 登录:

app.post('/login', 
    passport.authenticate('local', { failureRedirect: '/login', failureFlash: true }),
    function(req, res) {
        res.redirect('/');
});

不过,现在我想编写一个基于 JSON 的 API,通过调用特定 URL 来验证用户身份。将代码块修改为app.get(… 不起作用。

有什么建议吗?

【问题讨论】:

  • 更新:似乎护照使用 req.body 作为用户名/密码。如果我破解本地护照以使用req.query 它可以工作。虽然不是我喜欢的方法……

标签: node.js middleware


【解决方案1】:

我遇到了同样的问题,并通过将其添加到我的路由处理程序进行登录来解决它:

req.body = req.query;

也许不是完美的解决方案,但肯定比盗取护照要好。 ;)

另请注意,passport-local 中有一个待处理的请求来解决此问题:https://github.com/jaredhanson/passport-local/pull/12

【讨论】:

    【解决方案2】:

    也许https://github.com/yarax/passport-url 策略对你有用

    它允许使用 GET 请求提供的令牌对用户进行身份验证

    var url = new UrlStrategy({
        failRedirect : "/login",
        varName : "secret"
    }, function (secret, done) { // put your check logic here
        if (secret == 'foo') done(null, {id:'bar'});
        else done("wrong");
    });
    
    passport.use(url);
    

    【讨论】:

    • 这个问题已经过时了,因为它即将迎来它的三周年 :-) 无论如何感谢您的回复!
    猜你喜欢
    • 2016-08-15
    • 1970-01-01
    • 2016-07-19
    • 2019-06-29
    • 1970-01-01
    • 2018-01-29
    • 2022-07-11
    • 1970-01-01
    • 2014-10-30
    相关资源
    最近更新 更多