Laravel Authentication 認(rèn)證系統(tǒng)

Authentication 認(rèn)證系統(tǒng)使用

Authentication
學(xué)習(xí)筆記《Laravel Auth 代碼閱讀》

花了兩天研究 Laravel,昨天是通宵達(dá)旦搞到將近凌晨5點(diǎn),基本掌握系統(tǒng)架構(gòu),可以說是博大精深!今天繼續(xù),主要心思放到整個(gè) Laravel Auth 子系統(tǒng)中來了。

基本流程解讀

HTTP本身是無狀態(tài),通常在系統(tǒng)交互的過程中,使用賬號(hào)或者Token標(biāo)識(shí)來確定認(rèn)證用戶,Token 一般會(huì)以 Cookie 方式保存到客戶端,客戶端發(fā)送請(qǐng)求時(shí)一并將 Token 發(fā)回服務(wù)器。如果客服端沒有 Cookie,通常做法時(shí)時(shí)在 URL 中攜帶 Token。也可以 HTTP 頭信息方式攜帶 Token 到服務(wù)器。Laravel 提供 Session 和 URL 中攜帶 token 兩種方式做認(rèn)證。對(duì)應(yīng)配置文件中的 guards 配置的 web、api。

web 認(rèn)證基于 Session 根據(jù) SessionId 獲取用戶,provider 查詢關(guān)聯(lián)用戶;api認(rèn)證是基于token值交互,也采用users這個(gè)provider;

'providers' => [
    'users' => [
        'driver' => 'eloquent',
        'model' => App\User::class,
    ],

    // 'users' => [
    //     'driver' => 'database',
    //     'table' => 'users',
    // ],
],

配置文件 /config/auth.php 中的 provider 是提供用戶數(shù)據(jù)的接口,要標(biāo)注驅(qū)動(dòng)對(duì)象和目標(biāo)對(duì)象,默認(rèn)值 users是一套 provider 的名字,采用 eloquent 驅(qū)動(dòng),對(duì)應(yīng)模類是 App\User。也可以使用 database 的方式。

Laravel 內(nèi)置了認(rèn)證模塊,也可以手動(dòng)安裝,命令會(huì)生成相關(guān)的視圖文件:

artisan make:auth

app
+-- Http
|   +-- Middleware
|   |   +-- RedirectIfAuthenticated.php
|   +-- Controllers
|       +-- Auth
|           +-- ForgotPasswordController.php
|           +-- LoginController.php
|           +-- RegisterController.php
|           +-- ResetPasswordController.php
+-- Providers
|   +-- AuthServiceProvider.php
+-- User.php

整個(gè)認(rèn)證個(gè)模塊包括相應(yīng)的視圖文件,一個(gè) RedirectIfAuthenticated 中間件和四個(gè) Controller 文件,一個(gè) User 模型文件,它是 Authenticatable 接口類。另外還有一個(gè) AuthServiceProvider 它是服務(wù)容器,為注入認(rèn)證用戶的數(shù)據(jù)提供者 UserProvider 接口準(zhǔn)備的。密碼數(shù)據(jù)是從 Authenticatable 流向 UserProvider 的,前者讀取數(shù)據(jù),后者負(fù)責(zé)校驗(yàn)。列如可以嘗試這樣覆蓋 getAuthPassword() 方法來測(cè)試數(shù)據(jù)邏輯,而不是從數(shù)據(jù)庫(kù)中讀入數(shù)據(jù):

public function getAuthPassword(){
    return bcrypt("userpass");
}

此方法原本是在 Illuminate\Auth\Authenticatable 定義的,它是 trait Authenticatable 類,是 PHP 多繼承的規(guī)范。這個(gè)方法就是簡(jiǎn)單地返回 $this->password,即模型關(guān)聯(lián)的數(shù)據(jù)表字段 password 的值,數(shù)據(jù)是通過依賴注入的。

app/Http/Kernel.php 注冊(cè)中間件:

protected $routeMiddleware = [
    // ...
    'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
];

Auth模塊從功能上分為用戶認(rèn)證和權(quán)限管理兩個(gè)部分;

Illuminate\Auth是負(fù)責(zé)用戶認(rèn)證和權(quán)限管理的模塊;
Illuminate\Foundation\Auth 提供了登錄、修改密碼、重置密碼等一系統(tǒng)列具體邏輯實(shí)現(xiàn);
Illuminate\Foundation\Auth\AuthenticatesUsers 負(fù)責(zé)登錄視圖邏輯
Illuminate\Auth\Passwords 目錄下是密碼重置或忘記密碼處理的小模塊;
config\auth.php認(rèn)證相關(guān)配置文件

RedirectIfAuthenticated 只有 handle() 處理邏輯,但 Auth 類中并沒有定義 Auth::guard(),這是經(jīng)過 Facades 編程模式,通過 __callStatic() 關(guān)聯(lián)到了 AuthManager,代碼注解中有提示。Facade 是一套配合 Service Container 的靜態(tài)方法解決方案,是一套設(shè)計(jì)得非常優(yōu)雅的機(jī)制,是 Laravel 的核心機(jī)制之一。

public function handle($request, Closure $next, $guard = null)
{
    if (Auth::guard($guard)->check()) {
        return redirect('/home');
    }

    return $next($request);
}

通過獲取配置文件的 guards 設(shè)置,不同的依賴就會(huì)加載進(jìn)來:

HTTP Basic: /src/Illuminate/Auth/RequestGuard.php
Session: /src/Illuminate/Auth/SessionGuard.php
Token: /src/Illuminate/Auth/TokenGuard.php

這些按標(biāo)準(zhǔn)接口定義的類都有 user() 方法,這個(gè)方法就是從數(shù)據(jù)庫(kù)獲取匹配的授權(quán)用戶,用戶數(shù)據(jù)是通過 UserProvider 接口提供的,即 /app/Providers/AuthServiceProvider.php。通過實(shí)現(xiàn)此接口可以定制自己的認(rèn)證邏輯,UserProvider::validateCredentials() 就是檢驗(yàn)密碼的方法。自帶的 /app/User.php 模型就是實(shí)現(xiàn)了 Authenticatable 接口的類。

public function user()
{
    if ($this->loggedOut) {
        return;
    }

    if (! is_null($this->user)) {
        return $this->user;
    }

    $id = $this->session->get($this->getName());

    if (! is_null($id) && $this->user = $this->provider->retrieveById($id)) {
        $this->fireAuthenticatedEvent($this->user);
    }

    if (is_null($this->user) && ! is_null($recaller = $this->recaller())) {
        $this->user = $this->userFromRecaller($recaller);

        if ($this->user) {
            $this->updateSession($this->user->getAuthIdentifier());

            $this->fireLoginEvent($this->user, true);
        }
    }

    return $this->user;
}

/Illuminate/Routing/Router.php 中已經(jīng)定義好和認(rèn)證相關(guān)的一組路由,只需在 /routes/web.php 中添加一句 Auth::routes(); 就可以注冊(cè)這些路由,這些路由連接的控制器就前面安裝認(rèn)證模塊生成的。

public function auth(array $options = [])
{
    // Authentication Routes...
    $this->get('login', 'Auth\LoginController@showLoginForm')->name('login');
    $this->post('login', 'Auth\LoginController@login');
    $this->post('logout', 'Auth\LoginController@logout')->name('logout');

    // Registration Routes...
    if ($options['register'] ?? true) {
        $this->get('register', 'Auth\RegisterController@showRegistrationForm')->name('register');
        $this->post('register', 'Auth\RegisterController@register');
    }

    // Password Reset Routes...
    if ($options['reset'] ?? true) $this->resetPassword();

    // Email Verification Routes...
    if ($options['verify'] ?? false) $this->emailVerification();
}

public function resetPassword()
{
    $this->get('password/reset', 'Auth\ForgotPasswordController@showLinkRequestForm')->name('password.request');
    $this->post('password/email', 'Auth\ForgotPasswordController@sendResetLinkEmail')->name('password.email');
    $this->get('password/reset/{token}', 'Auth\ResetPasswordController@showResetForm')->name('password.reset');
    $this->post('password/reset', 'Auth\ResetPasswordController@reset')->name('password.update');
}

public function emailVerification()
{
    $this->get('email/verify', 'Auth\VerificationController@show')->name('verification.notice');
    $this->get('email/verify/{id}', 'Auth\VerificationController@verify')->name('verification.verify');
    $this->get('email/resend', 'Auth\VerificationController@resend')->name('verification.resend');
}

來看第一條路由,是一條命名路由 name(),執(zhí)行控制器的方法是 LoginController->showLoginForm() ,這個(gè)方法會(huì)調(diào)出視圖 view('auth.login'),需要自己建立視圖文件 \resources\views\auth\login.blade.php,可以使用命令 php artisan make:auth 來生成。登錄表單大概是這樣,@csrf 這是默認(rèn)需要添加的參數(shù),除非在 VerifyCsrfToken.php中間關(guān)閉了校驗(yàn)。

<style>
    .frame { width:50%; margin:auto; padding:32px; background: #484848; border-radius: 4px; color:white; }
    .filed { padding:16px; border:1px solid #4E6DB0; border-radius: 16px; background: #282828; margin:16px;}
    .center { text-align: center; }
</style>

<div class="frame">
    <form method="post">
        {{ csrf_field() }}
        <!-- @csrf -->
        <div class="filed">{{ __('E-Mail Address') }}: <input type="text" name="email"></div>
        <div class="filed">{{ __('Password') }}: <input type="password" name="password"></div>
        <div class="filed center">
        <button type="submit">{{ __('Submit') }}</button>
            <input type="checkbox" name="remember" id="remember" {{ old('remember') ? 'checked' : '' }}>
            <label class="form-check-label" for="remember">{{ __('Remember Me') }}</label>
        </div>
    </form>
</div>

所有post請(qǐng)求中必須包含一個(gè) @crsf 的字段用以防止跨域攻擊,只有通過驗(yàn)證才認(rèn)為是安全的提交動(dòng)作,否則會(huì)得到 419 Page Expired。打開 app\Http\Middleware\VerifyCsrfToken.php 添加排除規(guī)則即可關(guān)閉:

protected $except = [
    'login',
];

只是,LoginController 類并沒有定義這個(gè)方法,這個(gè)方法定義在 AuthenticatesUsers 中定義的,通過 use 關(guān)鍵字引入 trait Authenticatable,這相當(dāng)于 PHP 的多繼承用法。

use AuthenticatesUsers;

emailpassword 兩個(gè)變量用于匹配用戶,密碼生成使用的是 bcrypt() 方法,自帶的 DatabaseSeeder.php 中含有初始化數(shù)據(jù),按需要去執(zhí)行命令填充到數(shù)據(jù)庫(kù) artisan db:seed

還有一些其他的認(rèn)證方法:

Auth::check() 判斷當(dāng)前用戶是否已認(rèn)證(是否已登錄)
Auth::user() 獲取當(dāng)前的認(rèn)證用戶
Auth::id() 獲取當(dāng)前的認(rèn)證用戶的 ID(未登錄情況下會(huì)報(bào)錯(cuò))
Auth::attempt(['email' => $email, 'password' => $password], $remember)嘗試對(duì)用戶進(jìn)行認(rèn)證
Auth::attempt($credentials, true) 通過傳入 true 值來開啟 '記住我' 功能
Auth::once($credentials) 只針對(duì)一次的請(qǐng)求來認(rèn)證用戶
Auth::login($user)
Auth::login($user, true) // Login and "remember" the given user...
Auth::login(User::find(1), $remember) 登錄一個(gè)指定用戶到應(yīng)用上
Auth::guard('admin')->login($user)
Auth::loginUsingId(1) 登錄指定用戶 ID 的用戶到應(yīng)用上
Auth::loginUsingId(1, true) // Login and "remember" the given user...
Auth::logout() 使用戶退出登錄(清除會(huì)話)
Auth::validate($credentials) 驗(yàn)證用戶憑證
Auth::viaRemember() 是否通過記住我登錄
Auth::basic('username') 使用 HTTP Basic Auth 的基本認(rèn)證方式來認(rèn)證
Auth::onceBasic() 執(zhí)行「HTTP Basic」登錄嘗試
Password::remind($credentials, function($message, $user){}) 發(fā)送密碼重置提示給用戶

Adding Custom Guards

namespace App\Providers;

use App\Services\Auth\JwtGuard;
use Illuminate\Support\Facades\Auth;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;

class AuthServiceProvider extends ServiceProvider
{
    /**
     * Register any application authentication / authorization services.
     *
     * @return void
     */
    public function boot()
    {
        $this->registerPolicies();

        Auth::extend('jwt', function ($app, $name, array $config) {
            // Return an instance of Illuminate\Contracts\Auth\Guard...

            return new JwtGuard(Auth::createUserProvider($config['provider']));
        });
    }
}

/config/auth.php 中配置自定義的 Guards:

'guards' => [
    'api' => [
        'driver' => 'jwt',
        'provider' => 'users',
    ],
],

Closure Request Guards

use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

/**
 * Register any application authentication / authorization services.
 *
 * @return void
 */
public function boot()
{
    $this->registerPolicies();

    Auth::viaRequest('custom-token', function ($request) {
        return User::where('token', $request->token)->first();
    });
}

/config/auth.php 中配置自定義的 Guards:

'guards' => [
    'api' => [
        'driver' => 'custom-token',
    ],
],

Adding Custom User Providers

如果不使用傳統(tǒng)的關(guān)系數(shù)據(jù)庫(kù),可以自定義 Provider 可以實(shí)現(xiàn)自己的認(rèn)證邏輯,如實(shí)現(xiàn)一個(gè) riak Provider:

namespace App\Providers;

use Illuminate\Support\Facades\Auth;
use App\Extensions\RiakUserProvider;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;

class AuthServiceProvider extends ServiceProvider
{
    /**
     * Register any application authentication / authorization services.
     *
     * @return void
     */
    public function boot()
    {
        $this->registerPolicies();

        Auth::provider('riak', function ($app, array $config) {
            // Return an instance of Illuminate\Contracts\Auth\UserProvider...

            return new RiakUserProvider($app->make('riak.connection'));
        });
    }
}

/config/auth.php 中配置自定義的 riak:

'providers' => [
    'users' => [
        'driver' => 'riak',
    ],
],

'guards' => [
    'web' => [
        'driver' => 'session',
        'provider' => 'users',
    ],
],

The User Provider Contract

Illuminate\Contracts\Auth\UserProvider contract 接口定義:

namespace Illuminate\Contracts\Auth;

interface UserProvider {

    public function retrieveById($identifier);
    public function retrieveByToken($identifier, $token);
    public function retrieveByCredentials(array $credentials);
    public function updateRememberToken(Authenticatable $user, $token);
    public function validateCredentials(Authenticatable $user, array $credentials);

}

retrieveById(), retrieveByToken(), and retrieveByCredentials() 方法返回的對(duì)象需要實(shí)現(xiàn) Authenticatable 接口。

The Authenticatable Contract

namespace Illuminate\Contracts\Auth;

interface Authenticatable {

    public function getAuthIdentifierName();
    public function getAuthIdentifier();
    public function getAuthPassword();
    public function getRememberToken();
    public function setRememberToken($value);
    public function getRememberTokenName();

}

Events 認(rèn)證事件

認(rèn)證過程中(包括注冊(cè)、忘記密碼),定義了相關(guān)事件,實(shí)現(xiàn)自己的 EventServiceProvider 進(jìn)行監(jiān)聽:

protected $listen = [
    'Illuminate\Auth\Events\Registered' => ['App\Listeners\LogRegisteredUser', ],
    'Illuminate\Auth\Events\Attempting' => ['App\Listeners\LogAuthenticationAttempt', ],
    'Illuminate\Auth\Events\Authenticated' => ['App\Listeners\LogAuthenticated', ],
    'Illuminate\Auth\Events\Login' => ['App\Listeners\LogSuccessfulLogin', ],
    'Illuminate\Auth\Events\Failed' => ['App\Listeners\LogFailedLogin', ],
    'Illuminate\Auth\Events\Logout' => ['App\Listeners\LogSuccessfulLogout', ],
    'Illuminate\Auth\Events\Lockout' => ['App\Listeners\LogLockout', ],
    'Illuminate\Auth\Events\PasswordReset' => ['App\Listeners\LogPasswordReset', ], 
];
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 228,333評(píng)論 6 531
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 98,491評(píng)論 3 416
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 176,263評(píng)論 0 374
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我,道長(zhǎng),這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 62,946評(píng)論 1 309
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 71,708評(píng)論 6 410
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 55,186評(píng)論 1 324
  • 那天,我揣著相機(jī)與錄音,去河邊找鬼。 笑死,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,255評(píng)論 3 441
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 42,409評(píng)論 0 288
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 48,939評(píng)論 1 335
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 40,774評(píng)論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 42,976評(píng)論 1 369
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,518評(píng)論 5 359
  • 正文 年R本政府宣布,位于F島的核電站,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 44,209評(píng)論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,641評(píng)論 0 26
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,872評(píng)論 1 286
  • 我被黑心中介騙來泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 51,650評(píng)論 3 391
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 47,958評(píng)論 2 373

推薦閱讀更多精彩內(nèi)容