Ruby on rails Rails筛选请求对象的特定头
由于Desive不支持令牌身份验证,我一直在开发自己的身份验证系统。然而,我注意到,如果我在请求中传递Ruby on rails Rails筛选请求对象的特定头,ruby-on-rails,devise,postman,Ruby On Rails,Devise,Postman,由于Desive不支持令牌身份验证,我一直在开发自己的身份验证系统。然而,我注意到,如果我在请求中传递authentication\u-token或access\u-token头,它们可能会在rails中被过滤。这意味着类似于p request.headers['authentication\u token']及其几乎任何变体都返回nil。但是,名为:omfgwork的标题可以工作。有人能解释一下为什么会发生这种情况,并将我链接到一个可以找到过滤标题列表的源吗 编辑-我正在使用postman将请
authentication\u-token
或access\u-token
头,它们可能会在rails中被过滤。这意味着类似于p request.headers['authentication\u token']
及其几乎任何变体都返回nil。但是,名为:omfgwork
的标题可以工作。有人能解释一下为什么会发生这种情况,并将我链接到一个可以找到过滤标题列表的源吗
编辑-我正在使用postman将请求发送到我的rails应用程序
编辑2
class ApplicationController < ActionController::Base
include Pundit
protect_from_forgery with: :null_session
rescue_from Pundit::NotAuthorizedError, with: :user_not_authorized
protected
def authenticate_user_from_token!
100.times {print '|'}
p ''
p request.headers[:uid] # works
p request.headers[:authentication_token] # returns nil
p request.headers[:access_token] # returns nil
p request.headers['omfgwork'] # works
p request.headers[:omfgwork] # works
100.times {print '|'}
p ''
# return if request.headers[:uid].nil? or request.headers[:access_token].nil?
user_email = request.headers[:uid].presence
user = user_email && User.find_by_email(user_email)
# Notice how we use Devise.secure_compare to compare the token
# in the database with the token given in the params, mitigating
# timing attacks.
if user && Devise.secure_compare(user.authentication_token, request.headers[:access_token])
sign_in user, store: false
end
end
def user_not_authorized(exception)
policy_name = exception.policy.class.to_s.underscore
render json: "#{policy_name}: Not authorized", status: :forbidden
end
end
事实证明,我无法使用符号或字符串表示直接访问任何带有下划线的标题。在迭代
request.headers
hash时,我发现请求中发送的头在被大写后会以HTTP\uucode>作为前缀。因此,在我的例子中,可以使用rails应用程序中的request.headers[:HTTP\u access\u token]
或request.headers['HTTP\u access\u token']
来访问随请求发送的access\u token
但是,如果标题是单个单词的形式,如kites
,则可以使用请求直接访问它。结果是,我无法使用符号或字符串表示直接访问任何带有下划线的标题。在迭代request.headers
hash时,我发现请求中发送的头在被大写后会以HTTP\uucode>作为前缀。因此,在我的例子中,可以使用rails应用程序中的request.headers[:HTTP\u access\u token]
或request.headers['HTTP\u access\u token']
来访问随请求发送的access\u token
但是,如果标题是单个单词的形式,如kites
,则可以使用请求直接访问。标题[:kites]
您能告诉我们您是如何提出此请求的吗?@RyanBigg更新了问题!您正在使用Desive并构建自己的身份验证?@jdgray仅用于令牌身份验证,如指南中所链接。这并不比已经有的多,只是一些调整。你能用你的相关控制器更新吗?你能告诉我们你是如何提出这个请求的吗?@RyanBigg更新了这个问题!您正在使用Desive并构建自己的身份验证?@jdgray仅用于令牌身份验证,如指南中所链接。它并没有比现在更多,只是一些调整。你能用你的相关控制器更新吗?
before_action :set_article, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user_from_token!, except: [:index, :show]
before_action :authenticate_user!, except: [:index, :show]