2012-08-08 21 views
10

FINALLY找到了解決辦法:CakePHP的2.1製作與安全組件jQuery的Ajax調用激活

如果任何人有這個問題,把這個在您的beforefilter。

$this->Security->unlockedActions = array('givestar'); 

和更新庫到蛋糕2.3

問題:

我與安全組件blackholing我對我的ajax調用掙扎。

var id = 1;

$.ajax({ 
    type: "post", 
    url: "/messages/givestar/", 
    data: {"id" : id}, 
    dataType: "json" 
}); 

我只是嘗試發送ID爲控制器更新其中id = ID

但安全組件Blackholing我對我所有的Ajax調用的消息。

任何人都知道我可以如何使它與安全組件激活?

謝謝!

你太棒了!

-Tom

建議????

UPDATE2 我從黑洞的AUTH錯誤一些測試之後。

From Book: 
‘auth’ Indicates a form validation error, or a controller/action mismatch error. 

我有雙重檢查所有的ACO節點,他們是很好的。我在我的ajax調用中靠着來自安全組件的FORM VALIDATION ERROR。

UPDATE:

AppController.php

public $components = array(
     'Acl', 
     'Auth', 
     'Session', 
    'Security', 
    'Cookie' 
    ); 
public function beforeFilter() { 
    $this->Security->blackHoleCallback = 'blackhole'; 
} 
public function blackhole($type) { 
    $this->Session->setFlash(__('ERROR: %s',$type), 'flash/error'); 
} 

MessagesController.php

public $components = array('RequestHandler'); 

     public function beforeFilter() { 
      parent::beforeFilter(); 
     } 

public function givestar() { 
     $this->autoRender = false; 
      if ($this->request->is('ajax')) { 

       echo 'Working'; 
      } 
     return; 
    } 
+0

安全組件激活??意味着你想加密'id'或將其隱藏在ajax調用中? – coolguy 2012-08-08 17:01:59

+0

http://book.cakephp.org/2.0/en/core-libraries/components/security-component.html – Tom 2012-08-08 17:04:42

+0

aahhh我的不好。我不是一個蛋糕的PHP傢伙..我與Zend框架+ Jquery.There中有很多Cakephp專家..你會很快找到你需要的東西:) – coolguy 2012-08-08 17:06:20

回答

7

在beforefilter:

$this->Security->unlockedActions = array('givestar'); 
2

SecurityComponent行396:

if (!isset($controller->request->data['_Token'])) { 
    if (!$this->blackHole($controller, 'auth')) { 
     return null; 
    } 
} 

所以我想,如果你想保護這個動作必須用額外產生的「_Token」鍵發送數據。這個密鑰是使用Form-> secure($ fields)方法生成的(acctualy方法生成具有適當值的隱藏輸入)。