什么是不对的HTTP-GET基本验证code? [英] What is wrong with this HTTP-Get Basic authentication code?

查看:192
本文介绍了什么是不对的HTTP-GET基本验证code?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的node.js的RESTify作为后端运行一个REST API服务器和angularjs为前端调用HTTP GET。其余的服务器使用HTTP基本验证。用户名是,密码为

I am using node.js restify as backend to run a REST API server and angularjs as front-end to call the HTTP GET. The REST server uses HTTP Basic Authentication. The username is foo and password is bar.

我已经测试了后端code的工作原理是使用的RESTify客户端。这里是工作的客户端code;

I have tested that the backend code works by using a restify client. Here is the working client code;

var client = restify.createJsonClient({
    url: 'http://127.0.0.1'
});

client.basicAuth('foo', 'bar');

client.get('/alert?list=alertList', function(err, req, res, obj) {
    console.log(obj);
});

我有麻烦我angularjs HTTP-GET code工作。下面是相关code;

I have trouble getting my angularjs http-get code to work. Here is the relevant code;

.controller('ViewCtrl', ['$scope', '$http', '$base64', 
        function ($scope, $http, $cookies, $base64) { 
          var url = '127.0.0.1/alert?list=alertList';
          var auth = $base64.encode('foo:bar');
          $http.defaults.headers.common['Authorization'] = 'Basic ' + auth;
          $http.get(url).then(function (response) {
                tableData = response.data;
                //handle data
          });
}

我无法弄清楚什么是错的angularjs code。我使用的RESTify authorizationParser。是否有任何额外的头要求得到HTTP基本身份验证的RESTify authorizationParser工作?

I cannot figure out what is wrong with the angularjs code. I am using restify authorizationParser. Are there any extra header requirements to get HTTP basic authentication working with restify authorizationParser?

在浏览器的错误信息是这样的;

The error message on the browser looks like this;

{
code: "NotAuthorized",
message: ""
}

在铬调试器,这是我看到的;

In the chrome debugger, this is what I see;

Request Method:GET
Status Code:403 Forbidden
Remote Address:127.0.0.1:80
Request Headers
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding:gzip, deflate, sdch
Accept-Language:en-US,en;q=0.8,zh-CN;q=0.6,zh;q=0.4,zh-TW;q=0.2,ja;q=0.2
Cache-Control:max-age=0
Connection:keep-alive
Host:127.0.0.1
If-Modified-Since:Wed, 23 Dec 2015 02:22:04 GMT
Upgrade-Insecure-Requests:1
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36

我用这BASE64角模块。
https://github.com/ninjatronic/angular-base64

编辑:我发现没有什么用角code。问题出在服务器的RESTify。该服务器的RESTify支持并启用了HTTP基本身份验证时,该静态Web服务器停止工作静态Web服务器。

I discovered there is nothing with the angular code. The problem lies with the restify server. The restify server supports static web server and when http basic authentication was enabled, this static web server stopped working.

推荐答案

在控制器内,可以通过认证头是这样的:

Inside the controller, you can pass the authentication header like this:

var url = '127.0.0.1/alert?list=alertList';
var auth = $base64.encode('foo:bar');
var headers = {"Authorization": "Basic " + auth};
$http.get(url, {headers: headers}).then(function (response) 
     tableData = response.data;
     //handle data
});

这篇关于什么是不对的HTTP-GET基本验证code?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆