我正在使用 Ruby 1.8.7 在 Rails 2.3.14 中构建应用程序。
我的客户要求在网络研讨会页面上进行非常简单的身份验证。 我认为使用 http_auth 会非常合适,因为它只需要一个非常基本的用户名和密码。
现在,她要求如果他们点击取消或使用了错误的信息,他们将被重定向到一个页面,上面基本上写着“如果您忘记了登录信息,请联系我们。”
当我们收到“HTTP Basic:访问被拒绝”时,我该如何做到这一点?错误,我可以改为重定向到页面吗?或者,只是使用我们的自定义样式/内容自定义此页面?
提前致谢。
这是我的网络研讨会 Controller 的代码:
class WebinarsController < ApplicationController
before_filter :authenticate, :only => [:bpr]
def bpr
render :action => :bpr
end
protected
def authenticate
authenticate_or_request_with_http_basic do |username, password|
username == "abc" && password == "123"
end
end
end
最佳答案
如果您查看 authenticate_or_request_with_http_basic
source ,你会看到这个:
def authenticate_or_request_with_http_basic(realm = "Application", &login_procedure)
authenticate_with_http_basic(&login_procedure) || request_http_basic_authentication(realm)
end
def authenticate_with_http_basic(&login_procedure)
HttpAuthentication::Basic.authenticate(request, &login_procedure)
end
#...
def authenticate(request, &login_procedure)
unless request.authorization.blank?
login_procedure.call(*user_name_and_password(request))
end
end
所以您的 login_procedure
block 可以做几乎任何它想做的事情,只要它返回 true 表示成功登录。特别是,它可以调用 redirect_to
:
def authenticate
authenticate_or_request_with_http_basic do |username, password|
if(username == "abc" && password == "123")
true
else
redirect_to '/somewhere/else/with/instructions'
end
end
end
关于ruby-on-rails - Rails 中可能出现 HTTP 基本自定义错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7637356/