我正在尝试让 Spring Security 的记住我功能与 Varnish 一起使用,但这似乎非常困难。使用常规登录很容易,我只需设置 Varnish 以绕过 j_spring_security_check URL 的缓存,但使用记住我,任何 URL 都可以作为登录的入口点。如果用户在打开浏览器时点击的第一件事是 Varnish 跳过的 URL(即绕过缓存),那么一切都很好,但是如果用户点击主页(或 Varnish 缓存的任何内容),就会发生一些奇怪的事情:用户确实登录了,我得到了一个 CookieTheftException ,因此记住我的 cookie 被取消了,因此无法进行进一步的自动登录。当我想到它时,听起来这两个(记住我和 Varnish)根本无法一起工作!这是真的吗?
任何想法可能出了什么问题?如何让 remember-me 与 Varnish 一起工作?哈希函数可能有问题吗?
我在下面发布了我的 Varnish 配置的一部分(跳过了散列函数定义,如果你认为它相关,请说出来):
sub vcl_recv {
# Forward IP to Apache log
unset req.http.X-Forwarded-For;
set req.http.X-Forwarded-For = client.ip;
if (req.http.host ~ "(?i)mysite\.com$") {
if (req.restarts == 0) {
set req.backend = mysite;
}
else {
error 750 "mysite";
}
}
# static content should always be cached
if (req.url ~ "\.(js|css|gif|jpg|jpeg|png|swf|flv|txt|pdf|mp3)$") {
unset req.http.Cookie;
return(lookup);
}
# only cache "get" or "head" requests
if (req.request != "GET" && req.request != "HEAD") {
return (pass);
}
# do not cache http authentication
if (req.http.Authorization || req.http.Authenticate) {
return (pass);
}
# do not cache Spring Security URLs, esi, personal pages etc.
if (req.url ~ "^(/logout|/j_spring_security_check|/personal/)" || req.url ~ "\?service=esi") {
return(pass);
}
return (lookup); # skip default vcl_recv
}
sub vcl_fetch {
# Try again if backend not responding
if (beresp.status != 200 && beresp.status != 403 && beresp.status != 404 && beresp.status != 301 && beresp.status != 302 && beresp.status != 401) {
return(restart);
}
# block sensitive files
if (req.url ~ "\.(bak|conf|config|ear|exe|gz|jar|log|old|properties|tar|tmp|tgz|war)$") {
error 405 "Not allowed";
}
# do esi processing for all non-static resources
if (req.url !~ "\.(js|css|gif|jpg|jpeg|png|swf|flv|txt|xml|html|htm|pdf|mp3|doc)$") {
esi;
}
# do not cache when told not to
if (req.http.Cache-Control ~ "no-cache") {
return (pass);
}
# do not cache Spring Security URLs, esi, personal pages etc.
if (req.url ~ "^(/logout|/j_spring_security_check|/personal/)" || req.url ~ "\?service=esi") {
set beresp.http.Cache-Control = "private, no-cache, no-store, must-revalidate";
set beresp.http.Pragma = "no-cache";
set beresp.http.Expires = "Sat, 01 Jan 2000 00:00:00 GMT";
return(pass);
}
# static content should always be cached
if (req.url ~ "\.(js|css|gif|jpg|jpeg|png|swf|flv|txt|xml|html|htm|pdf|mp3|doc)$") {
unset beresp.http.set-cookie;
set beresp.ttl = 1h;
} else {
set beresp.ttl = 300s;
}
}
更新:我在这里很好地记录了我的最终实现。