Iphone发布到django web服务器会给出csrf验证错误

时间:2012-01-11 09:18:25

标签: iphone ios django

我正在尝试创建一个iphone应用程序,它将验证用户的登录信息。

然而,当我传入用户名和密码,并打印出从网上收到的jsonString的日志时,

我收到以下错误:

2012-01-11 13:44:51.470 Different Views[53942:18903] 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<head>
  <meta http-equiv="content-type" content="text/html; charset=utf-8">
  <meta name="robots" content="NONE,NOARCHIVE">
  <title>403 Forbidden</title>
  <style type="text/css">
    html * { padding:0; margin:0; }
    body * { padding:10px 20px; }
    body * * { padding:0; }
    body { font:small sans-serif; background:#eee; }
    body>div { border-bottom:1px solid #ddd; }
    h1 { font-weight:normal; margin-bottom:.4em; }
    h1 span { font-size:60; color:#666; font-weight:normal; }
    #info { background:#f6f6f6; }
    #info ul { margin: 0.5em 4em; }
    #info p, #summary p { padding-top:10px; }
    #summary { background: #ffc; }
    #explanation { background:#eee; border-bottom: 0px none; }
  </style>
</head>
<body>
<div id="summary">
  <h1>Forbidden <span>(403)</span></h1>
  <p>CSRF verification failed. Request aborted.</p>

</div>

<div id="info">
  <h2>Help</h2>

    <p>Reason given for failure:</p>
    <pre>
    CSRF token missing or incorrect.
    </pre>


  <p>In general, this can occur when there is a genuine Cross Site Request Forgery, or when
  <a
  href='http://docs.djangoproject.com/en/dev/ref/contrib/csrf/#ref-contrib-csrf'>Django's
  CSRF mechanism</a> has not been used correctly.  For POST forms, you need to
  ensure:</p>

  <ul>
    <li>The view function uses <a
    href='http://docs.djangoproject.com/en/dev/ref/templates/api/#subclassing-context-requestcontext'><code>RequestContext</code></a>
    for the template, instead of <code>Context</code>.</li>

    <li>In the template, there is a <code>{Jsrf_token
    }</code> template tag inside each POST form that
    targets an internal URL.</li>

    <li>If you are not using <code>CsrfViewMiddleware</code>, then you must use
    <code>csrf_protect</code> on any views that use the <code>csrf_token</code>
    template tag, as well as those that accept the POST data.</li>

  </ul>

  <p>You're seeing the help section of this page because you have <code>DEBUG =
  True</code> in your Django settings file. Change that to <code>False</code>,
  and only the initial error message will be displayed.  </p>

  <p>You can customize this page using the CSRF_FAILURE_VIEW setting.</p>
</div>

</body>
</html>

此错误来自我尝试访问的网页。

根据我自己的研究,我发现了类似的ans here,但我不知道如何设置/获取令牌

非常感谢任何帮助。谢谢。

1 个答案:

答案 0 :(得分:0)

来自“工作原理”下的Django CSRF docs

  

CSRF保护基于以下内容:

     
      
  1. 设置为随机值的CSRF cookie(与会话无关   nonce,因为它被称为),其他网站将无法访问。

         

    此cookie由CsrfViewMiddleware设置。这是永久的,   但由于没有办法设置一个永不过期的cookie,它就是   发送每个已拨打的回复   django.middleware.csrf.get_token()(内部使用的函数)   检索CSRF令牌。

  2.   
  3. 隐藏的表单字段中包含名称“csrfmiddlewaretoken”   传出的POST表单。该字段的值是CSRF的值   cookie中。

         

    这部分由模板标签完成。

  4.   
  5. 对于所有未使用HTTP GET,HEAD,OPTIONS的传入请求   或TRACE,必须存在CSRF cookie,并且'csrfmiddlewaretoken'   字段必须存在且正确。如果不是,用户将得到一个   403错误。

         

    此检查由CsrfViewMiddleware完成。

  6.   
  7. 此外,对于HTTPS请求,严格的引用检查由   CsrfViewMiddleware。这对于解决中间人问题是必要的   使用独立会话时在HTTPS下可能发生的攻击   nonce,因为HTTP'Set-Cookie'标题是   (不幸的是)被正在与网站交谈的客户接受   HTTPS。 (没有为HTTP请求进行引用检查,因为   在HTTP下,Referer头的存在不够可靠。)

  8.         

    这可确保只有源自您网站的表单   可用于POST数据。(强调我的)

您不能简单地发布用户名和密码,还必须在POST数据中包含CSRF令牌。花一些时间阅读文档并了解CSRF的工作原理。