2015-10-21 28 views
1

我使用插件BzUtils插件創建令牌進行身份驗證。 文件助推:如何使用BzUtils插件在cakephp2中使用Token進行身份驗證。

CakePlugin :: load('BzUtils');

文件的AppController:

public function beforeFilter() { 
     $this->Auth->authenticate = array(
      'Form' => array(
       'fields' => array(
        'username' => 'username', 
        'password' => 'password' 
       ), 
       'userModel' => 'User', 
       'scope' => array(
        'User.active' => 1, 
       ) 
      ), 
      'BzUtils.JwtToken' => array(
       'fields' => array(
        'username' => 'username', 
        'password' => 'password', 
       ), 
       'header' => 'AuthToken', 
       'userModel' => 'User', 
       'scope' => array(
        'User.active' => 1 
       ) 
      ) 
     ); 

和行動登錄在RestUsersController:

public function login() { 
     if ($this->Auth->login()) { 
      $user = $this->Auth->user(); 
      $token = JWT::encode($user, Configure::read('Security.salt')); 
      $this->set('user', $user); 
      $this->set('token', $token); 
      $this->set('_serialize', array('user', 'token')); 
     } else { 
      throw new NotAcceptableException(__('Email or password is wrong.')); 
     } 
    } 

地址:http://kcxcode1.dev/restusers/login

錯誤:致命錯誤:類 '智威湯遜' 不缶ND在C:\ XAMPP \ htdocs中\ koreconx \應用\控制器\ RestUsersController.php第42行

+0

這是你使用的插件嗎? https://github.com/burzum/cakephp-bz-utils – gmponos

+0

我使用的插件:https://github.com/burzum/cakephp-bz-utils/tree/develop –

+0

是的,我發現不同的分支。在這個插件/分支裏,靜態函數'encode'是什麼?你能找到它並把它鏈接到這裏嗎? – gmponos

回答

0

Error : Fatal error: Class 'JWT' not found in

該錯誤是很明顯的。 您需要在適配器內使用的php-jwt庫。通過作曲家添加它或手動添加它。

composer require firebase/php-jwt 

我已經更新了插件composer.json與depedency但只有建議它。此外,我更新了readme.md,在那裏添加了適配器,並鏈接到了一篇關於它的文章。我已經將開發合併爲主。

它看起來像你使用my article,它在第二段提到php-jwt是需要的,甚至鏈接到它。

+0

非常感謝,我會再試一次。智威湯遜支持cakephp2? –

+0

我不明白這個問題。 JWT和Cake沒有任何關係,lib是一個獨立的lib,可以和任何其他框架一起工作。他們彼此之間沒有很強的依賴關係。 – burzum

+0

sorrry你。我修復了錯誤 –

0

對不起,我錯了Declaration of JwtTokenAuthenticate::getUser() should be compatible with BaseAuthenticate::getUser($request) [APP\Plugin\BzUtils\Controller\Component\Auth\JwtTokenAuthenticate.php, line 123]

+0

這不是一個論壇,請更新您的問題或提出一個新的問題,這是一個不同的問題。這不是一個答案。我也不明白這個意思。簽名匹配。請參閱http://api.cakephp.org/2.7/source-class-BaseAuthenticate.html#215 – burzum

+0

我同意操作我有完全相同的問題。雖然文章**沒有***鏈接到jwt-php庫,但沒有解釋或例子說明如何將它包含在Cake App中。這篇文章和例子沒有提供完整的步驟(如上所述),這意味着試圖從各種源頭拼湊一些代碼,這是令人遺憾的,因爲這看起來像一個優雅的解決方案。 –