CakePHP和MD5登录系统

时间:2015-02-25 09:27:01

标签: cakephp cakephp-2.0 md5 cakephp-2.3

对于CakePHP应用程序,我必须使用MD5算法来存储登录密码。

我遇到的问题是我无法将正确提交的登录密码转换为md5。

例如,如果密码是欧元,那么它的md5是2acae8be44a70913481e66dd2cd3f761

但CakePHP将其转换为f85378844044678d28f1a1f306d4af50,由于用户无法登录应用程序(或者我认为这是一个问题)。

要启用MD5密码,请在AppControler中添加:

public function beforeFilter() {
    Security::setHash('md5');
}

另外,我尝试用以下方法检查登录功能:

function login() {

    if ($this->request->is('post')) {

        pr(AuthComponent::password($this->data['Company']['password']));
        // this returns f85378844044678d28f1a1f306d4af50

        // etc...

    }

}

你能不能给我一些线索,告诉我这件事我做错了什么?在这种情况下我必须使用MD5。

1 个答案:

答案 0 :(得分:0)

在此代码中,使用md5加密密码并与AuthComponent::password()进行比较,这两种方法在两种情况下使用AuthComponent::password()的方法都不同。

以下是如何使用的示例: -

在模型中添加以下代码行。 使用auth组件进行authpassword

App::uses('SimplePasswordHasher', 'Controller/Component/Auth');

在保存功能之前添加代码以加密密码: -

public function beforeSave($options = array()) {
if (isset($this->data[$this->alias]['password'])) {
$this->data[$this->alias]['password'] = AuthComponent::password(
$this->data[$this->alias]['password']
);
}
return true;
}

使用此模型功能来比较密码: -

public function checkCurrentPassword($data) {
$this->id = AuthComponent::user('id');
$password = $this->field('password');
return(AuthComponent::password($data['current_password']) == $password);
}
相关问题