简体   繁体   English

反应原生 Axios Django - Csrf 失败引用检查失败没有引用

[英]React native Axios Django - Csrf failed referer checking failed no referer

I am calling a Django back-end api from React front-end using axios.我正在使用 axios 从React前端调用Django后端 api。

For that api which is login api, I am using Django Knox package in logic.对于登录 api 的那个 api,我在逻辑中使用了 Django Knox 包。

React.js - I am calling axios.request(method, url, data) and the api call is working correctly. React.js - 我正在调用axios.request(method, url, data)并且 api 调用工作正常。 When I went to Developer tools > Network , I can see Referer header set to React.js website in request header and no other csrf-related header.当我转到Developer tools > Network ,我可以看到Referer标头在请求标头中设置为 React.js 网站,并且没有其他与 csrf 相关的标头。 In Response headers I can see two set-cookie headers, csrftoken and sessionid.在响应头中,我可以看到两个set-cookie头, csrftokensessionid.

React Native - same way I am calling api but api returns error csrf failed referer checking failed - no referer . React Native - 我调用 api 的方式相同,但 api 返回错误csrf failed referer checking failed - no referer When I checked response.config , Referer header is not set unlike React.js当我检查response.config ,与 React.js 不同,未设置Referer标头

Curl - works fine卷曲- 工作正常

httpie - works fine httpie - 工作正常

How can I get rid of this error.我怎样才能摆脱这个错误。

Note 1 - My Django back-end is based on api token logic and not csrf in any way.注 1 - 我的 Django 后端基于 api 令牌逻辑,而不是以任何方式基于 csrf。

Note 2 - React.js and Django are hosted on different domains.注 2 - React.js 和 Django 托管在不同的域上。 I am facing error in React Native which is in debug mode.我在处于调试模式的 React Native 中遇到错误。

Update 1 - After disabling CSRF middleware in Django settings.py, now I am getting only one setCookie header (csrftoken is no longer obtained) but same error still persists.更新 1 - 在 Django settings.py 中禁用 CSRF 中间件后,现在我只得到一个 setCookie 标头(不再获得 csrftoken)但同样的错误仍然存​​在。

Django Rest api need a Referer header. Django Rest api 需要一个 Referer 标头。

In case of React.js it is automatically set (maybe by browser) and its value is current website.在 React.js 的情况下,它会自动设置(可能通过浏览器)并且它的值是当前网站。

But in case of React Native, it is not set.但是在 React Native 的情况下,它没有设置。 so we have to manually set it.所以我们必须手动设置它。

  1. From this link , i set Referer header in axios.从此链接中,我在 axios 中设置了 Referer 标头。 see below code看下面的代码
export const axiosAPICall = (method,url,data,headers) => {
    let request = {
        method: method,
        url: url,
    };
    if (data) {
        request['data'] = data;
    }
    if (headers) {
        request['headers'] = headers;
    }
    // Referer is auto-set in react.js as same website value.
    // for react-native we have to set it manually to target api:port
    request['headers'] = {
        ...request['headers'],
        Referer: url
    }
    return axios.request(request)
        .then(res => res.data)
        .catch(error => {throw error});
};
  1. In Django settings.py , I commented CSRF middleware在 Django settings.py ,我评论了CSRF middleware
  2. In Django settings.py , I added only TokenAuthentication class to remove SessionAuthentication .在 Django settings.py ,我只添加了TokenAuthentication类来删除SessionAuthentication
REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': [
        'rest_framework.authentication.TokenAuthentication',
    ]
}

Note - Please do steps 2 and 3 at your risk after knowing your requirements properly.注意 - 在正确了解您的要求后,请自行承担风险执行第 2 步和第 3 步。 I removed CSRF middleware because my API was completely dependent on token for auth.我删除了 CSRF 中间件,因为我的 API 完全依赖于令牌进行身份验证。 I did not need CSRF in any way.我在任何方面都不需要 CSRF。

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

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