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

2024-01-09

我正在开发一个旧的 php 应用程序,用户的密码是用md5()功能。所以密码的存储方式如下:

c0c92dd7cc524a1eb55ffeb8311dd73f

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


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

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。

但您确实必须考虑向所有用户发送链接,以确保他们更改密码。

EDIT:

为了进一步提高安全性,根据@martinstoeckli https://stackoverflow.com/users/575765/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');
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

将带有哈希密码的用户表从旧的 php 应用程序迁移到新的 laravel 应用程序 的相关文章

随机推荐