当涉及到在Sinatra应用程序中的一组受保护的URL(管理部分)后重定向时,我偶然发现了一些问题。这很可能是一个愚蠢的错误,但我还没有在网上找到任何有帮助的东西。
这是一个密码保护区域,如助手所示,用户可以在其中创建新事件。当用户第一次尝试访问管理员时,会提示他们输入密码,然后留下后续页面。我的问题是,当应用程序试图在成功的新事件后重定向时,用户必须重新定位自己.这看起来有点多余。
这也适用于删除和编辑过程,当尝试重定向时,用户总是会收到提示。我尝试将第二个参数的303传递给不同的HTTP代码,但没有结果。
不管怎么说,这是代码,任何问题/帮助都将不胜感激
helpers do
def protected!
unless authorized?
response['WWW-Authenticate'] = %(Basic realm="Restricted Area")
throw(:halt, [401, "Not authorized\n"])
end
end
def authorized?
@auth ||= Rack::Auth::Basic::Request.new(request.env)
@auth.provided? && @auth.basic? && @auth.credentials && @auth.credentials == ['admin', 'admin']
end
end
...
get "/admin/events/:id" do
protected!
conf = Conference.where(:_id => params[:id]).first
not_found unless conf
haml :admin_event_edit, :layout => :admin_layout, :locals => { :event => conf }
end
post "/admin/events/new/" do
protected!
conf = Conference.new(params[:event])
if conf.save!
redirect "/admin/events/"
else
"Something went horribly wrong creating the new event, heres the form contents #{params.inspect}"
end
end
get "/admin/events/" do
protected!
haml :admin_events, :layout => :admin_layout, :locals => { :our_events => Conference.where(:made => true).order_by(:start_date.asc).limit(15), :other_events => Conference.where(:made => false).order_by(:start_date.asc).limit(15)}
end发布于 2011-08-09 23:36:04
这只发生在Safari吗?
我使用了上面的代码,它只在Safari、Chrome和FireFox中按预期重新创作。
看起来,如果您不检查“记住我的用户名/密码”,Safari将发送每个后续请求,而不需要头中的授权(监视标题等的一个很好的工具是查尔斯)。如果你确实检查了它,那么苹果会正确地发送头中的Auth,即使你退出Safari,它也会继续记住在重新启动时发送Auth。
所以这是苹果的愚蠢,不是你:)
https://stackoverflow.com/questions/7003641
复制相似问题