2009-10-22 324 views
3

我想使用“authenticate_ with_ http_ basic”,但我不能得到它 的工作。Authlogic - 通过基本HTTP身份验证进行身份验证

在我的RoR应用程序Authlogic工作正常,我正在使用用户会话。虽然保持该方法,因为它现在我需要使用authenticate_with_http_basic.I有一个iPhone SDK的应用程序,现在我需要从我的Web应用程序获取一些产品,并显示为列表。所以我假设我需要发送请求到我的webapp这样; http://username:[email protected]/products/

所以我的问题是验证这个用户名和密码,以及我需要做什么我的UserSession控制器?

回答

5

您不需要对UserSessionController做任何事情,因为该控制器只会处理登录表单提交和注销。

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

所以最后,你ProductsController会像通过HTTP验证这

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 
+0

嗨,这是现在的工作。但是在控制器上集成http_basic_athentication之后,基于UserSession的身份验证现在不起作用。我总是得到http_basic_athentication登录提示符。 – randika 2009-11-11 13:21:09

+0

对不起,延迟回复。我更新了答案:) – sikachu 2009-11-16 09:48:21

1

认证现已集成到AuthLogic,它是默认启用。

+0

我在文档的方法列表中看到此方法authenticate_with_http_basic(&block),但似乎无法弄清楚如何使用它。使用通过HTTP身份验证集成身份验证的解决方案与使用Sikachu提供的解决方案相比,会有什么结果? – 2010-02-16 17:56:35