Warden auth失败后,Rails日志中没有状态代码

Fla*_*ino 8 logging rack ruby-on-rails warden

我已经设置了带有--api标志的Rails 5(5.0.0.rc1)应用程序.它使用Warden进行身份验证.

这一切都有效,除了当Warden身份验证失败时,响应未被正确记录.日志看起来像这样:

Started GET "/widgets.json" for ::1 at 2016-06-14 11:38:20 +0000
Processing by WidgetsController#index as JSON
Completed   in 0ms (ActiveRecord: 0.0ms)
Run Code Online (Sandbox Code Playgroud)

或者,在生产中:

I, [2016-06-14T14:12:54.938271 #17625]  INFO -- : [db39f895-eeb1-4861-91d0-5d52c124e37a] Completed   in 1ms (ActiveRecord: 0.0ms)
Run Code Online (Sandbox Code Playgroud)

它当然应该说Completed 401 Unauthorized in...,但无论出于何种原因,它都不知道响应的状态代码.

Warden身份验证错误被发送到Rack兼容的ActionController::Metal控制器,非常简单:

class UnauthorizedController < ActionController::Metal
  include ActionController::Head

  def self.call(env)
    @respond ||= action(:respond)
    @respond.call(env)
  end

  def respond
    head :unauthorized
  end
end
Run Code Online (Sandbox Code Playgroud)

它使用基本head方法来响应(不需要渲染任何东西),所以可能它的行为与head在常规Rails控制器中使用的行为相同.但不是.

如果我尝试使用redirect_to ...render ...(包括相关模块之后)也会发生同样的事情.所以在Rack→Rails→Warden→Warden failure app(控制器)的某个地方,响应的状态代码丢失了.日志知道开始记录请求,并知道它已被处理,因为它显然吐出了"已完成..." - 行.但有些东西没有正确连接.

有想法该怎么解决这个吗?

its*_*lay 2

典狱长没有rails记录器。它只是抛出一个异常并捕获它,before_failure Warden提供了一种通过回调 来处理这个异常的方法

# config/inializers/warden_config.rb
Warden::Manager.before_failure do |env, opts|
  Rails.logger.info("401 Unuthorized")
end
Run Code Online (Sandbox Code Playgroud)