【问题标题】:Filter chain halted as :authenticate_user rendered or redirected过滤器链因 :authenticate_user 呈现或重定向而停止
【发布时间】:2018-10-08 23:15:28
【问题描述】:

此错误在生产服务器上突然开始。 在本地一切正常。

我正在使用我自己的基于身份验证令牌的架构。 authenticate_user 是检查当前用户是否通过身份验证的函数。

I, [2018-04-28T07:20:53.095743 #1063]  INFO -- : [af1e0a7c-41c9-4082-965a- 
3327aae9fd99] Processing by Api::V1::AnalysisController#analysis as JSON
I, [2018-04-28T07:20:53.095838 #1063]  INFO -- : [af1e0a7c-41c9-4082-965a- 
3327aae9fd99]   Parameters: {"customer_id"=>""}
I, [2018-04-28T07:20:53.098717 #1063]  INFO -- : [af1e0a7c-41c9-4082-965a- 
3327aae9fd99] [active_model_serializers] Rendered 
ActiveModel::Serializer::Null with Hash (0.18ms)
I, [2018-04-28T07:20:53.098997 #1063]  INFO -- : [af1e0a7c-41c9-4082-965a- 
3327aae9fd99] Filter chain halted as :authenticate_user rendered or 
redirected
I, [2018-04-28T07:20:53.099120 #1063]  INFO -- : [af1e0a7c-41c9-4082-965a- 
3327aae9fd99] Completed 401 Unauthorized in 3ms (Views: 2.8ms)

它在我运行的差异服务器上工作。 两者之间的唯一区别是 1. 我使用自定义异常来处理错误。并从控制器中救出它们。 2. Authnitcate_user 方法直接渲染,不引发任何异常。 3. 之前 authenitcate_user 在某些方法之前被调用。现在没有唯一的字段了。

可认证模块:

def authenticate_user
    if current_user
      true
    else
      render json: 'some error', status: 404
    end
end
def current_user
    @user = User.find_by(auth_token: request.headers['Authorization']) if request.headers['Authorization'].present?
    @user if @user && @user.token_expiry > Time.now
end

控制器:

class Api::V1::AnalysisController < ApplicationController

include Authenticable

before_action :authenticate_user

【问题讨论】:

  • 至少用完整的堆栈跟踪发布你的错误,以及一些代码来显示你正在尝试做什么。 stackoverflow.com/help/how-to-ask
  • 更新了问题。
  • 能否贴出调用失败方法的相关代码?
  • 代码发布@lacostenycoder
  • 如果request.headers['Authorization'].present? == false 会发生什么?

标签: ruby-on-rails ruby authentication


【解决方案1】:

不确定,但可以试试这个

def authenticate_user
  if current_user
     render json: 'success', status: 200
  else
    render json: 'error', status: 404
  end
end

def current_user
  return unless @user = User.find_by(auth_token: request.headers['Authorization'])
  @user if @user.token_expiry > Time.now
end

【讨论】:

  • 我注意到了一些事情。一些控制器方法有效。比如用户更新。其中before_action 具有only 属性
  • 如果您添加only 属性,它只会在您指定的那些上运行before_action。如果没有,它将在控制器调用的任何操作之前运行钩子。
【解决方案2】:

我发现了问题, 问题在于服务器的缓存和 cookie 处理。 在 CloudFront 中对其进行了编辑,能够再次捕获请求标头。

【讨论】:

    猜你喜欢
    • 2016-02-25
    • 2017-02-11
    • 2017-06-18
    • 1970-01-01
    • 2012-09-19
    • 2015-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多