我目前正在開發一個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');
}
}
}
如果我將'Basic'或'Digest'添加到authenticate數組中,我知道CakePHP會根據數組中方法的順序來執行優先級。但是,如果我這樣做,那麼我會得到HTTP認證表單,這對於網絡我不想發生。 – Mezzair