Ruby on rails I';I’我正试图解决一个问题;“过滤器链停止”;RubyonRails中的错误

Ruby on rails I';I’我正试图解决一个问题;“过滤器链停止”;RubyonRails中的错误,ruby-on-rails,Ruby On Rails,我正在接收的错误消息状态为: Filter chain halted as :authenticate_request rendered or redirected Completed 401 Unauthorized in 1ms 我正在尝试访问名为localhost:3000/items 我已经完全摆脱了动作之前的,并让它运行,但我需要身份验证才能工作 编辑 我通过在终端中执行以下操作来接收令牌: $ curl -H "Content-Type: application/json" -X

我正在接收的错误消息状态为:

Filter chain halted as :authenticate_request rendered or redirected
Completed 401 Unauthorized in 1ms
我正在尝试访问名为
localhost:3000/items

我已经完全摆脱了动作之前的
,并让它运行,但我需要身份验证才能工作

编辑
我通过在终端中执行以下操作来接收令牌:

$ curl -H "Content-Type: application/json" -X POST -d '{"email":"example@mail.com","password":"123123123"}' http://localhost:3000/authenticate
当我收到令牌时,我会尝试通过以下方式传递它:

$ curl -H "Authorization: 123EXAMPLETOKEN123" http://localhost:3000/items
这是我的应用程序控制器,我相信这是我的主要问题

class ApplicationController < ActionController::API
 before_action :authenticate_request
 attr_reader :current_user

 private

 def authenticate_request
  @current_user = AuthorizeApiRequest.call(request.headers).result
  render json: { error: 'Not Authorized' }, status: 401 unless @current_user
 end
end

看起来你需要在你的请求头中发送一个有效的JsonWebToken。请求中传递的头是什么?@TheGeorgeous:我以为传递了“授权”。我正在使用基于令牌的身份验证。然后我在下面使用该令牌:$curl-H“Authorization:token”@eikes:I通过在终端:$curl-H“Content-Type:application/json”-X POST-d'{“email”中执行以下操作来接收令牌:example@mail.com,“password”:“123123”},然后按以下格式发送该令牌:$curl-H“Authorization:token”本地主机:3000/items@howthegodschill,如果您在这段代码中记录接收到的标题并将其发布在此处,则会有所帮助。即使你传递了它,在你在Classic中收到它之前,标题也可能被篡改。看起来你需要在请求标题中发送一个有效的JsonWebToken。与请求一起传递的标题是什么?@TheGeorgeous:我以为传递了“授权”。我正在使用基于令牌的身份验证。然后我在下面使用该令牌:$curl-H“Authorization:token”@eikes:I通过在终端:$curl-H“Content-Type:application/json”-X POST-d'{“email”中执行以下操作来接收令牌:example@mail.com,“password”:“123123”},然后按以下格式发送该令牌:$curl-H“Authorization:token”本地主机:3000/items@howthegodschill,如果您在这段代码中记录接收到的标题并将其发布在此处,则会有所帮助。即使您传递了它,在类中接收它之前,标题也可能被篡改
class AuthorizeApiRequest
 prepend SimpleCommand

 def initialize(headers = {})
  @headers = headers
 end

 def call
  user
 end

 private

 attr_reader :headers

 def user
  @user ||= User.find(decoded_auth_token[:user_id]) if decoded_auth_token
  @user || errors.add(:token, 'Invalid token') && nil
 end

 def decoded_auth_token
  @decoded_auth_token ||= JsonWebToken.decode(http_auth_header)
 end

 def http_auth_header
  if headers['Authorization'].present?
   return headers['Authorization'].split(' ').last
  else
   errors.add(:token, 'Missing token')
  end
  nil
 end
end