【问题标题】:HTTP Basic authentication using Authlogic or authenticate_or_request_with_http_basic for API call?使用 Authlogic 或 authenticate_or_request_with_http_basic 进行 API 调用的 HTTP 基本身份验证?
【发布时间】:2010-04-24 05:37:53
【问题描述】:

我有一个 Rails 2.3.x 应用程序,根据 Authlogic Github 示例实现了用户模型中的 act_as_authentic 和 UserSession 模型。我正在实现一个 API 以允许从 iPhone 访问。将通过 https 使用 HTTP 基本身份验证(不会实现单一访问令牌)。每个 API 调用都需要用户名/密码才能访问。

例如,我可以通过调用 http://username:password@localhost:3000/books.xml 来访问 API。如果使用单一访问令牌,Authlogic 将不会持续存在。但我使用的是 HTTP Basic,我认为 Authlogic 将为 API 调用创建会话,这不用于我的 API 方法。因此,对于我进行的每个 API 调用,都会创建新的会话对象。因此在我看来,这会很快加载服务器资源。听起来是个坏主意。

另一种方法是为 API 控制器使用 Rails authenticate_or_request_with_http_basic。添加 before_filter 的示例:

def require_http_auth_user
    authenticate_or_request_with_http_basic do |username, password|
      if @current_user = User.find_by_email(username) 
        @current_user.valid_password?(password)
      else
        false
      end
    end
 end

这将绕过 Authlogic UserSession 并仅使用 User 模型。但这将涉及在应用程序中使用单独的身份验证代码。

谁有cmet,可以分享一下经验吗?谢谢

【问题讨论】:

    标签: ruby-on-rails authlogic basic-authentication


    【解决方案1】:

    最终让 UserSession 成为模型对象对我来说更容易。希望这可以帮助。链接中包含的相对有效使用的代码示例,如果有其他我可以帮助您的东西,请 lmk。

    http://www.corprew.org/blog/2010/01/27/authlogic-and-objectiveresource/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-13
      • 2016-07-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-12
      • 2015-09-07
      相关资源
      最近更新 更多