ruby-on-rails – rails-api通过标题的令牌认证

我想使用rails-api gem特殊来创建仅API应用程序.要提供认证机制,我想使用内建的authenticate_or_request_with_http_token方法描述在 Railscasts #352中,但这种方法在这里缺少.

有人在rails-api宝石上有经验吗?

附:我可以看到this approach,但是这个生产准备好了吗?

解决方法

我正在使用rails-api开发服务.我们还没有部署,但接近那个时候,在测试中没有任何问题.您需要包含任何您想要使用的非必要模块,因为rails-api正好被修剪.我正在ApplicationController中使用authenticate_or_request_with_http_token,如下所示:
include ActionController::HttpAuthentication::Token::ControllerMethods

def authenticate
  authenticate_or_request_with_http_token do |token,options|
    apiKey = ApiKey.where(auth_token: token).first
    @current_user = apiKey.user if apiKey
  end
end

如果你只是想要令牌,那么有一个方便的方法token_and_options:

include ActionController::HttpAuthentication::Token

def current_user
  api_key = ApiKey.where(auth_token: token_and_options(request)).first
  User.find(api_key.user_id) if api_key
end

相关文章

validates:conclusion,:presence=>true,:inclusion=>{...
一、redis集群搭建redis3.0以前,提供了Sentinel工具来监控各...
分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣...
上一篇博文 ruby传参之引用类型 里边定义了一个方法名 mo...
一编程与编程语言 什么是编程语言? 能够被计算机所识别的表...
Ruby类和对象Ruby是一种完美的面向对象编程语言。面向对象编...