在 Rails 3/devise 中使用来自请求标头的 auth_token 而不是 POST/PUT 参数 [英] 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
这篇关于在 Rails 3/devise 中使用来自请求标头的 auth_token 而不是 POST/PUT 参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!