簡體   English   中英

Http摘要驗證在node.js中失敗

[英]Http Digest Authentication fails in node.js

我正在嘗試使用摘要身份驗證進行http get請求。 我在下面發布我的代碼`

var digestOption    =   {
                                        'host' : hostName,
                                        'port'  : port,
                                        'path'  : path
                                    }
            http.get(digestOption, function(res){
                var challengeParams = parseDigest(res.headers['www-authenticate'])
                var ha1 = crypto.createHash('md5');
                ha1.update([username,challengeParams.realm,password].join(':'));
                console.log('h1 >>> '+ha1);
                var ha2 = crypto.createHash('md5');
                ha2.update(['GET',digestOption.path].join(':'));
                var response = crypto.createHash('md5');
                response.update([ha1,challengeParams.nonce,'1::auth',ha2].join(':'));
                var authRequestParams = {
                  username : username,
                  realm : challengeParams.realm,
                  nonce : challengeParams.nonce,
                  uri : digestOption.path, 
                  qop : challengeParams.qop,
                  response : response,
                  nc : '1',
                  cnonce : '',
                }
                digestOption.headers = { 'Authorization' : renderDigest(authRequestParams) }
                http.get(digestOption, function(res) {
                  res.setEncoding('utf-8')
                  var content = ''
                  res.on('data', function(chunk) {
                    content += chunk
                  }).on('end', function() {
                    console.log(content)
                  })
                });
            });

`但是我越來越

400錯誤的請求錯誤

我已經測試了從瀏覽器進行的連接並獲得了身份驗證對話框。 因此,我的代碼有問題。

您可以使用RFC 1738中指定的URL本身的技巧。只需在用戶之前將用戶名/ pass傳遞給@符號即可。

var request = require('request'),
username = "john",
password = "1234",
url = "http://" + username + ":" + password + "@www.example.com";

request({
    url : url
},
function (error, response, body) {
    // Do more stuff with 'body' here
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM