西纳特拉饼干在某些路线上消失了?

发布于 2024-12-16 18:32:37 字数 1047 浏览 0 评论 0原文

我正在使用一个简单的 Sinatra 应用程序,由于某种原因,cookie 似乎不适用于某些路线,我觉得这很奇怪。

require "sinatra"

set(:authenticate) do |*vars|
    condition do
        unless request.cookies.has_key?("TestCookie")
            redirect to("/login"), 303 
        end
    end
end

get "/login" do
    return "No valid cookie"
end

get "/secret", :authenticate => [:auth_cookie] do
    cookie = request.cookies["TestCookie"]
    return "Secrets ahoy - #{cookie}"
end

get '/cookie/set' do
    response.set_cookie("TestCookie", {
        :expires => Time.now + 2400,
        :value => "TestValue"
    })
    return "Cookie is set"
end

get '/cookie/get' do
    cookie = request.cookies["TestCookie"]
    return "Cookie with value #{cookie}"
end

如果我转到cookies/set,它会正确设置cookie(可以在firecookie中看到它),然后如果我转到cookies/get,我会得到正确的cookie输出。但是,如果我转到 /secret,它总是重定向到 /login。由于我对 Ruby 语法还很陌生,我认为这可能是我的身份验证扩展中的条件有问题,所以我尝试删除它,然后像另一个一样吐出 cookie。但是仍然什么都没有,所以我不知道为什么 cookie 在那里,我可以在浏览器中看到它......并且 /cookies/get 有效,但 /secret 不起作用......

我在这里遗漏了一些东西吗?

I have a simple Sinatra app that I am playing with, and for some reason the cookies don't seem to work for certain routes, which I find quite bizarre.

require "sinatra"

set(:authenticate) do |*vars|
    condition do
        unless request.cookies.has_key?("TestCookie")
            redirect to("/login"), 303 
        end
    end
end

get "/login" do
    return "No valid cookie"
end

get "/secret", :authenticate => [:auth_cookie] do
    cookie = request.cookies["TestCookie"]
    return "Secrets ahoy - #{cookie}"
end

get '/cookie/set' do
    response.set_cookie("TestCookie", {
        :expires => Time.now + 2400,
        :value => "TestValue"
    })
    return "Cookie is set"
end

get '/cookie/get' do
    cookie = request.cookies["TestCookie"]
    return "Cookie with value #{cookie}"
end

If I go to cookies/set it correctly sets the cookie (can see it in firecookie), then if I go to cookies/get I get the correct cookie output. However if I go to /secret it always redirects to the /login. As I am still fairly new to Ruby syntax I thought it may be a problem with my condition within the authenticate extension, so I have tried removing that and just spitting out the cookie like the other one does. However still nothing, so I am at a loss as to why the cookie is there, I can see it in the browser... and /cookies/get works, but /secret doesn't...

Am I missing something here?

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

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

发布评论

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

评论(1

放手` 2024-12-23 18:32:37

问题在于 cookie 是使用路径 /cookie 设置的。当您设置 cookie 时,您可以指定一个路径,该路径实际上是您希望应用 cookie 的网站的子部分。我猜 Sinatra/Rack 默认使用当前请求的路径,在 /cookie/set 中将是 /cookie

您可以通过显式指定路径使其按照您期望的方式工作:

  response.set_cookie("TestCookie", {
    :expires => Time.now + 2400,
    :value => "TestValue",
    :path => '/'
  })

或者您可以在名为 /cookie-set 而不是 /cookie/set 的路由上设置 cookie

The problem is that the cookie is set with path /cookie. When you set a cookie your can specify a path, which is effectively a sub-part of the Website that you want the cookie to apply to. I guess Sinatra/Rack use the path of the current request by default which in /cookie/set would be /cookie.

You can make it work the way you expect by explicitly specifying the path:

  response.set_cookie("TestCookie", {
    :expires => Time.now + 2400,
    :value => "TestValue",
    :path => '/'
  })

Or you could set the cookie at a route called say /cookie-set rather than /cookie/set

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