0

我有用作 api 的 rails 应用程序,也使用设计身份验证,就 rails 应用程序而言,身份验证正常。但是在 Rails Api 的情况下,为什么我需要传递 auth_token。看起来传递 auth_token 需要自定义设计控制器,这似乎很痛苦。使用 rest 客户端在请求正文中传递 auth_token 允许我通过 Post 方法登录,但我没有找到任何回应。我需要定制设计控制器吗?

需要帮助谢谢,

4

1 回答 1

0

不想将您附加:auth_tokenPOST/PUT客户端的参数,而是将此令牌作为请求标头发送。

这就是你可以做到的-

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

并说您用于设计的身份验证密钥设置我们的 api 密钥,例如-

config.token_authentication_key = :api_key

您还可以像这样在设计中使用支持令牌 HTTP 标头身份验证的自定义策略

于 2013-02-12T15:08:02.223 回答