PHP Yii2密码加密

PHP Yii2 Password Encryption

需要帮助,因为我还是 Yii2 的新手。我想在将密码保存到数据库之前对其进行加密。所以我正在使用 sha1 但问题是当我在如下所示的控制器中应用这行代码时,表单中的密码字段有内容。

$model->password = sha1($model->attributes['password']);

这是控制器创建方法:

public function actionCreate()
{
    $model = new Employeeinformation();

    //$model->password = sha1($model->attributes['password']);

    $model->created_date = date('Y-m-d H:i:s');

    if ($model->load(Yii::$app->request->post()) && $model->save()) {
        return $this->redirect(['view', 'id' => $model->employee_id]);
    } else {
        return $this->render('create', [
            'model' => $model,
        ]);
    }
}

这是表格:

<div class="employeeinformation-form">

<?php $form = ActiveForm::begin(); ?>

<?= $form->field($model, 'employee_id')->textInput(['minlength' => true, 'maxlength' => true]) ?>

<?= $form->field($model, 'password')->passwordInput(['maxlength' => true]) ?>

<?= $form->field($model, 'last_name')->textInput(['maxlength' => true]) ?>

<?= $form->field($model, 'first_name')->textInput(['maxlength' => true]) ?>

<?= $form->field($model, 'hired_date')->widget(\yii\jui\DatePicker::classname(), [
    'language' => 'en',
    'dateFormat' => 'yyyy-MM-dd',
]) ?>



<div class="form-group">
    <?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
</div>

<?php ActiveForm::end(); ?>

我的问题截图:

http://i.imgur.com/YTDW1Ud.png

提前致谢。

$password = md5($password);

最佳处理方式,确保将此与登录屏幕相关联以进行检查

$passwordentered = md5($passwordentered);
if ($passwordentered = "Correct"){
"Grant Access"
}

希望对您有所帮助。

Yii2 在高级设置中自带用户模块。查看它如何以加密方式存储用户密码。

您可以在用户模型中使用 setPassword() 方法来获取散列密码。

public function setPassword($password)
{
    $this->password_hash = Yii::$app->security->generatePasswordHash($password);
}

并在保存模型数据之前调用此方法。

public function signup()
{
    if ($this->validate()) {
        $user = new User();
        $user->username = $this->username;
        $user->email = $this->email;
        $user->setPassword($this->password);
        $user->generateAuthKey();
        if ($user->save()) {
            return $user;
        }
    }
    return null;
}

另请参阅 passwords and authentication 的 Yii2 文档。

在您的模型中添加:

public function beforeSave()
    {
        $this->password=md5($this->password);
        return true; 
    }

现在将其添加到您的控制器中:

$model->password = md5($model->password);

密码的内容在那里,因为您在通过保存(和验证)方法发送数据之前设置了属性。

如果你喜欢在controller里面做,你可以按照下面的方式做:

public function actionCreate()
{
    $model = new Employeeinformation();

    if ($model->load(Yii::$app->request->post())){

        $model->password = sha1($model->password);
        $model->created_date = date('Y-m-d H:i:s');
        if ($model->save())
            return $this->redirect(['view', 'id' => $model->employee_id]);
    } 
    return $this->render('create', [
        'model' => $model,
    ]);

}

另一种方法是在 Employeeinformation 模型的 beforeSave 方法中进行密码哈希处理(在模型中添加此方法 class):

public function beforeSave($insert) 
{
    if(isset($this->password))
        $model->password = sha1($model->password);
    $model->created_date = date('Y-m-d H:i:s');

    return parent::beforeSave($insert);
}

如果使用 beforeSave 方法完成,则可以删除控制器代码中的这两行,因为它们不再是必需的:

$model->password = sha1($model->password);
$model->created_date = date('Y-m-d H:i:s');

但参考http://www.yiiframework.com/doc-2.0/guide-security-passwords.html,不建议使用md5或sha1进行密码加密。 Yii2 提供了两个辅助函数来生成和验证密码。

用这个来加密密码:

$hash = Yii::$app->getSecurity()->generatePasswordHash($password);

并验证它:

if (Yii::$app->getSecurity()->validatePassword($password, $hash)) {
    // all good, logging user in
} else {
    // wrong password
}

与您发布的原始代码中使用的 sha1 相比,这是一个更好的选择。

I want to encrypt the password before saving it to the database.

不,你不知道。好吧,您可能认为您想要加密密码,但如果您试图保护用户,您实际上想要散列密码,而不是加密它。

SHA1 不提供加密,它是一种散列函数。 这是一个很常见的误解。您可以在链接的博客 post.

中了解有关 basic cryptography terms and concepts 的更多信息

更重要的是:您不需要像 SHA1 这样的快速散列密码。 使用 password_hash() and password_verify(),您将拥有安全的密码存储。您甚至不需要特别关心这些函数在内部做了什么就可以正确使用它们。

public function actionCreate()
{
    $model = new Employeeinformation();
    $post = Yii::$app->request->post();

    if ($model->load($post)) {
        $model->password = password_hash($model->password, PASSWORD_DEFAULT);
        $model->created_date = date('Y-m-d H:i:s');
        if ($model->save()) {
            return $this->redirect(['view', 'id' => $model->employee_id]);
        }
    }
    return $this->render('create', [
        'model' => $model,
    ]);
}

员工登录时,您只需要这样做:

if (password_verify($request->password, $storedEmployeeData->hashed_password)) {
    // Success
}

你可以看看User模型,例如有方法setPassword()

public function setPassword($password) { $this->password_hash = Yii::$app->security->generatePasswordHash($password); }

这是给数据库设置密码的方法,而且已经用yii2加密了