Php 将md5迁移到Laravel中的哈希时出现错误
该算法可以工作,但一旦密码被转换为哈希并保存到数据库中,它就不会重定向到主页。相反,它会重定向到登录页面,指出登录凭据不正确。但如果我尝试登录,它是好的。我做错了什么 AuthenticatesUsers.phpPhp 将md5迁移到Laravel中的哈希时出现错误,php,laravel,hash,md5,Php,Laravel,Hash,Md5,该算法可以工作,但一旦密码被转换为哈希并保存到数据库中,它就不会重定向到主页。相反,它会重定向到登录页面,指出登录凭据不正确。但如果我尝试登录,它是好的。我做错了什么 AuthenticatesUsers.php protected function attemptLogin(Request $request) { $check = $this->guard()->attempt( $this->credentials($request), $reque
protected function attemptLogin(Request $request)
{
$check = $this->guard()->attempt(
$this->credentials($request), $request->has('remember')
);
if ($check === false)
{
$user = User::where('username','=',$request->input('username'))->first();
if(isset($user)) {
if($user->password == md5($request->input('password'))) { // If their password is still MD5
$hashed_password = Hash::make($request['password']); // Convert to new format
$user->password = $hashed_password;
$user->save();
return $this->guard()->attempt(
array(
'username'=>$request->input('username'),
'password'=>$hashed_password
), $request->has('remember')
);
} else {
// Redirect to the login page.
return false;
}
}
}
return $check;
}
尝试
不接受散列密码,而是接受从用户处获得的密码(纯文本密码)。用户不知道其密码的哈希版本,尝试
执行哈希检查,该检查需要纯文本版本
如果您已经验证了用户及其凭据,并且有一个代表他们的用户实例,那么您也不需要第二次调用trunt
。只需使用login
即可登录。您不必通过尝试
来重新查询数据库以获取用户,然后检查您知道的哈希值是否正确,因为您刚刚设置了它
在某种程度上,您拥有的代码的一部分只是重新创建尝试
在内部所做的事情
另外,您不需要为用户自己查询数据库。对尝试的第一次调用
将保留它在查询数据库时找到的“用户”。您可以从guard中检索它,这样就不必再次查询数据库,$this->guard()->getLastAttempted()
进行这些更改将消除第二次
尝试调用时出现的“错误凭据”问题,因为它将不再被调用。这也会将您的查询从3个选择和1个更新减少到1个选择和1个更新。(大致上)因此您可以想象用户可以使用md5哈希密码登录
?是的,因为我正在将当前md5替换为laravel的哈希。但问题是在那之后。所以您需要将数据库中现有的md5密码转换为laravel哈希
?如果是这样的话,试着说if(password\u verify($request->input('password'),$user->password))
。$request->session()->重新生成();$this->clearloginattests($request);Auth::login($user,true)代码>