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

发布于 2024-12-07 13:05:17 字数 650 浏览 0 评论 0原文

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

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

现在,她要求如果他们点击取消或使用错误的信息,他们会被重定向到一个页面,该页面基本上显示“如果您忘记登录信息,请联系我们”。

我该如何做到这一点,以便当我们收到“HTTP Basic:访问被拒绝”时。错误,我可以重定向到页面吗?或者,只需使用我们的自定义样式/内容自定义此页面?

提前致谢。

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

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

I am building an application in Rails 2.3.14 using Ruby 1.8.7.

My client has requested a very simple authentication on a webinars page.
I thought using http_auth would be very fitting, as it just needs a very basic username and password.

Now, she has requested that if they hit cancel or use the wrong information, they get redirected to a page that basically says "if you forget login information, contact us."

How do I make it so that when we get the "HTTP Basic: Access denied." error, I can instead redirect to a page? Or, instead, just customize this page with our custom styles/content?

Thanks in advance.

Here is the code from my webinars 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

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

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

发布评论

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

评论(1

狼性发作 2024-12-14 13:05:17

如果您查看 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

因此您的 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

If you look at the authenticate_or_request_with_http_basic source, you'll see this:

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

So your login_procedure block can do pretty much anything it wants as long as it returns true for a successful login. In particular, it can call 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文