【问题标题】:Hashing password when adding new user添加新用户时的哈希密码
【发布时间】:2013-09-18 06:29:04
【问题描述】:

CakePHP 2.4

添加新用户时,我必须先对密码进行哈希处理,然后才能将其存储到数据库中。为此,我做了:

//UsersController
public $helpers = array('Html', 'Form');
public $components = array(
    'Session',
    'Auth' => array(
    'loginRedirect' => array('controller' => 'users', 'action' => 'home', 'home'),
    'logoutRedirect' => array('controller' => 'users', 'action' => 'logout')
    )
);
public function add(){
    if($this->request->is('post')){
        $this->User->create();
        if($this->User->save($this->request->data)){
            $this->Session->setFlash('Saved!');
        }
    }
}

//UserModel
public function beforeSave(){
    if(isset($this->data[$this->alias]['password']))
        $this->data[$this->alias]['password'] = md5($this->data[$this->alias]['password']);
    return true;
}

//add.ctp
echo $this->Form->create();
echo $this->Form->input('text', array('label'=>'Username', 'name'=>'username'));
echo $this->Form->input('text', array('label'=>'Full name', 'name'=>'fullname'));
echo $this->Form->input('password', array('label'=>'Password', 'name'=>'password'));
echo $this->Form->input('text', array('label'=>'Password hint', 'name'=>'pass_hint'));
echo $this->Form->input('text', array('label'=>'Email', 'name'=>'email'));
echo $this->Form->input('text', array('label'=>'Contact', 'name'=>'cell'));
echo $this->Form->end('Create account');

但问题是密码在没有经过哈希处理的情况下被存储!

提前谢谢...

更新:将视图代码更改为

echo $this->Form->create('User');
echo $this->Form->input('username', array('label'=>'Username'));
echo $this->Form->input('fullname', array('label'=>'Full name'));
echo $this->Form->input('pwd', array('label'=>'Password', 'type'=>'password'));
echo $this->Form->input('pass_hint', array('label'=>'Password hint'));
echo $this->Form->input('email', array('label'=>'Email'));
echo $this->Form->input('cell', array('label'=>'Contact'));
echo $this->Form->end('Create account');

【问题讨论】:

  • 你没有加密它,你正在散列它。这是不同的 :) 提示:请参阅 dereuromark.de/2011/08/25/working-with-passwords-in-cakephp
  • 好文章。但是这个时间表在密码字段中什么也没有。 beforeSave() 是if (!empty($this->data[$this->alias]['pwd'])) { $this->data[$this->alias]['password'] = AuthComponent::password($this->data[$this->alias]['pwd']); }
  • 为什么要使用 md5?您至少应该使用 sha1 - 甚至像河豚这样的新标准之一可能会更好。如果您想这样做,您还需要命名您的字段 pwd(您不能/不应该仍然将其命名为密码)。如果您无法使其正常工作,请使用我的行为来获得更干净、更安全的方法。
  • 是的,我也重命名了我的视图。我尝试了你的方式,但仍然没有运气。我认为我的 beforeSave() 没有被执行。那可能吗?我检查了 pr($this->data) 和 debug($this->data) 但没有打印,我的调试级别是 2。
  • 我想提一提的是 AuthComponent::password() 已被弃用,你应该在 cake 2.4 中使用 Security::hash()

标签: cakephp encryption cakephp-2.4


【解决方案1】:

我不知道为什么,但我刚刚用用户表烘焙了一个项目,发现 CakePHP 2.4 将模型命名为 User 而不是 UserModel。所以,我将我的从 UserModel 重命名为 User 并且神奇地为我工作:)

【讨论】:

  • 啊,是的,就像你没有注意到一样,它们遵循 Java 的做法——类名应该与文件名相同,例如,User.php,你可以使用 @987654326 @等
【解决方案2】:

您正在通过在输入类型中设置名称属性来更改默认行为,

去掉name属性就可以了,

在您的用户模型 beforesave 功能中,您正在检查

if(isset($this->data[$this->alias]['password']))

由于您正在设置这样的密码,因此不再存在

echo $this->Form->input('password', array('label'=>'Password', 'name'=>'password'));

改成

echo $this->Form->input('User.password', array('label'=>'Password'));

更新

并且像这样更新表单然后它会完美地工作。

echo $this->Form->create('User');
echo $this->Form->input('username', array('label'=>'Username'));
echo $this->Form->input('fullname', array('label'=>'Full name'));
echo $this->Form->input('password', array('label'=>'Password', 'name'=>'password'));
echo $this->Form->input('pass_hint', array('label'=>'Password hint'));
echo $this->Form->input('email', array('label'=>'Email'));
echo $this->Form->input('cell', array('label'=>'Contact'));
echo $this->Form->end('Create account');

【讨论】:

  • 如果我想要自定义标签怎么办?
  • 您使用array('label' => 'name')设置的自定义标签
  • 哎呀,这对我仍然不起作用。我尝试使用和不使用'name'=>'password',但没有运气。这是正在呈现的视图 html 源代码<div class="input password"><label for="UserPassword">Password</label><input name="data[User][password]" type="password" id="UserPassword"/></div>
  • 只需通过执行pr($this->data) 来检查您在beforeSave 回调中获得的数据。
  • 将你的调试设置为 2 然后尝试,pr 仅在调试模式为 2 时有效或尝试print_r($this->data)
【解决方案3】:

您忘记了添加操作的重要部分:

public function add(){
    if ($this->request->is('post')){
        $this->User->create(); // important!
        if ($this->User->save($this->request->data)){
            $this->Session->setFlash('Saved!');
        }
    }
}

它可能适用于您的情况(有时),但很容易中断,一旦您在 beforeFilter() 回调等中执行某事

【讨论】:

  • 我后来添加了它(忘记更新我的问题)但没有用。
猜你喜欢
  • 2023-04-08
  • 1970-01-01
  • 2011-12-06
  • 1970-01-01
  • 2019-04-21
  • 2014-07-15
  • 2012-08-02
  • 1970-01-01
  • 2021-07-22
相关资源
最近更新 更多