1

我有一个 Rails 2.3.x 应用程序,它根据 Authlogic Github 示例在 User 模型和 UserSession 模型中实现 act_as_authentic 。我正在实现一个 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 模型。但这将涉及在应用程序中使用单独的身份验证代码。

任何人有任何意见,可以分享他们的经验吗?谢谢

4

1 回答 1

1

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

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

于 2010-04-23T21:37:53.320 回答