我曾经使用MD5来哈希我的密码,但后来了解到使用bcrypt更加安全。
使用MD5时,很容易检查表单中输入的密码是否正确。我只需要这样做:
if(md5($request->password) == $user->password)
//Login or whatever
那么我该如何使用bcrypt呢?我尝试了。
if(bcrypt($request->password) == $user->password)
但那并没有起作用。
使用attempt()
方法:
if (Auth::attempt(['email' => $email, 'password' => $password]))
attempt
方法接受一个键值对数组作为其第一个参数。数组中的值将用于在您的数据库表中查找用户。
https://laravel.com/docs/5.4/authentication#authenticating-users
在底层,attempt()
使用password_verify()
方法来检查密码。
Hash
门面的 check
方法。if (Hash::check($request->password, $user->password)) {
// The passwords match...
}
password_hash()
和password_verify()
。 - Alex Howansky