2012-06-21 22 views
1

我目前正在開發一個CakePHP應用程序,它當前具有表單身份驗證。我也想打開這個應用程序來通過REST連接到其他應用程序。CakePHP 2.1 - 作爲一個Web應用程序和REST服務與身份驗證

我知道的CakePHP會使用

Router::mapResources() 

Router::parseExtensions() 

不過,我不確定如何得到這個有工作說基本或摘要HTTP認證能夠做到這一點。

我得在AppController.php

public $components = array(
    'Session', 
    'Auth' => array(
     'authenticate' => array(
      'Form' 
     ), 
     'loginAction' => array(
      'admin' => false, 
      'controller' => 'users', 
      'action' => 'login' 
     ), 
     'loginRedirect' => array(
      'controller' => 'users', 
      'action' => 'home' 
     ) 
    ) 
); 

如果該身份驗證領域,我在「基本」有例如以下 - 登錄到基於Web版本時,我得到一個HTTP認證盒子,而不是基於網絡的表格。

這樣做的最好方法是什麼?我現在能想到的唯一方法是創建一個單獨的ApiController並手動進行身份驗證?

任何建議將是可怕的。

更新:

這是它給了我正確的水煤漿我修改後的代碼 - 我敢肯定,應該有更好的方式來做到這一點。

class AppController extends Controller { 

    public $components = array(
     'Session', 
     'RequestHandler', 
     'Auth' => array(
      'loginAction' => array(
       'admin' => false, 
       'controller' => 'users', 
       'action' => 'login' 
      ), 
      'loginRedirect' => array(
       'controller' => 'users', 
       'action' => 'home' 
      ) 
     ) 
    ); 

    public $helpers = array('Html', 'Form', 'Session'); 

    public function beforeFilter() { 
     $header = $_SERVER['HTTP_AUTHORIZATION']; 
     if($header) { 
      $this->Auth->authenticate = array('Basic'); 
     } 
    } 

} 

回答

1
public function beforeFilter() { 

    // Change the authentication if using REST 
    if($this->params['ext'] == 'json') { 
     $this->Auth->authenticate = array('Basic'); 
    } 

} 

這將檢查JSON擴展,如果請求包含它 - 然後切換到基本身份驗證。

0

Check the Authentication chapter in the CakePHP book.

CakePHP的支持形式,基本和摘要,你可以創建自己的驗證對象,並使用它們。

您可以加載多個認證對象,當第一個認證對象向認證處理程序返回真時,Cake會記錄用戶。你只有加載了表單驗證對象,不知道你爲什麼得到http驗證框。 Cake不太可能是問題。

+0

如果我將'Basic'或'Digest'添加到authenticate數組中,我知道CakePHP會根據數組中方法的順序來執行優先級。但是,如果我這樣做,那麼我會得到HTTP認證表單,這對於網絡我不想發生。 – Mezzair

相關問題