Yii2 - 加密数据库连接的密码

时间:2016-06-03 04:39:57

标签: yii2 yii2-basic-app

如何加密数据库密码?

return [
  'class' => 'yii\db\Connection',
  'dsn' => 'pgsql:host=localhost;port=5432;dbname=database',
  'username' => 'user',
  'password' => 'abcPassword',
  'charset' => 'utf8',
];

2 个答案:

答案 0 :(得分:0)

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;
}

另请查看密码=> http://www.yiiframework.com/doc-2.0/guide-security-passwords.html和的Yii2文档 认证=&GT。 http://www.yiiframework.com/doc-2.0/guide-security-authentication.html

答案 1 :(得分:0)

尝试通过这种方式..我没有尝过它......但可能对你有所帮助。

$database = 'mydb';
    $username = 'abc';
    $password = '1234';

$config['components'] = [
    'db' => [
        'class' => 'yii\db\Connection',
        'dsn' => 'mysql:host=192.168.*.*;dbname=' . $database,
        'username' => $username,

        'password' => md5($password);    // Try this 



        'password' => Yii::$app->security->generatePasswordHash($password); // Or Else try this

        'charset' => 'utf8',
    ]

    return config;