Django CSRF 框架有很多失败

发布于 2024-08-12 01:17:45 字数 184 浏览 2 评论 0 原文

我的站点上的 CSRF Django 中间件(来自 SVN trunk 的版本)出现了许多失败。我得到的唯一错误是: CSRF 失败:原因=CSRF 令牌丢失或不正确。

我如何诊断这些 CSRF 错误来自何处?我自己无法导致 CSRF 错误,但我将网站设置为每当触发 CSRF 错误视图时都会向我发送电子邮件,因此我知道这种情况经常发生。

I'm getting many failures from the CSRF Django middleware on my site (the version from SVN trunk.) The only errors I get are: CSRF failure: reason=CSRF token missing or incorrect.

How could I diagnose where these CSRF errors are coming from? I can't cause the CSRF errors myself, but I setup the site to email me whenever the CSRF error view is triggered so I know that it is happening often.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

窗影残 2024-08-19 01:17:46

我真的很努力地把它做好,但最终还是做到了。以下是我的主要问题(Django 1.2 beta):

  1. 根据您使用的 Django 版本,确保您的中间件内容正确。 Django 的在线文献对此进行了详细介绍。
  2. 确保每个表单中都有 {% csrf_token %},就在表单的开始标记之后
  3. 这是我的主要问题,确保所有表单都有一个转到页面,即不要执行操作= “以你的形式。
  4. 确保您的设置电子邮件都是正确的。我必须做这样的事情:

    EMAIL_HOST='mail.my-domain.com'
    EMAIL_HOST_USER='我在服务器上的用户名'
    EMAIL_HOST_PASSWORD='密码'
    EMAIL_PORT= '26' # 在我读到的许多论坛帖子中通常看起来是 25 或 26
    DEFAULT_FROM_EMAIL='[电子邮件受保护]' # 在托管域上,请确保它已设置并发送
    SERVER_EMAIL = '[电子邮件受保护]' # 与上面相同的电子邮件

  5. 将 request_context 添加到 render_to_response 的末尾
  6. return render_to_response('contact.htm',{'favicon':r'____.ico',
    'more_stuff':“……”
    'more_stuff':“……”
    'more_stuff':“……”
    },
    context_instance = RequestContext(request))

确保您

TEMPLATE_CONTEXT_PROCESSORS = (
     "django.contrib.auth.context_processors.csrf",
     .....
   )

的 settings.py 文件中

有:请注意,这实际上不是一个如何做,这只是我为了让我的工作而所做的。现在发布它的原因是我看到论坛上有很多人讨论这个主题,只是关闭 csrf_token。

I really struggled to get it right, but eventually did. Here were my main issues (Django 1.2 beta):

  1. Make sure your middleware stuff is right, according to the version of Django that you are using. This is well covered in Django's literature online.
  2. Make sure that you have the {% csrf_token %} in each form,just following the opening tag for the form
  3. This was my main problem, make sure that all your forms have an go-to page, i.e. don't do action="" in your form.
  4. Make sure that your settings emails are all the right ones. I had to do something like this:

    EMAIL_HOST='mail.my-domain.com'
    EMAIL_HOST_USER='my user name on the server'
    EMAIL_HOST_PASSWORD='passwd'
    EMAIL_PORT= '26' # often seems to be 25 or 26 on many of the forum posts I read
    DEFAULT_FROM_EMAIL='[email protected]' # on hosted domains, make sure it is set up and sending
    SERVER_EMAIL = '[email protected]' # Same email as above

    1. Add the request_context to the end of your render_to_response

    return render_to_response('contact.htm',{'favicon':r'____.ico',
    'more_stuff':"......"
    'more_stuff':"......"
    'more_stuff':"......"
    },
    context_instance = RequestContext(request))

Make sure you have:

TEMPLATE_CONTEXT_PROCESSORS = (
     "django.contrib.auth.context_processors.csrf",
     .....
   )

in your settings.py file.

Note that this is really not a how to, this is just what I did to get mine working. The reason for posting it now is that I see so many people on forums discussing this topic resort to just turning the csrf_token off.

嗼ふ静 2024-08-19 01:17:46

当中间件成功阻止跨站请求伪造攻击时,应该会发生 CSRF 错误。验证情况的最佳方法可能是检查您的 Web 服务器日志,并且您应该看到与早期请求无关的请求。

A CSRF error should happen when the middleware successfully stops a Cross Site Request Forgery attack. Probably the best way to verify that this is the case it to check your web server logs and you should see requests that aren't related to an earlier request.

沉溺在你眼里的海 2024-08-19 01:17:46

此外,您还应该检查 settings.py 文件中 MIDDLEWARE_CLASSES 的顺序。应该看起来像这样:

MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.locale.LocaleMiddleware',
)

最后的 LocaleMiddleware
对我来说,解决方案是 RequestContext 实例和排序。

Also you should check the order of the MIDDLEWARE_CLASSES in your settings.py file. Should look something like this:

MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.locale.LocaleMiddleware',
)

LocaleMiddleware at the end.
For me, the solution was the RequestContext instance and the ordering.

岁月如刀 2024-08-19 01:17:46

确保 GET 请求的视图函数如下所示:

def login_view():
c = {}
c.update(csrf(request))
request.session.set_expiry(0)
if request.method == 'GET':
  return render_to_response('newform.html',<b>c</b>)

然后检查 newform.html 的视图源,它必须有隐藏字段。

<`form action="" method="post" name="loginform"> <`div style='display:none'`><`input type='hidden' name='csrfmiddlewaretoken' value='6f4dee99ab2f5e7201e057cb63' />

在这里,action 可以引用同一页面,action=""

Make sure your view function for GET Request looks like this:

def login_view():
c = {}
c.update(csrf(request))
request.session.set_expiry(0)
if request.method == 'GET':
  return render_to_response('newform.html',<b>c</b>)

Then check the view source for your newform.html, it must have Hidden field.

<`form action="" method="post" name="loginform"> <`div style='display:none'`><`input type='hidden' name='csrfmiddlewaretoken' value='6f4dee99ab2f5e7201e057cb63' />

Here, action can refer the same page, action="".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文