ruby-on-rails - Rails 中可能出现 HTTP 基本自定义错误?

标签 ruby-on-rails ruby authentication basic-authentication

我正在使用 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/

相关文章:

asp.net - Page.User.Identity.IsAuthenticated 返回未设置到对象实例的对象引用

javascript - 根据所选下拉列表更改标签 [Ruby on Rails 4.2]

ruby-on-rails - Ruby - 嵌套 IF 语句语法

ruby-on-rails - cat ~/.gemrc 结果为 "no such file or directory"

ruby-on-rails - Ruby:不区分大小写的数组比较

java - Dropwizard 摘要认证

ruby - 如何使用正则表达式将数字匹配为整数?

ruby - 我怎样才能将 ruby​​ logger 日志输出到 stdout 和文件?

ruby-on-rails - 迁移错误

node.js - 在 NodeJS 应用程序中存储远程 API 的身份验证 token 的最佳方法是什么?