nginx的问题。如何使用jwt(Django)对API进行身份验证调用?

问题描述 投票:0回答:1

我在nginx和django-rest-framework方面遇到了一些问题。我已经试图弄清楚如何在大约24小时内向API发出经过身份验证的请求。我真的很累,希望有人可以帮助我。

我有以下nginx conf:

location / {

     if ($request_method = OPTIONS ) {
        add_header 'Access-Control-Allow-Origin' "*;
        add_header 'Access-Control-Allow-Methods' "GET, PUT, POST, PATCH, DELETE, OPTIONS";
        add_header 'Access-Control-Allow-Headers' "Authorization, 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range";
        add_header 'Access-Control-Allow-Credentials' "true";
     }

       uwsgi_pass  django;
       include     /path/to/your/mysite/uwsgi_params;, 
}

以下是客户端的代码:

    var xhr = new XMLHttpRequest();
    xhr.withCredentials = true;
    xhr.open('POST', 'http://api/v0/api-token-auth/', true);

    xhr.setRequestHeader('Authorization', 'JWT ' + getCookie("token"))

    xhr.send(JSON.stringify(json)); 

我总是得到401 Unauthorized。我不知道还需要做些什么才能解决这个问题。

django authentication nginx django-rest-framework jwt
1个回答
0
投票

问题是django为了安全原因阻止某些标头允许或阻止请求使用CORS https://github.com/OttoYiu/django-cors-headers

© www.soinside.com 2019 - 2024. All rights reserved.