将带有散列密码的用户表从旧的 php 应用程序迁移到新的 laravel 应用程序


Migrating users table with hashed password from old php app to new laravel app

我正在开发一个旧的php应用程序,用户的密码使用md5()函数进行哈希处理。因此,密码的存储方式如下:

c0c92dd7cc524a1eb55ffeb8311dd73f

我正在使用Laravel 4开发一个新应用程序,我需要有关如何在不丢失密码字段的情况下迁移users表的建议。

尽可能快地丢失密码字段,但如果您不想冒失去用户的风险,则可以在身份验证方法上执行以下操作:

if (Auth::attempt(array('email' => Input::get('email'), 'password' => Input::get('password'))))
{
    return Redirect::intended('dashboard');
}
else
{
    $user = User::where('email', Input::get('email'))->first();
    if( $user && $user->password == md5(Input::get('password')) )
    {
        $user->password = Hash::make(Input::get('password'));
        $user->save();
        Auth::login($user->email);
        return Redirect::intended('dashboard');
    }
}

这基本上会在用户每次登录时将密码从 md5 更改为 Hash。

但是您真的必须考虑向所有用户发送链接,以便他们更改密码。

编辑:

根据@martinstoeckli评论,为了进一步提高安全性,最好是:

对所有当前的 md5 密码进行哈希处理:

foreach(Users::all() as $user)
{
    $user->password = Hash::make($user->password);
    $user->save();
}

然后使用更干净的方法来更新密码:

$password = Input::get('password');
$email = Input::get('email');
if (Auth::attempt(array('email' => $email, 'password' => $password)))
{
    return Redirect::intended('dashboard');
}
else
if (Auth::attempt(array('email' => $email, 'password' => md5($password))))
{
    Auth::user()->password = Hash::make($password);
    Auth::user()->save();
    return Redirect::intended('dashboard');
}