Rails authenticate_or_request_with_http_basic响应

时间:2016-09-23 13:54:33

标签: ruby-on-rails authentication

如果身份验证失败,我正在尝试重定向到root_path。 目前,它只是通过HTTP Basic更改页面主体:拒绝访问。

这是我的控制器:

class ProjectsController < ApplicationController
 before_filter :http_basic_auth, only: [:show]

  def http_basic_auth
    if (authenticate_or_request_with_http_basic do |user, password| password == @project.passcode && user == @project.passcode end if @project.private?)
    true
    else
      response.headers["Location"] = url_for(root_path)
    end
  end

end

研究了以下内容,但我仍然无法做到正确:
Rails: What is the location option for in the render method

http://api.rubyonrails.org/classes/ActionController/HttpAuthentication/Basic/ControllerMethods.html#method-i-authenticate_with_http_basic

http://guides.rubyonrails.org/v2.3.11/action_controller_overview.html#the-request-and-response-objects

1 个答案:

答案 0 :(得分:1)

我仍然无法让这个工作:

尝试使用authenticate_or_request_with_http_basic代替 if user = authenticate_with_http_basic { |u, p| @account.users.authenticate(u, p) } @current_user = user else request_http_basic_authentication end

Rails API documentation

中的示例摘录
android.provider.Telephony.SMS_RECEIVED