Rails中可能出现HTTP基本自定义错误?

use*_*385 4 ruby authentication ruby-on-rails basic-authentication

我正在使用Ruby 1.8.7在Rails 2.3.14中构建一个应用程序.

我的客户在网络研讨会页面上请求了一个非常简单的身份验证.我认为使用http_auth非常合适,因为它只需要一个非常基本的用户名和密码.

现在,她要求如果他们点击取消或使用错误的信息,他们会被重定向到一个基本上说"如果你忘记了登录信息,请联系我们"的页面.

当我们得到"HTTP Basic:Access denied"时,我该如何做到这一点.错误,我可以改为重定向到一个页面?或者,只需使用我们的自定义样式/内容自定义此页面?

提前致谢.

这是我的网络研讨会控制器的代码:

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
Run Code Online (Sandbox Code Playgroud)

mu *_*ort 5

如果你看一下这个authenticate_or_request_with_http_basic来源,你会看到:

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
Run Code Online (Sandbox Code Playgroud)

因此login_procedure,只要成功登录返回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
Run Code Online (Sandbox Code Playgroud)