密码在 cakephp 中没有散列

发布于 2024-12-11 05:15:58 字数 1846 浏览 1 评论 0原文

以下代码无法对用户的密码进行哈希处理,而是以明文形式将密码存储在数据库中。更改密码后,我无法登录,因为密码需要采用哈希值。 以下代码在我的模型中。

'password_confirm'=>array(  
        'compare'    => array(
            'rule'      => array('password_match', 'password', true),
            'message'   => 'Password does not match',
            'required'  => true,
        ),
        'notempty' => array(
            'rule' => array('notempty'),
            'message' => 'Confirm password is empty',
            'allowEmpty' => false,
            'required' => true)
    ),

    'password'=>array(
        'notempty' => array(
            'rule' => array('notempty'),
            'message' => 'Password is empty',
            'allowEmpty' => false,
            'required' => true)
    )

function password_match($data, $password_field, $hashed = true)
    {
        $password         = $this->data[$this->alias][$password_field];
        $keys             = array_keys($data);
        $password_confirm = $hashed ?
              Security::hash($data[$keys[0]], null, true) :
              $data[$keys[0]];
        return $password === $password_confirm;
    }

以下代码在我的user_controller中

function change_password(){
        #CURRENTLY NOT WORKING
    $this->layout = "mainLayout";
    $in_user_id = $id = $this->Auth->user('id');

    if($this->data){
        $this->User->validate['password_confirm']['compare']['rule'] =
        array('password_match', 'password', false);

        $this->User->set($this->data);
        $this->User->useValidationRules('ChangePassword');
        if($this->User->validates()){
            $this->data['User']['id']=$in_user_id;
            $this->User->save($this->data,array('validate'=>false));
        }
    }
}

the following code is not able to hash the user's password, and it stores the password in clear text in the database. After changing the password, I am unable to log in as the password needs to be in hash.
The following code is in my model.

'password_confirm'=>array(  
        'compare'    => array(
            'rule'      => array('password_match', 'password', true),
            'message'   => 'Password does not match',
            'required'  => true,
        ),
        'notempty' => array(
            'rule' => array('notempty'),
            'message' => 'Confirm password is empty',
            'allowEmpty' => false,
            'required' => true)
    ),

    'password'=>array(
        'notempty' => array(
            'rule' => array('notempty'),
            'message' => 'Password is empty',
            'allowEmpty' => false,
            'required' => true)
    )

function password_match($data, $password_field, $hashed = true)
    {
        $password         = $this->data[$this->alias][$password_field];
        $keys             = array_keys($data);
        $password_confirm = $hashed ?
              Security::hash($data[$keys[0]], null, true) :
              $data[$keys[0]];
        return $password === $password_confirm;
    }

The following code is in my user_controller

function change_password(){
        #CURRENTLY NOT WORKING
    $this->layout = "mainLayout";
    $in_user_id = $id = $this->Auth->user('id');

    if($this->data){
        $this->User->validate['password_confirm']['compare']['rule'] =
        array('password_match', 'password', false);

        $this->User->set($this->data);
        $this->User->useValidationRules('ChangePassword');
        if($this->User->validates()){
            $this->data['User']['id']=$in_user_id;
            $this->User->save($this->data,array('validate'=>false));
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

万劫不复 2024-12-18 05:15:59

您的模型和验证函数仅检查password 和confirm_password 输入是否匹配。它在任何时候都不会改变数据来散列输入值。

验证输入后、保存模型之前,您需要对输入的密码进行哈希处理。像这样的事情:

$this->data[ 'User' ][ 'Password' ] = Security::hash( $this->data[ 'User' ][ 'Password' ], null, true );

Your model and validation function are only checking that the password and confirm_password inputs match. At no point does it alter the data to hash the input value.

After you validate your input, and before you save your model, you need to hash the password input. Something like this:

$this->data[ 'User' ][ 'Password' ] = Security::hash( $this->data[ 'User' ][ 'Password' ], null, true );
淡墨 2024-12-18 05:15:59

你不应该在 cake1.3 中使用字段名称“password”,因为它是自动的。
使用不同的字段并在保存之前重命名。

如果您想使用更干净的方法,请考虑使用以下行为:
http://www.dereuromark.de/2011/ 08/25/working-with-passwords-in-cakephp/

you shouldn't use the field name "password" in cake1.3 due to its automatic.
use a different field and rename it prior to saving.

if you want to use a cleaner approach, consider using a behavior:
http://www.dereuromark.de/2011/08/25/working-with-passwords-in-cakephp/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文