Laravel Auth - 使用 md5 而不是集成的 Hash::make()


Laravel Auth - use md5 instead of the integrated Hash::make()

所以,我正在为我的网站切换到laravel。我的旧网站目前拥有大约 500 个用户。每个用户都有一个附加的 md5 哈希作为密码(duh ^^)。

当我切换到 laravel 时,我希望使用 Auth::try不幸的是,它使用自己的方法来散列密码字符串。我不希望我的所有用户都更改他们的密码,因为我要切换到 laravel,是否可以让 Auth 类使用 md5 代替,这样我的用户就不必切换密码?:)

如果是,有人可以告诉我怎么做吗?

MD5已经过时了。我建议你不要试图保留它。相反,当用户首次登录并且Auth::attempt失败时,您应该尝试将其密码与数据库进行比较为 MD5

$user = User::where('username', '=', Input::get('username'))->first();
if(isset($user)) {
    if($user->password == md5(Input::get('password'))) { // If their password is still MD5
        $user->password = Hash::make(Input::get('password')); // Convert to new format
        $user->save();
        Auth::login(Input::get('username'));
    }
}

不要使用 md5 进行密码哈希。甚至 php 手册也警告不要这样做:"Warning It is not recommended to use this function to secure passwords, due to the fast nature of this hashing algorithm.".但是在您的情况下,您可以在项目中使用以下代码片段

    $user = User::where([ 
        'login_id'  => $request->login_id,
        'password'  => md5($request->password)
    ])->first(); 
    
    if ($user) { 
        Auth::login($user); 
        return redirect()->intended('home')->withSuccess('User Signed in');
    }