gpt4 book ai didi

注销后Django无法删除csrftoken

转载 作者:行者123 更新时间:2023-12-01 05:17:24 25 4
gpt4 key购买 nike

我使用 varnish 作为 Django 应用程序的前端缓存。这一切都适用于 VCL 配置。我的问题是在用户注销后 csrftoken cookie 不会被删除,从那时起 Varnish 有一个 MISS 响应而不是一个 HIT。在stackoverflow上阅读了一些相关问题后,我有这个注销 View

def logout_view(request):
response = render_to_response('registration/logout.html', {}, context_instance=RequestContext(request))

if request.user.is_authenticated():
logout(request)

if request.GET.get('next', False):
response = HttpResponseRedirect(next)

response.delete_cookie('sessionid')
response.delete_cookie('csrftoken')
return response

以及用户点击注销页面后的响应 header
Response Headers
Age:0
Cache-Control:max-age=600
Connection:keep-alive
Content-Language:en
Content-Type:text/html; charset=utf-8
Date:Mon, 23 Sep 2013 09:20:43 GMT
Expires:Mon, 23 Sep 2013 09:30:43 GMT
P3P:CP="IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT"
Server:nginx/1.4.1
Set-Cookie:sessionid=; expires=Thu, 01-Jan-1970 00:00:00 GMT; Max-Age=0; Path=/
Set-Cookie:csrftoken=; expires=Thu, 01-Jan-1970 00:00:00 GMT; Max-Age=0; Path=/
Transfer-Encoding:chunked
Vary:Cookie, Accept-Language, Host
Via:1.1 varnish
X-Cache:MISS
X-Varnish:1950616479

default.vcl 以确保完整性:
backend default {
.host = "127.0.0.1";
.port = "8000";
}

sub vcl_recv {
set req.grace = 15s;

if (req.http.Cookie) {
set req.http.Cookie = regsuball(req.http.Cookie, "(^|; ) *__utm.=[^;]+;? *", "\1"); # removes all cookies named __utm? (utma, utmb...) - tracking thing
}

# unless sessionid/csrftoken is in the request, don't pass ANY cookies (referral_source, utm, etc)
if (req.request == "GET" && (req.url ~ "^/static" || (req.http.cookie !~ "flash_sessionid" && req.http.cookie !~ "csrftoken"))) {
remove req.http.Cookie;
}

# normalize accept-encoding to account for different browsers
# see: https://www.varnish-cache.org/trac/wiki/VCLExampleNormalizeAcceptEncoding
if (req.http.Accept-Encoding) {
if (req.http.Accept-Encoding ~ "gzip") {
set req.http.Accept-Encoding = "gzip";
} elsif (req.http.Accept-Encoding ~ "deflate") {
set req.http.Accept-Encoding = "deflate";
} else {
# unknown algorithm
remove req.http.Accept-Encoding;
}
}
}

sub vcl_fetch {
set beresp.ttl = 300s;
set beresp.grace = 15s;

# static files always cached
if (req.url ~ "^/static") {
unset beresp.http.set-cookie;
return (deliver);
}

# pass through for anything with a session/csrftoken set
if (beresp.http.set-cookie ~ "flash_sessionid" || beresp.http.set-cookie ~ "csrftoken") {
return (hit_for_pass);
} else {
return (deliver);
}
}

sub vcl_deliver {
# Add a header to indicate a cache HIT/MISS
if (obj.hits > 0) {
set resp.http.X-Cache = "HIT";
} else {
set resp.http.X-Cache = "MISS";
}
return (deliver);
}

在响应头中,我看到 Django 将 cookie 值设置为过去的日期,但是 csrftoken cookie 在下一个请求中仍然存在。

我还尝试删除 'django.middleware.csrf.CsrfViewMiddleware' 中间件,但 cookie 仍然存在。

最佳答案

您可以通过如下编辑 vcl_fetch 来解决此问题:

sub vcl_fetch {
# pass through for anything with a session/csrftoken set
if (beresp.http.set-cookie ~ "flash_sessionid" || beresp.http.set-cookie ~ "csrftoken" || beresp.http.set-cookie ~ "sessionid") {
return (hit_for_pass);
} else {
return (deliver);
}
}

这样你就可以检查 Set-Cookie:sessionid以及。

使用 beresp.http.set-cookie 时,Varnish 只能看到第一个 Set-Cookie header ,所以在你的情况下,Varnish 返回 vcl_deliver 而不是 hit_for_pass。

如需进一步阅读,我建议查看 vmod_header .

关于注销后Django无法删除csrftoken,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18956240/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com