加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 20:05:01 所属栏目:百科 来源:网络整理
导读:我想使用rails-api gem特殊来创建仅API应用程序.要提供认证机制,我想使用内建的authenticate_or_request_with_http_token方法描述在 Railscasts #352中,但这种方法在这里缺少. 有人在rails-api宝石上有经验吗? 附:我可以看到this approach,但是这个生产准
我想使用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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读