需要帮助,因为我对Yii2还是新手。在将密码保存到数据库之前,我想对密码进行加密。所以我使用的是sha1,但问题是,当我在下面所示的控制器中应用这一行代码时,表单中的密码字段有内容。
$model->password = sha1($model->attributes'password');
这是Controller方法:
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
提前谢谢你。
发布于 2015-09-03 03:19:59
在将密码保存到数据库之前,我想对密码进行加密。
不,你没有。你可能认为你想加密密码,但是如果你想保护用户,你实际上想要散列密码,而不是加密它。
SHA1不提供加密,它是一个哈希函数。这是一个非常常见的误解。您可以在链接的博客文章中了解更多关于基本密码学术语和概念的信息。
更重要的是:对于密码,您不需要像SHA1这样的快速散列。使用password_hash()和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
}发布于 2015-09-02 05:13:50
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;
}还可以查看Yii2文档中的密码和身份验证。
发布于 2015-09-02 12:46:30
密码的内容是存在的,因为您在通过保存(和验证)方法发送数据之前设置了属性。
如果您喜欢在控制器中执行此操作,则可以按以下方式执行:
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方法中进行密码哈希(在模型类中添加此方法):
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更好的选择。
https://stackoverflow.com/questions/32343874
复制相似问题