【发布时间】: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