如何同时使用MD5和Bcrypt哈希登录laravel
How to log in to laravel with MD5 and Bcrypt hashing at the same time
我有一个数据库,其中存储的密码是用 MD5 散列的。
现在我需要做一些步骤:
1.User 登录(使用 bcrypt)
2.If(登录失败)转步骤3
否则登录并退出
3.User登录(使用MD5)
4.If(登录成功){
通过 bcrypt 更新数据库中的散列密码。
}
结束
所以我的系统需要在登录时检查 MD5 和 bcrypt,
我该怎么做?
您可以使用自己的身份验证方法轻松完成此操作,如 laravel 文档中所述:
https://laravel.com/docs/5.8/authentication#authenticating-users
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class YourCustomController extends Controller
{
/**
* Handle an authentication attempt.
*
* @param \Illuminate\Http\Request $request
*
* @return Response
*/
public function login(Request $request)
{
if (Auth::attempt($request->only('email', 'password'))) {
// Authentication passed...
return redirect('other/path');
}
$user = \App\Models\User::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
文件中创建一个命名路由以重定向到你的新身份验证 URL,这是因为 laraver 中内置的许多机制会自动重定向你的用户到正常的路线(如未登录或会话过期时重定向)。
Route::get('login', 'YourCustomController@login')->name('login');
我有一个数据库,其中存储的密码是用 MD5 散列的。
现在我需要做一些步骤:
1.User 登录(使用 bcrypt)
2.If(登录失败)转步骤3
否则登录并退出
3.User登录(使用MD5)
4.If(登录成功){ 通过 bcrypt 更新数据库中的散列密码。 }
结束
所以我的系统需要在登录时检查 MD5 和 bcrypt,
我该怎么做?
您可以使用自己的身份验证方法轻松完成此操作,如 laravel 文档中所述: https://laravel.com/docs/5.8/authentication#authenticating-users
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class YourCustomController extends Controller
{
/**
* Handle an authentication attempt.
*
* @param \Illuminate\Http\Request $request
*
* @return Response
*/
public function login(Request $request)
{
if (Auth::attempt($request->only('email', 'password'))) {
// Authentication passed...
return redirect('other/path');
}
$user = \App\Models\User::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
文件中创建一个命名路由以重定向到你的新身份验证 URL,这是因为 laraver 中内置的许多机制会自动重定向你的用户到正常的路线(如未登录或会话过期时重定向)。
Route::get('login', 'YourCustomController@login')->name('login');