从请求头,而不是从POST / PUT参数使用AUTH_TOKEN使用Rails 3 /色器件 [英] Using auth_token from request headers instead from POST/PUT parameters with Rails 3 / devise
问题描述
我需要在Rails 3.1的API结合最新版本的色器件使用基于令牌的认证。到目前为止,没有任何问题。
I need to use token based authentication in a Rails 3.1 API in conjunction with the most recent version of devise. No problem so far.
现在我不想我的追加。AUTH_TOKEN到客户端上的POST / PUT参数,但发送该令牌像HTTP_X_MYAPP_AUTH_TOKEN的请求头
Now I do not want to append my :auth_token to the POST/PUT parameters on client side but send this token as a request header like HTTP_X_MYAPP_AUTH_TOKEN".
我能说服从使用而不是从参数令牌设计?是否有可能同时实施,让自己的API,用户可以通过请求头或POST / PUT参数发送令牌?
Can I convince devise from using that rather than a token from parameters? Is it possible to implement both, so that my API users can send the token via request header OR POST/PUT parameter?
问候。菲利克斯
推荐答案
我有同样的需求,这种解决方案提出了:
I had the same need and came up with this solution:
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
。
Note I have my devise Devise.token_authentication_key
set to api_key
.
config.token_authentication_key = :api_key
这篇关于从请求头,而不是从POST / PUT参数使用AUTH_TOKEN使用Rails 3 /色器件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!