-1
我正在使用Laravel 5.1,並且需要使用具有自己的密碼算法的現有用戶表。 經過數小時和數小時的研究,我找到了解決方案,下面是步驟。 希望這可以幫助Laravelers。在Laravel中自定義身份驗證
我正在使用Laravel 5.1,並且需要使用具有自己的密碼算法的現有用戶表。 經過數小時和數小時的研究,我找到了解決方案,下面是步驟。 希望這可以幫助Laravelers。在Laravel中自定義身份驗證
在config/auth.php文件中, 將驅動程序值設置爲自定義。喜歡這個。
...
'driver' => 'custom',
...
在app/Auth目錄下創建一個文件CustomUserProvider.php。
<?php
namespace App\Auth;
use App\Model\User;
use Carbon\Carbon;
use Illuminate\Auth\GenericUser;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Contracts\Auth\UserProvider;
class CustomUserProvider implements UserProvider{
/**
* Retrieve a user by their unique identifier.
*
* @param mixed $identifier
* @return \Illuminate\Contracts\Auth\Authenticatable|null
*/
public function retrieveById($identifier){
$qry = User::where('id', '=', $identifier);
if ($qry->count() > 0){
$user = $qry->select('id', 'firstName', 'lastName', 'city', 'state', 'zipcode', 'email', 'dob'
, 'mobilePhone', 'password', 'question', 'answer', 'passwordSalt', 'promoCode', 'createdDate', 'isActivated', 'activationDate'
, 'isDeleted', 'firstTimeLogin', 'role', 'remember_token')->first();
return $user;
}
return NULL;
}
/**
* Retrieve a user by by their unique identifier and "remember me" token.
*
* @param mixed $identifier
* @param string $token
* @return \Illuminate\Contracts\Auth\Authenticatable|null
*/
public function retrieveByToken($identifier, $token){
$qry = User::where('id', '=', $identifier)
->where('remember_token', '=', $token);
if ($qry->count() > 0){
$user = $qry->select('id', 'firstName', 'lastName', 'city', 'state', 'zipcode', 'email', 'dob'
, 'mobilePhone', 'password', 'question', 'answer', 'passwordSalt', 'promoCode', 'createdDate', 'isActivated', 'activationDate'
, 'isDeleted', 'firstTimeLogin', 'role', 'remember_token')->first();
return $user;
}
return NULL;
}
/**
* Update the "remember me" token for the given user in storage.
*
* @param \Illuminate\Contracts\Auth\Authenticatable $user
* @param string $token
* @return void
*/
public function updateRememberToken(Authenticatable $user, $token){
$user->setRememberToken($token);
$user->save();
}
/**
* Retrieve a user by the given credentials.
*
* @param array $credentials
* @return \Illuminate\Contracts\Auth\Authenticatable|null
*/
public function retrieveByCredentials(array $credentials){
$qry = User::where('email', 'like', $credentials['email']);
if ($qry->count() > 0){
$user = $qry->select('id', 'firstName', 'lastName', 'city', 'state', 'zipcode', 'email', 'dob'
, 'mobilePhone', 'password', 'question', 'answer', 'passwordSalt', 'promoCode', 'createdDate', 'isActivated', 'activationDate'
, 'isDeleted', 'firstTimeLogin', 'role', 'remember_token')->first();
return $user;
}
return NULL;
}
/**
* Validate a user against the given credentials.
*
* @param \Illuminate\Contracts\Auth\Authenticatable $user
* @param array $credentials
* @return bool
*/
public function validateCredentials(Authenticatable $user, array $credentials){
$salt = base64_decode($user->passwordSalt);
$password = $credentials['password'];
$utf16Password = mb_convert_encoding($password, 'UTF-16LE', 'UTF-8');
$calculatedPassword = base64_encode(sha1($salt . $utf16Password, true));
if ($user->email == $credentials['email'] && $user->getAuthPassword() == $calculatedPassword){
return true;
}
return false;
}
}
?>
接下來,在app/Providers目錄下創建一個文件'CustomAuthProvider.php'。
<?php
namespace App\Providers;
use App\Auth\CustomUserProvider;
use Illuminate\Support\ServiceProvider;
class CustomAuthProvider extends ServiceProvider{
/**
* Bootstrap the application services.
*
* @return void
*/
public function boot(){
$this->app['auth']->extend('custom', function(){
return new CustomUserProvider();
});
}
/**
* Register the application services.
*
* @return void
*/
public function register(){
//
}
}
?>
就是這樣。
回答你自己的問題很好,但請保持問答格式。一個問題需要成爲一個問題,而不是參考下面的答案 –