使用来自请求标头的auth_token而不是使用Rails 3/devise的POST/PUT参数

Geo*_*ieF 24 authentication ruby-on-rails token devise

我需要在Rails 3.1 API中结合最新版本的设备使用基于令牌的身份验证.到目前为止没问题.

现在我不想将我的:auth_token附加到客户端的POST/PUT参数,而是将此令牌作为请求标头发送,如HTTP_X_MYAPP_AUTH_TOKEN".

我可以说服设计使用它而不是参数中的令牌吗?是否可以实现这两者,以便我的API用户可以通过请求头或POST/PUT参数发送令牌?

问候.费利克斯

Chr*_*ley 38

我有同样的需求,并提出了这个解决方案:

class YourController < ApplicationController
  prepend_before_filter :get_api_key
  before_filter :authenticate_user!

  private
  def get_api_key
    if api_key = params[:api_key].blank? && request.headers["X-API-KEY"]
      params[:api_key] = api_key
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

请注意我有我的色器件Devise.token_authentication_key设置api_key.

config.token_authentication_key = :api_key
Run Code Online (Sandbox Code Playgroud)

  • 嗯对我不起作用: - ````get_api_key```在```authenticate_user!```之前正确执行但是遗憾的是,这次Warden Rack中间件已经自己解析了params而且有自己的params副本不会注意到添加:api_key项目. (2认同)