简体   繁体   English

从AngularJs App访问Django服务器:简单的身份验证解决方案?

[英]Accessing Django server from AngularJs App: Easy authentication solution?

I've been looking everywhere but can't seem to find an answer that has been helpful. 我到处都在寻找,但似乎找不到一个有用的答案。

I have written an angular app that is trying to pull data from a django RESTful backend that was written by my company. 我编写了一个有角度的应用程序,试图从我公司编写的Django RESTful后端提取数据。 I have a username and password, but I'm not sure how to write the get request to provide the authentication credentials to the backend. 我有一个用户名和密码,但是我不确定如何编写get请求以将身份验证凭据提供给后端。 I'm also confused about what exactly I'm supposed to provide in the header. 我也对应该在标题中提供的内容感到困惑。

Eventually, we will be communicating using nginx. 最终,我们将使用nginx进行通信。

What I want 我想要的是

I'm looking for a fix to write in the angular app itself. 我正在寻找一种可在angular应用本身中编写的修补程序。 I'm trying to write the angular app as separate as possible from the django backend. 我正在尝试将Angular应用与Django后端尽可能地分开编写。 I have already enabled cross origin resource sharing. 我已经启用跨源资源共享。

This will be a band aid fix just to display data from the backend. 这将是一个创可贴修复,仅用于显示来自后端的数据。 It does not need to be permanent by any means. 它不需要任何方式都是永久的。

So far I have tried: 到目前为止,我已经尝试过:

app.factory('mySamples', ['$http', function($http) {
    return $http.get('website/api/foo', {
    headers: {'username':"foo", 'password': 'bar'}
  }).success(function(data) {
    return data;
  }).error(function(err) {
    return err;
  }); 
};

app.factory('mySamples', ['$http', function($http) {
    return $http({method: 'GET', url: 'website/api/samples/', headers: {
        'user': 'foo', 'auth':'bar'}
    });
};

The second factory I wrote returns METHOD: OPTIONS in the networks inspector under returned header. 我写的第二个工厂在返回的标题下的网络检查器中返回METHOD:OPTIONS。 Does anybody have a quick fix just to get data from my api? 有人能快速解决从我的api中获取数据的问题吗?

EDIT: My django backend doesn't support basic authentication, only session based. 编辑:我的django后端不支持基本身份验证,仅基于会话。 How would I edit my get request? 我将如何编辑我的获取请求?

(1) If you are using basic authentication, you can add a basic authentication like so: (1)如果您使用基本身份验证,则可以添加基本身份验证,如下所示:

app.factory('mySamples', ['$http', function($http) {
    var credentials = btoa(username + ':' + authtoken);
    var authorization = {
        'Authorization': 'Basic ' + credentials
    };        
    return $http({method: 'GET', url: 'website/api/samples/', headers: authorization });
};

If all of your $http calls use this same auth, you can use $http.defaults.headers.common.Authorization 如果您所有的$ http调用都使用同一身份验证,则可以使用$http.defaults.headers.common.Authorization

(2) I'm pretty sure nginx does not provide session handling—you'll have to do that in django. (2)我很确定nginx不提供会话处理-您必须在Django中进行。

(3) I don't typically use sessions with REST since REST is usually done stateless. (3)我通常不使用REST会话,因为REST通常是无状态的。


OP asked how to enable CORS. OP询问如何启用CORS。 Enabling CORS is usually done on the server-side. 启用CORS通常是在服务器端完成的。 You can do it in nginx by adding: 您可以在nginx中添加以下内容:

 add_header 'Access-Control-Allow-Origin' '*';
 add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';

To the appropriate location block. 到适当的location块。 You can also do this on the django side with middleware pacakges like django-cors-middleware ( https://pypi.python.org/pypi/django-cors-middleware ) or django-cors-headers ( https://pypi.python.org/pypi/django-cors-headers ). 您也可以在django上使用django-cors-middleware( https://pypi.python.org/pypi/django-cors-middleware )或django-cors-headers( https:// pypi。 python.org/pypi/django-cors-headers )。

You first need to know what kind of Authentication and Authorization is implemented on the server side ie What headers the server looks for authentication/authorization credentials in and what format is expected by the server, then send the credentials under that header key. 您首先需要知道在服务器端实施了哪种AuthenticationAuthorization ,即服务器在什么标头中查找身份验证/授权凭证以及服务器期望哪种格式,然后在该标头密钥下发送凭证。 For example if the sever checks for Authentication credentials in the Authorization header in format username::password then you need to add the headers like 例如,如果服务器在username::password格式为username::passwordAuthorization标头中检查身份验证凭据,则您需要添加标头,例如

headers: {'Authorization': 'johndoe::password'}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM