我正在尝试在CakePHP中编写一个应用程序。我遇到了需要帮助的登录系统问题。
我设置了一个名为users的数据库表,其中包含字段id,username和password。密码的类型为varchar,长度为50。
我的User.php模型如下所示:
<?php
App::uses('AppModel', 'Model');
class User extends AppModel {
public $validate = array(
'id' => array(
'rule' => 'blank',
'on' => 'create'
),
'username' => array(
'alphaNumeric' => array(
'required' => true,
'rule' => 'alphaNumeric',
'message' => 'Alleen letters en cijfers zijn toegestaan'
),
'between' => array(
'rule' => array('between', 5, 20),
'message' => 'Gebruikersnaam moet tussen de 5 en 20 tekens zijn'
),
'notEmpty' => array(
'rule' => 'notEmpty',
'message' => 'Dit veld mag niet leeg zijn'
)
),
'password' => array(
'between' => array(
'rule' => array('between', 5, 50),
'message' => 'Wachtwoord moet tussen de 5 en 50 tekens zijn',
'required' => true
),
'notEmpty' => array(
'rule' => 'notEmpty',
'message' => 'Dit veld mag niet leeg zijn'
)
)
);
public function beforeSave($options = array()) {
if (isset($this->data['User']['password'])) {
$this->data['User']['password'] = AuthComponent::password($this->data['User']['password']);
}
return true;
}
}
?>
在我的UsersController.php中,我有这样的登录功能:
//login function. Logs in a user
public function login() {
//if already logged in
if ($this->Session->check('Auth.user')) {
$this->redirect(array('action' => 'index'));
}
$user = $this->User->findById(2);
debug($user);
if ($this->request->is('post')) {
debug($this->request->data['User']['password']);
debug($this->request->data);
debug(AuthComponent::password($this->request->data['User']['password']));
debug($user['User']['password']);
if ($user['User']['password'] == AuthComponent::password($this->request->data['User']['password'])) {
echo 'user pw == hashed request data pw <br />';
}
if ($this->Auth->login()) {
return $this->redirect($this->Auth->redirectUrl());
} else {
$this->Session->setFlash(__('Gebruikersnaam of wachtwoord is onjuist.'));
}
}
}
如您所见,我添加了大量调试信息。检查密码是否已经散列的检查与数据库中的密码相同甚至有效。
这是我的AppController.php中的Auth组件:
'Auth' => array(
'authenticate' => array('Form'),
'loginAction' => array(
'controller' => 'Users',
'action' => 'login'
),
'loginRedirect' => array(
'controller' => 'Users',
'action' => 'index'
),
'logoutRedirect' => array(
'controller' => 'Users',
'action' => 'login'
),
'authError' => 'U moet ingelogd zijn om deze pagina te bezoeken.'
)
问题是,一切似乎都是正确的,但它每次都会给我错误的用户名或密码错误消息。我甚至尝试过根本不加密密码,但这也不起作用。我做错了什么,我该如何解决?
P.S:我对cakephp并不是很有经验,所以如果我犯了一个明显的错误,我很抱歉。我在cakephp网站上使用了这个教程。修改 所以我正在改变一些调试打印以使用方法debug(),我还从auth组件中删除了某个部分,这与passwordhasher有关。我刚尝试再次登录,现在登录完全正常。我想这部分是导致问题存在的原因。
这是我的代码: 'Form'=&gt;阵列( 'passwordHasher'=&gt;阵列( 'className'=&gt; '简单', 'hashType'=&gt; 'SHA256' ) )
我将此更改为“表单”,现在可以正常工作。
答案 0 :(得分:0)
基本上,通过使用:
'authenticate' => array(
'Form' => array
'passwordHasher' => array(
'className' => 'Simple',
'hashType' => 'sha256'
)
)
),
您在登录操作中进行密码检查时告诉AuthComponent::login()
方法使用sha256 hashType。
但是,在User::beforeSave()
回调中,您使用旧的AuthComponent :: password()方法在数据库中散列并存储密码。此方法不接受hashType参数,并将hashType默认为sha1。如果您确实要继续使用sha256和上面的代码,请更改您的beforeSave()
方法,以使用新显示的SimplePasswordHasher
here。 (我假设你在Cake 2.4.x上)