https请求基本认证node.js

mam*_*one 5 javascript authentication https node.js

我真的很疯狂地在网络和 stackoverflow 上寻找这个。关于此主题的其他帖子讨论的是 http 请求,而不是 httpS。

我正在使用 node.js 编码服务器端,我需要向另一个网站发出 https 请求才能登录

如果我在 Chrome 中使用邮递员工具尝试使用https://user:pass@webstudenti.unica.it/es​​se3/auth/Logon.do一切正常并且我登录。

如果我在节点中使用请求库,我将无法登录,并且会收到一个页面,其中包含有关获取/发送数据中的错误的自定义错误消息。

也许我错误地设置了传递给请求的选项。

var request = require('request');
var cheerio = require('cheerio');
var user =  'xxx';
var pass = 'yyy';
var options = {
    url : 'https://webstudenti.unica.it',
    path : '/esse3/auth/Logon.do',
    method : 'GET',
    port: 443,
    authorization : {
        username: user,
        password: pass
    }
}

request( options, function(err, res, html){
    if(err){
        console.log(err)
        return
    }
    console.log(html)
    var $ = cheerio.load(html)
    var c = $('head title').text();
    console.log(c);
})
Run Code Online (Sandbox Code Playgroud)

http://jsfiddle.net/985bs0sc/1/

msc*_*dex 5

您没有正确设置http 身份验证选项(即authorization应该是auth)。它应该看起来像:

var options = {
    url: 'https://webstudenti.unica.it',
    path: '/esse3/auth/Logon.do',
    method: 'GET',
    port: 443,
    auth: {
        user: user,
        pass: pass
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 使用“auth”对我不起作用。有效的是 ```headers: { 'Authorization': 'Basic ' + new Buffer(username + ':' + passw).toString('base64') }```,请参阅 http://server.dzone.com /文章/nodejs-call-https-basic (9认同)

Ale*_*ill 4

http/https 在身份验证中应该没有区别。您的用户/通行证很可能需要进行 Base64 编码。尝试

var user =  new Buffer('xxx').toString('base64');
var pass = new Buffer('yyy').toString('base64');
Run Code Online (Sandbox Code Playgroud)

请参阅:https ://security.stackexchange.com/questions/29916/why-does-http-basic-authentication-encode-the-username-and-password-with-base64