【问题标题】:How to config devise to accept auth token in HTTP header?如何配置设计以接受 HTTP 标头中的身份验证令牌?
【发布时间】:2012-06-12 11:07:49
【问题描述】:

目前,devise 配置为通过 URL 接受令牌身份验证,并且 curl 运行良好

curl 'http://localhost/index.json?auth_token=TOKENVALUE'

现在我想通过 HTTP 标头而不是 URL 传递 TOKENVALUE,我如何配置设计以从 either HTTP 标头 URL 获取 TOKENVALUE?这样上面和下面的 curl 请求都可以工作:

curl 'http://localhost/index.json' -H 'Authorization: Token token="TOKENVALUE"'

this railscast所示。

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 devise


    【解决方案1】:

    似乎设计中没有这样的配置。但是其他人有解决方案。请看Using auth_token from request headers instead from POST/PUT parameters with Rails 3 / devise

    【讨论】:

      【解决方案2】:

      首先将此添加到您的 gemfile https://github.com/stvp/devise_header_token 然后您可以在 config/initializers/devise.rb 中为其添加配置

      # Configuration for :token_authenticatable
      # Defines name of the authentication token params key
      config.token_authentication_key = 'AUTH-TOKEN'
      

      【讨论】:

        【解决方案3】:

        Devise 允许通过基本身份验证进行身份验证令牌身份验证。如果您查看source,您会看到:

        对于标头,您可以使用基本身份验证将令牌作为用户名和 空白密码。由于某些客户端可能需要密码,您可以将“X”传递为 密码,它将被忽略。

        【讨论】:

          【解决方案4】:

          自从提出这个问题以来,情况发生了变化,因为该设备不再具有内置的令牌身份验证功能。它被提取到一个单独的 gem,devise-token_authenticable。我正在使用该 gem,并希望与提出问题的人做同样的事情。

          我发现我必须在我的 config/initializers/devise.rb 中设置它:

          config.http_authenticatable = true

          我通过 curl 进行了尝试,它成功了。在我的 RSpec 测试中,我能够将令牌放在 HTTP 标头中,如下所示:

          user = FactoryGirl.create(:user)
          header = ActionController::HttpAuthentication::Token.encode_credentials(
            user.authentication_token)
          get "/api/v1/your_url", 
              { },
              { 
                'Accept' => 'application/json',
                'Authorization' => header
              } 
          

          希望这对那里的人有所帮助!

          【讨论】:

            【解决方案5】:

            【讨论】:

              猜你喜欢
              • 2023-03-09
              • 2017-08-12
              • 2012-12-12
              • 1970-01-01
              • 2015-05-08
              • 1970-01-01
              • 2021-08-17
              • 2011-11-06
              • 1970-01-01
              相关资源
              最近更新 更多