2014-08-27 39 views
0

我已經在cakePHP之前成功地開發了驗證機制,但是這次我不知道什麼是錯誤的,每次我都會提示錯誤的用戶名/密碼。我已經使用驗證成分,下面是詳細介紹:
型號名稱:用戶許可證
樣本用戶信息:用戶名:密碼ahmad_aghae10adc3949ba59abbe56e057f20f883e這是123456
MD5我不知道如果它在這種情況下很重要,但我有啓用管理員路由爲我的控制器。
AppController.php:cakephp驗證組件:每次都登錄失敗

class AppController extends Controller { 

    public $components = array('DebugKit.Toolbar', 
     'Auth' => array(
      'authenticate' => array(
       'Form' => array(
        'passwordHasher' => array(
         'className' => 'Simple', 
         'hashType' => 'sha256' 
        ) 
       ) 
      )), 
     'Cookie'); 

    public function beforeFilter() { 
     Security::setHash('md5'); 
     $this->Auth->loginRedirect = array('controller' 
      => 'licenses', 'action' => 'index'); 
     $this->Auth->logoutRedirect = array('controller' 
      => 'owners', 'action' => 'login'); 
     $this->Auth->allow('signup', 'confirm', 'login', 'logout', 'notauthorized', 'display'); 
     $this->Auth->authorize = array('controller'); 
     $this->set('loggedIn', $this->Auth->user('id')); 
     $this->Auth->userScope = array('User.activated' => '1'); 
     parent::beforeFilter(); 
    } 

    public function isAuthorized($user) { 
     // Here is where we should verify the role and give access based on role 

     return true; 
    } 

}

Login.ctp爲用戶查看

<div class="users form"> 
    <?php echo $this->Session->flash('auth'); ?> 
    <?php echo md5('136112'); ?> 
    <?php echo $this->Form->create('User', array('action' => 'login')); ?> 
    <fieldset> 
     <legend> 
      <?php echo __('لطفا نام کاربری و کلمه عبور را وارد کنید'); ?> 
     </legend> 
     <?php 
     echo $this->Form->input('username',array('label'=>'نام کاربری')); 
     echo $this->Form->input('password',array('label'=>'کلمه عبور')); 
     echo $this->Form->input('remember_me',array('label'=>'مرا به خاطر بسپار','type'=>'checkbox')); 
     ?> 
    </fieldset> 
    <?php echo $this->Form->end(__('ورود')); ?> 
</div> 

,這裏是UsersController.php的登錄()動作:

function login() { 
    if ($this->request->is('post')) { 
     if ($this->Auth->login()) { 
      /* if (!empty($this->data)) { 
       if (empty($this->data['User']['remember_me'])) { 
       $this->Cookie->delete('User'); 
       } else { 
       $cookie = array(); 
       $cookie['username'] = $this->data['User'] 
       ['username']; 
       $cookie['password'] = $this->data['User'] 
       ['password']; 
       $this->Cookie->write('User', $cookie, true, '+2 weeks'); 
       } 
       unset($this->data['User']['remember_me']); 
       } */ 
      $this->redirect($this->Auth->redirect()); 
     } else { 
      $this->Session->setFlash(__('Invalid username or password, try again')); 
     } 
    } 
} 

回答

0

你說你的密碼是MD5哈希,而在配置Auth你已經設置'hashType' => 'sha256'。所以散列類型的不匹配是非常明顯的。設置Security::setHash('md5')不會執行任何操作,因爲在Auth配置中設置的哈希類型將優先。您需要將hashType更改爲md5。還簡單地在db中保存密碼的md5散列將不起作用,因爲密碼散列器在散列之前將密碼(core.php中指定的Security.salt)附加到密碼。因此,(new SimplePasswordHasher)->hash('123456')獲得需要存儲在數據庫中的哈希值。所有這些在CakePHP手冊中都有解釋。