2009-10-22 234 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