同一域上的多个Django站点-CSRF失败 [英] Multiple Django sites on the same domain - CSRF fails

查看:66
本文介绍了同一域上的多个Django站点-CSRF失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个应用程序在同一域的不同端口上运行,都使用csrf中间件。

I have two applications running on the same domain on different ports, both using csrf middleware.

当我登录其中一个应用程序时,所有 POST 从另一个失败提交。

我想是因为 SESSION_COOKIE_DOMAIN 是相同的。

When I log-in in one of the applications all POST submits from the other fail.
I presume because the SESSION_COOKIE_DOMAIN is the same.

我尝试更改 SESSION_COOKIE_NAME ,但是'csrftoken' cookie是在两个站点上都以 POST 形式的请求使用,无论现在是否有一个我指定名称的新cookie。

I tried changing SESSION_COOKIE_NAME, however, the 'csrftoken' cookie is used in the forms POST request on both sites, no matter that there now is a new cookie with the name I specified.

当我使用AJAX发布信息并从cookie中获取具有新名称的csrf令牌时,它可以工作,但是,表单提交失败且CSRF验证失败。

When I post information with AJAX and get the csrf token from the cookie with the new name - it works, however, form submits fail with CSRF verification failed.

推荐答案

默认情况下,CSRF令牌cookie的名称为csrftoken,但是您可以通过CSRF_COOKIE_NAME设置来控制cookie名称。 文档

The CSRF token cookie is named csrftoken by default, but you can control the cookie name via the CSRF_COOKIE_NAME setting. Docs.

为每个应用使用不同的CSRF cookie名称。

Use a different CSRF cookie name for each app.

这篇关于同一域上的多个Django站点-CSRF失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆