【问题标题】:How to handle CSRF tokens with React Relay如何使用 React Relay 处理 CSRF 令牌
【发布时间】:2018-01-08 07:06:12
【问题描述】:

我正忙于开发一个 React Native 应用程序,该应用程序与 Django 服务器上的 GraphQL api 对话。

在 React Native 中,我正在使用 React Relay 尝试处理我的 GraphQL 请求(按照找到的指南 here),但我的请求出现 403 问题。

回复说CSRF token missing or incorrect,我正试图找出最好的方法来让它工作。

我知道我需要先获取一个 CSRF cookie 令牌,然后以某种方式将它与我的 GraphQL Post 请求一起传递,但运气不佳。 我目前的实现如下...

fetch('http://' + ip + ':8000/sign-in/') 
    .then((response) => {
        const cookieHeader = response.headers.map["set-cookie"]; // This gets me a cookie response with a CSRF token
        fetch('http://' + ip + ':8000/graphql', {
          method: 'POST',
          headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json',
            'Cookie': cookieHeader, // Try and pass the received cookie to my POST
            'XSRF-TOKEN': cookieHeader // Trying this as well
          },
          body: JSON.stringify({
            query: operation.text,
            variables,
          }),
        }).then(response => {
          console.log('RESPONSE', response) // Currently getting a 403
          return response.json()
        })
    })

但这仍然给我一个 403 错误。

我似乎找不到更多关于如何解决这个问题的信息。任何人都可以告诉我哪里出错了,或者关于如何解决这个问题的一些建议?

(下面是我的 API 请求的快照)

【问题讨论】:

    标签: reactjs react-native csrf react-relay


    【解决方案1】:

    所以设法让它与以下...

    return getCsrfToken().then(csrfToken => {
        if (csrfToken == null) {
            console.log('CSRF NOT SET')
        }
    
        const url = 'http://' + ip + '/graphql'
        return fetch(url, {
                method: 'POST',
                headers: {
                    'Accept': 'application/json',
                    'Content-Type': 'application/json',
                    'X-CSRFToken': csrfToken
                },
                body: JSON.stringify({
                    query: operation.text,
                    variables,
                }),
            })
            .then(response => {
                return response.json()
            })
            .catch(error => {
                console.log('POST ERROR', error)
            })
    });
    
    function getCsrfToken() {
        var url = 'http://' + ip + '/graphql';
        return CookieManager.get(url).then(response => {
            return response.csrftoken;
        });
    }
    

    【讨论】:

      【解决方案2】:

      添加这个是因为这是我在使用 Relay for Django + GraphQL 对 CSRF 进行故障排除时发现的最具体的问题

      即使我发布了 CSRF 令牌,我也收到了类似的 CSRF 错误响应。我必须添加到 fetch 标头以匹配我的 Django 后端的安全设置。在这种情况下,我是浏览器中的中继,所以我从 cookie 中获取 CSRF 令牌。

      我已经关注了CSRF with AJAX in cookies 的 Django 文档。由于我的安全设置,我不得不添加“同源”凭据。我将从 Relay 快速入门教程中标记我必须更改的几件事

      import { get as getCookie} from 'browser-cookies'
      
      return fetch('/graphql/', { // Added the trailing slash here 
        method: 'POST',
        credentials: "same-origin", // Added this line
        headers: {
          'Content-Type': 'application/json',
          'X-CSRFToken': getCookie('csrftoken'), // getting token from cookies
        },
        body: JSON.stringify({
          query: operation.text,
          variables,
        }),
      }).then(response => {
        return response.json();
      });
      

      这就是为我解决的问题。

      【讨论】:

        猜你喜欢
        • 2018-07-17
        • 2021-02-19
        • 2017-04-16
        • 1970-01-01
        • 2013-05-05
        • 2020-08-15
        • 2019-02-26
        • 1970-01-01
        • 2020-01-06
        相关资源
        最近更新 更多