我需要在 Rails 3.1 API 中使用基于 token 的身份验证以及最新版本的设计。到目前为止没有问题。
现在我不想将我的 :auth_token 附加到客户端的 POST/PUT 参数,而是将此 token 作为请求 header 发送,如 HTTP_X_MYAPP_AUTH_TOKEN”。
我可以说服设计使用它而不是来自参数的 token 吗?是否可以同时实现,以便我的 API 用户可以通过请求 header 或 POST/PUT 参数发送 token ?
问候。菲利克斯
请您参考如下方法:
我有同样的需求并想出了这个解决方案:
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
注意我有我的设计
Devise.token_authentication_key
设置为
api_key
.
config.token_authentication_key = :api_key