如何同时使用MD5和Bcrypt hashing登录Laravel



我有一个数据库,其中的密码存储是用MD5哈希。

现在我需要做一些步骤:

1.用户登录(使用bcrypt(

2.如果(登录失败(转到步骤3
否则loggin和退出

3.用户登录(使用MD5(

4. if(登录成功({通过BCRypt在数据库中更新哈希密码。}

结束

因此,我的系统需要在Loggin时检查MD5和BCRypt,

我该怎么做?

您可以使用自己的身份验证方法轻松完成这项工作,如Laravel文档中所述:https://laravel.com/docs/5.8/authentication#authenticating-users

<?php
namespace AppHttpControllers;
use IlluminateHttpRequest;
use IlluminateSupportFacadesAuth;
class YourCustomController extends Controller
{
    /**
     * Handle an authentication attempt.
     *
     * @param  IlluminateHttpRequest $request
     *
     * @return Response
     */
    public function login(Request $request)
    {
        if (Auth::attempt($request->only('email', 'password'))) {
            // Authentication passed...
            return redirect('other/path');
        }
        $user = AppModelsUser::where([ // This model use your second connection to the other database
            'email' => $request->email,
            'password' => md5($request->password)
        ])->first();
        if ($user) {
            $user->password = bcrypt($request->password);
            $user->save();
            $this->guard()->login($user);
            return redirect('other/path');;
        }
        return redirect('fail-path-with-instructions-for-create-account');
    }
}

我也建议您在您的routes/web.php文件中创建一个指定的dout,以重定向到您的新验证网址,这是因为在laraver中构建的许多机制都会自动地将用户自动重定向到正常的dout(例如重定向,如果未登录,在会话过期的情况下(。

Route::get('login', 'YourCustomController@login')->name('login');

最新更新