django配置-SESSION_COOKIE_SECURE = True
官方参考:https://docs.djangoproject.com/en/dev/topics/security/
#transfer cookie with https
SESSION_COOKIE_SECURE = True
If a browser connects initially via HTTP, which is the default for most browsers, it is possible for existing cookies to be leaked. For this reason, you should set your SESSION_COOKIE_SECURE and CSRF_COOKIE_SECURE settings to True. This instructs the browser to only send these cookies over HTTPS connections. Note that this will mean that sessions will not work over HTTP, and the CSRF protection will prevent any POST data being accepted over HTTP (which will be fine if you are redirecting all HTTP traffic to HTTPS).
如果浏览器最初通过 HTTP(大多数浏览器的默认设置)进行连接,则现有 cookie 可能会泄露。 因此,您应该将 SESSION_COOKIE_SECURE 和 CSRF_COOKIE_SECURE 设置设为 True。 这会指示浏览器仅通过 HTTPS 连接发送这些 cookie。 请注意,这意味着会话将无法通过 HTTP 工作,并且 CSRF 保护将阻止通过 HTTP 接受任何 POST 数据(如果您将所有 HTTP 流量重定向到 HTTPS,这会很好)。
这个配置项,我们一般在开发测试环境使用HTTP的话,我们需要把SESSION_COOKIE_SECURE 设置为False
SESSION_COOKIE_SECURE = True的话,接口返回的 set-cookie 字段是 secure 属性为 true,开发联调时,前端目前起本地服务如果没有用 https,浏览器的安全限制会导致本地 cookie 没有插进去。
#transfer cookie with https
SESSION_COOKIE_SECURE = False
经过测试,如果你开发测试环境使用http访问django, SESSION_COOKIE_SECURE 还是True的话,
就会报:
400 Bad Request
HTTP 400 Bad Request 响应状态码表示由于语法无效,服务器无法理解该请求。 客户端不应该在未经修改的情况下重复此请求。
状态
400 Bad Request
造成一些现象:开发测试环境,我们使用react 登录,出现登录秒退,就是该问题引起的!