【问题标题】:Rails authenticate_or_request_with_http_basic responseRails authenticate_or_request_with_http_basic 响应
【发布时间】:2016-09-23 13:54:33
【问题描述】:

如果身份验证失败,我会尝试重定向到 root_path。 目前,它只是使用 HTTP Basic: Access denied 更改页面的正文。

这是我的控制器:

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

【问题讨论】:

  • 你为什么不用redirect_to root_path
  • 我试过但没用
  • 它确实有效。您的情况可能有问题,而不是redirect_to
  • 当然,我会继续努力

标签: ruby-on-rails authentication


【解决方案1】:

我还没有让这个工作:

尝试使用authenticate_with_http_basic 代替authenticate_or_request_with_http_basic

来自Rails API documentation 的示例的 sn-p:

    if user = authenticate_with_http_basic { |u, p| @account.users.authenticate(u, p) }
      @current_user = user
    else
      request_http_basic_authentication
    end

【讨论】:

  • 这两个选项都有效,但如果用户关闭或取消弹出窗口,仍然无法重定向到根路径。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-08-05
  • 1970-01-01
  • 1970-01-01
  • 2012-01-26
  • 2015-06-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多