简体   繁体   English

CakePHP REST Web服务和身份验证,如何处理

[英]CakePHP REST webservice and authentication, how to handle

I've followed the guide about authentication in CAKEPHP http://www.google.com/url?sa=D&q=http://book.cakephp.org/view/1250/Authentication 我已遵循有关CAKEPHP中身份验证的指南http://www.google.com/url?sa=D&q=http://book.cakephp.org/view/1250/Authentication

And I followed the guide on how to set REST webservice: http://book.cakephp.org/view/1239/The-Simple-Setup 我遵循了有关如何设置REST Web服务的指南: http : //book.cakephp.org/view/1239/The-Simple-Setup

This is exactly my app_controller: 这正是我的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));
            }       
        }
     }
}

Now in every controllers I overwrite beforeFilter function, and allow some action. 现在,在每个控制器中,我都覆盖了beforeFilter函数,并允许执行某些操作。 For example I want to share my REST with everyone, so in my controller: 例如,我想与所有人共享我的REST,因此在我的控制器中:

function beforeFilter()    {
   parent::beforeFilter();
   $this->Auth->allow('showMatchesBySeasonId', 'matchDetails', 'view');
}

where view is the name of my REST service. 其中view是我的REST服务的名称。 The first time I call VIEW function I will redirected to the login action. 第一次调用VIEW函数时,我将重定向到登录操作。 If I call again VIEW it will be executed without problem and info is showed, and there will be no problem until a cookie named "CAKEPHP" will be stored in my computer (if I flush memory cache there will be the same problem). 如果我再次调用VIEW,它将被正确执行并显示信息,直到名为“ CAKEPHP”的cookie存储在我的计算机中(如果我刷新内存缓存,也将出现同样的问题),这将是没有问题的。 Why? 为什么? How can I avoid this behavior? 如何避免这种行为?

Regards! 问候!

I've noticed issues like this happening with the Session component. 我注意到Session组件发生了类似的问题。 Try using Cookie instead. 尝试改用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));
            }       
        }
     }
}

Also, you'll notice that I set your Cookie->write() key to "Season.editable_id". 另外,您会注意到我将Cookie-> write()项设置为“ Season.editable_id”。 You can use sesssion/cookie as a multi-dimensional array using the ".". 您可以使用“。”将sesssion / cookie用作多维数组。 I find it better practice so that you're not overwriting key/val pairs. 我发现它是更好的做法,这样您就不会覆盖键/值对。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM