前期準(zhǔn)備
Laravel的權(quán)限配置文件位于 config/auth.php屑柔,Laravel的認(rèn)證組件由“guards”和“providers”組成,
Guard 通過 session 來維護(hù)用戶登錄的狀態(tài)姻氨。Provider 是登錄以及訪問頁面的時(shí)候獲取用戶的信息。本篇主要講的是如何自定義Provider 剪验,獲取用戶信息肴焊。
config/auth.php文件
Laravel提供了兩種guard,web以及api功戚,采取默認(rèn)的web認(rèn)證娶眷。在guards的web中,用了users提供者啸臀。接下來就需要注意了届宠,我們自定義了服務(wù)提供者,就需要換到新的providers乘粒。首先豌注,定義一個(gè)使用新驅(qū)動(dòng)的provider:
'providers' => [
'users' => [
'driver' => 'Focus', //名稱自定義,這里為Focus
'model' => App\Models\User::class, //Model放在Models文件夾下
],
],
Notes: 默認(rèn)提供了兩種驅(qū)動(dòng)database和eloquent灯萍,而這兩種的提供者分別是DatabaseUserProvider和EloquentUserProvider轧铁,都處于laravel\framework\src\Illuminate\Auth文件夾下,實(shí)現(xiàn)了UserProvider旦棉,我們自定義的 Focus provider 也將實(shí)現(xiàn)UserProvider齿风。
生成路由和視圖
php artisan make:auth //命令可快速生成認(rèn)證所需要的路由和視圖
Http/Controllers 和 resources/views下會(huì)相應(yīng)生成控制器和視圖
默認(rèn)用的Email药薯,我們用username
LoginController:
//添加此方法,返回username
public function username(){
return 'username';
}
login.blade.php:
將郵箱改為域賬號(hào)救斑,email 改為username
數(shù)據(jù)庫
修改.env配置中的數(shù)據(jù)庫信息果善。
php artisan make:model Models/User // 使用命令創(chuàng)建User模型
默認(rèn)User是繼承Model的,需要修改系谐。
use Illuminate\Foundation\Auth\User as Authenticatable; //引入Authenticatable
class User extends Authenticatable
{
protected $table = 'employee';
protected $primaryKey = 'employee_id';
//can set all fields to user model
protected $guarded = [];
}
為什么要引入Authenticatable呢,是因?yàn)锳uthenticatable實(shí)現(xiàn)了Illuminate\Contracts\Auth\Authenticatable接口讨跟,而FocusUserProvider 需要用到接口的實(shí)現(xiàn)纪他。
創(chuàng)建擴(kuò)展
在app下新建 Extensions/FocusUserProvider 文件,參考DatabaseUserProvider和EloquentUserProvider晾匠,實(shí)現(xiàn)UserProvider:
namespace App\Extensions;
use App\Services\LdapValidator;
use Illuminate\Support\Str;
use Illuminate\Contracts\Auth\UserProvider;
use \Illuminate\Contracts\Auth\Authenticatable;
class LaravelUserProvider implements UserProvider {
protected $model;
public function __construct($model)
{
$this->model = $model;
}
//登錄成功后茶袒,通過此方法獲取用戶信息,返回匹配該ID的 Authenticatable 實(shí)現(xiàn)
public function retrieveById($identifier) {
//此處可以將信息放入緩存凉馆,緩解數(shù)據(jù)庫壓力薪寓。
$model = $this->createModel();
return $model->newQuery()
->where($model->getAuthIdentifierName(), $identifier)
->first();
}
public function retrieveByToken($identifier, $token) {
}
public function updateRememberToken(Authenticatable $user, $token) {
}
//該方法可以根據(jù)賬號(hào)名去查詢數(shù)據(jù)庫是否存在匹配的賬號(hào)
public function retrieveByCredentials(array $credentials) {
if (empty($credentials)) {
return;
}
// First we will add each credential element to the query as a where clause.
// Then we can execute the query and, if we found a user, return it in a
// Eloquent User "model" that will be utilized by the Guard instances.
$query = $this->createModel()->newQuery();
foreach ($credentials as $key => $value) {
if (! Str::contains($key, 'password')) {
$query->where($key, $value);
}
}
return $query->first();
}
//該方法可以驗(yàn)證密碼是否正確,因?yàn)槲覀兪莑dap登錄澜共,可以在此驗(yàn)證域賬號(hào)
public function validateCredentials(Authenticatable $user, array $credentials) {
//LdapValidator類是為了驗(yàn)證域密碼的向叉,放在了app/Services,在上面已經(jīng)引入
$Ldap = new LdapValidator($user->username, $credentials['password']);
return $Ldap->validatePassword();
}
/**
* Create a new instance of the model.
*
* @return \Illuminate\Database\Eloquent\Model
*/
public function createModel()
{
$class = '\\'.ltrim($this->model, '\\');
return new $class;
}
}
注冊(cè)提供者
Laravel 提供了AuthServiceProvider嗦董, 我們可以在這里注冊(cè)母谎。
public function boot()
{
$this->registerPolicies();
//Focus為auth.php里面定義的驅(qū)動(dòng)
Auth::provider('Focus', function($app, array $config){
return new FocusUserProvider ($config['model']);
});
}
下面就可以訪問http://你的域名/login 登錄系統(tǒng)