[英]CakePHP REST webservice and authentication, how to handle
我已遵循有關CAKEPHP中身份驗證的指南http://www.google.com/url?sa=D&q=http://book.cakephp.org/view/1250/Authentication
我遵循了有關如何設置REST Web服務的指南: http : //book.cakephp.org/view/1239/The-Simple-Setup
這正是我的app_controller:
class AppController extends Controller {
var $components = array('DebugKit.Toolbar', 'Session', 'Auth');
var $helpers = array('Html','Javascript', 'Session', 'Ajax', 'Facebook.Facebook');
function beforeFilter()
{
// importa modello Season
$this->Season = ClassRegistry::init('Season');
// ricava la variabile di sessione
$id = $this->Session->read('editable_season_id');
// verifica se tale id è esistente
$exist = $this->Season->exist($id);
// se non esiste o è un valore non valido o è nullo
if((is_null($id)) || (!is_numeric($id) || (!$exist)))
{
// cerca l'id della stagione più recente
$id = $this->Season->getLastSeasonId();
// se esiste assegnalo ad una var di sessione
if($id)
{
$this->Session->write("editable_season_id", $id);
$title = $this->Season->getSeasonName($id);
$this->Session->write("editable_season_title", $title);
$this->redirect($this->referer());
} else { // altrimenti lancia errore
$seasons = $this->Season->getSeasons();
$this->cakeError('defaultSesonNotFound', array('seasons' => $seasons));
}
}
}
}
現在,在每個控制器中,我都覆蓋了beforeFilter函數,並允許執行某些操作。 例如,我想與所有人共享我的REST,因此在我的控制器中:
function beforeFilter() {
parent::beforeFilter();
$this->Auth->allow('showMatchesBySeasonId', 'matchDetails', 'view');
}
其中view是我的REST服務的名稱。 第一次調用VIEW函數時,我將重定向到登錄操作。 如果我再次調用VIEW,它將被正確執行並顯示信息,直到名為“ CAKEPHP”的cookie存儲在我的計算機中(如果我刷新內存緩存,也將出現同樣的問題),這將是沒有問題的。 為什么? 如何避免這種行為?
問候!
我注意到Session組件發生了類似的問題。 嘗試改用Cookie。
class AppController extends Controller {
var $components = array('DebugKit.Toolbar', 'Cookie', 'Auth');
var $helpers = array('Html','Javascript', 'Ajax', 'Facebook.Facebook');
function beforeFilter()
{
// importa modello Season
$this->Season = ClassRegistry::init('Season');
// ricava la variabile di sessione
$id = $this->Cookie->read('editable_season_id');
// verifica se tale id è esistente
$exist = $this->Season->exist($id);
// se non esiste o è un valore non valido o è nullo
if((is_null($id)) || (!is_numeric($id) || (!$exist)))
{
// cerca l'id della stagione più recente
$id = $this->Season->getLastSeasonId();
// se esiste assegnalo ad una var di sessione
if($id)
{
$this->Cookie->write("Season.editable_id", $id);
$title = $this->Season->getSeasonName($id);
$this->Cookie->write("Season.editable_title", $title);
$this->redirect($this->referer());
} else { // altrimenti lancia errore
$seasons = $this->Season->getSeasons();
$this->cakeError('defaultSesonNotFound', array('seasons' => $seasons));
}
}
}
}
另外,您會注意到我將Cookie-> write()項設置為“ Season.editable_id”。 您可以使用“。”將sesssion / cookie用作多維數組。 我發現它是更好的做法,這樣您就不會覆蓋鍵/值對。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.