Authlogic - 通过基本的HTTP身份验证进行认证

3
我想使用“authenticate_with_http_basic”,但我无法让它正常工作。
在我的RoR应用程序中,Authlogic运作得很好,我正在使用User Sessions。现在,保持该方法不变,我需要使用authenticate_with_http_basic。我有一个iPhone SDK应用程序,现在我需要从我的Web应用程序中获取一些产品并显示为列表。因此,我假设我需要像这样向我的Web应用程序发送请求:http://username:password@192.168.1.9/products/ 因此,我的问题是如何验证这个用户名和密码,以及我需要对我的UserSession Controller进行什么操作?
2个回答

5

您无需对 UserSessionController 进行任何操作,因为该控制器仅处理登录表单提交和注销。

Authlogic 和 authenticate_with_http_basic 互不相关。如果您想通过 HTTP 基本身份验证进行认证,只需创建一个使用 Rails 提供的方法进行身份验证的方法,并将该方法放在 before_filter 中。通过 HTTP 认证登录,我假设用户名和密码应该对每个请求都是必需的。

因此,您的 ProductsController 应该像这样:

class ProductsController < ApplicationController
  before_filter :authenticate_via_http_basic

  # In case you have some method to redirect user to login page, skip it
  skip_before_filter :require_authentication

  ...

  protected

  def authenticate_via_http_basic
    unless current_user
      authenticate_with_http_basic do |username, password|
        if user = User.find_by_username(username)
          user.valid_password?(password)
        else
          false
        end
      end
    end
  end

嗨,现在这个功能已经可以使用了。但是在控制器上集成http_basic_athentication之后,UserSession基于身份验证现在无法工作。我总是收到http_basic_athentication登录提示。 - randika

1

现在,通过HTTP身份验证进行身份验证已经集成到AuthLogic中,并且默认情况下启用。


我在文档的方法列表中看到了authenticate_with_http_basic(&block)这个方法,但是我似乎无法弄清楚如何使用它。与Sikachu提供的解决方案相比,使用集成的HTTP身份验证的解决方案会是什么样子? - William Jones

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接