We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Laravel 提供了完善的用户状态判断鉴定、验证机制,这篇文章我们用十五分钟左右的时间来深入框架内部来了解它。 在学习之前,建议先查阅 Laravel China 文档库 > Laravel 5.7 > 用户认证,熟悉用法后,学习效率更高。
Illuminate\Auth\SessionGuard
guard
web
Illuminate\Auth\EloquentUserProvider
App\Http\Middleware\Authenticate
App\User
User
Illuminate\Auth\Authenticatable
Illuminate\Foundation\Auth\User
Auth::guard 可用的守护者定义在:
laravel/config/auth.php
Lines 38 to 48 in 4d76a68
这些配置文件中的 providers 定义在
providers
Lines 67 to 77 in 4d76a68
而
Line 40 in 4d76a68
Line 45 in 4d76a68
session 和 token 分别指的是 Illuminate\Auth\SessionGuard 和 Illuminate\Auth\TokenGuard。
session
token
Illuminate\Auth\TokenGuard
具体的构建方法见 AuthManager::createSessionDriver 和 AuthManager::createTokenDriver,这两个方法是在 AuthManager::resolve 时用以下代码调用的。 laravel/vendor/laravel/framework/src/Illuminate/Auth/AuthManager.php Line 91 in 4d76a68 $driverMethod = 'create'.ucfirst($config['driver']).'Driver';
具体的构建方法见 AuthManager::createSessionDriver 和 AuthManager::createTokenDriver,这两个方法是在 AuthManager::resolve 时用以下代码调用的。
AuthManager::createSessionDriver
AuthManager::createTokenDriver
AuthManager::resolve
laravel/vendor/laravel/framework/src/Illuminate/Auth/AuthManager.php
Line 91 in 4d76a68
Illuminate\Auth\AuthManager 类
Illuminate\Auth\AuthManager
Lines 8 to 12 in 4d76a68
Illuminate\Auth\CreatesUserProviders
在 Illuminate\Foundation\Application 的 registerCoreContainerAliases 方法中,注册 auth 时, Illuminate\Auth\AuthManager 被使用。
Illuminate\Foundation\Application
registerCoreContainerAliases
auth
laravel/vendor/laravel/framework/src/Illuminate/Foundation/Application.php
Lines 1074 to 1117 in 4d76a68
__construct
在 Illuminate\Auth\AuthServiceProvider 中的 register 方法,
Illuminate\Auth\AuthServiceProvider
register
laravel/vendor/laravel/framework/src/Illuminate/Auth/AuthServiceProvider.php
Lines 17 to 26 in 4d76a68
registerAuthenticator 方法为
registerAuthenticator
Lines 33 to 47 in 4d76a68
registerUserResolver 方法为
registerUserResolver
Lines 54 to 61 in 4d76a68
这里便将 auth 和 Illuminate\Contracts\Auth\Authenticatable 成功的从 Application 中的 alias 升级为 singleton 。
Illuminate\Contracts\Auth\Authenticatable
Application
在我们的 config/app.php 的 providers 第一个便是 Illuminate\Auth\AuthServiceProvider
config/app.php
laravel/config/app.php
Lines 122 to 127 in 4d76a68
到这里,web 环境框架启动部分所有初始化用户状态维护机制就完成。
当一个路由声明必须登陆后可访问,也就是 auth 中间件,会触发:
laravel/vendor/laravel/framework/src/Illuminate/Auth/Middleware/Authenticate.php
Lines 24 to 27 in 4d76a68
AuthManager
在容器构建 AuthManager 时,会触发 __construct
Lines 49 to 56 in 4d76a68
中间件的 handle
handle
Lines 39 to 44 in 4d76a68
调用了中间件 authenticate 方法
authenticate
Lines 55 to 70 in 4d76a68
如果没登陆,报错
Lines 67 to 69 in 4d76a68
如果手工在业务中使用 auth()->user() 会触发
auth()->user()
Lines 290 to 293 in 4d76a68
AuthManager 的 guard() 默认返回的是 SessionGuard,所以 auth()->user() 调用的是
guard()
SessionGuard
laravel/vendor/laravel/framework/src/Illuminate/Auth/SessionGuard.php
Lines 113 to 151 in 4d76a68
其 131 行的 $this->provider->retrieveById($id) 调用了 EloquentUserProvider::retrieveById
$this->provider->retrieveById($id)
EloquentUserProvider::retrieveById
laravel/vendor/laravel/framework/src/Illuminate/Auth/EloquentUserProvider.php
Lines 46 to 53 in 4d76a68
$this->createModel() 返回的是 $this->model,是在
$this->createModel()
$this->model
Lines 34 to 38 in 4d76a68
在 AuthManager 调用 createUserProvider 后调用 createEloquentProvider 方法时,
createUserProvider
createEloquentProvider
laravel/vendor/laravel/framework/src/Illuminate/Auth/CreatesUserProviders.php
Lines 24 to 45 in 4d76a68
Lines 80 to 83 in 4d76a68
其传进去的 $config 的获取逻辑为
$config
Lines 54 to 59 in 4d76a68
也就是这里设定的 'model' => App\User::class 了
'model' => App\User::class
前面 getAuthIdentifierName 的方法,就是调用 App\User::getAuthIdentifierName 来查找用户了。
getAuthIdentifierName
App\User::getAuthIdentifierName
Lines 50 to 52 in 4d76a68
本文章采用「署名 4.0 国际」创作共享协议, 转载前请阅读 相关说明 »
The text was updated successfully, but these errors were encountered:
No branches or pull requests
相关类
Illuminate\Auth\SessionGuard
guard
定义为web
生效Illuminate\Auth\EloquentUserProvider
App\Http\Middleware\Authenticate
App\User
User
模型Illuminate\Auth\Authenticatable
App\User
的父类Illuminate\Foundation\Auth\User
所使用配置文件解析
Auth::guard 可用的守护者定义在:
laravel/config/auth.php
Lines 38 to 48 in 4d76a68
这些配置文件中的
providers
定义在laravel/config/auth.php
Lines 67 to 77 in 4d76a68
而
laravel/config/auth.php
Line 40 in 4d76a68
laravel/config/auth.php
Line 45 in 4d76a68
session
和token
分别指的是Illuminate\Auth\SessionGuard
和Illuminate\Auth\TokenGuard
。注册流程 基础 + 容器部分
Illuminate\Auth\AuthManager
类laravel/vendor/laravel/framework/src/Illuminate/Auth/AuthManager.php
Lines 8 to 12 in 4d76a68
用到了
Illuminate\Auth\CreatesUserProviders
trait此 trait 提供如下方法
在
Illuminate\Foundation\Application
的registerCoreContainerAliases
方法中,注册auth
时,Illuminate\Auth\AuthManager
被使用。laravel/vendor/laravel/framework/src/Illuminate/Foundation/Application.php
Lines 1074 to 1117 in 4d76a68
容器对象的
registerCoreContainerAliases
方法是在__construct
阶段被触发的。注册流程 服务提供者 Illuminate\Auth\AuthServiceProvider
在
Illuminate\Auth\AuthServiceProvider
中的register
方法,laravel/vendor/laravel/framework/src/Illuminate/Auth/AuthServiceProvider.php
Lines 17 to 26 in 4d76a68
registerAuthenticator
方法为laravel/vendor/laravel/framework/src/Illuminate/Auth/AuthServiceProvider.php
Lines 33 to 47 in 4d76a68
registerUserResolver
方法为laravel/vendor/laravel/framework/src/Illuminate/Auth/AuthServiceProvider.php
Lines 54 to 61 in 4d76a68
这里便将
auth
和Illuminate\Contracts\Auth\Authenticatable
成功的从Application
中的 alias 升级为 singleton 。在我们的
config/app.php
的providers
第一个便是Illuminate\Auth\AuthServiceProvider
laravel/config/app.php
Lines 122 to 127 in 4d76a68
到这里,web 环境框架启动部分所有初始化用户状态维护机制就完成。
中间件流程 限制登陆后可访问
当一个路由声明必须登陆后可访问,也就是
auth
中间件,会触发:laravel/vendor/laravel/framework/src/Illuminate/Auth/Middleware/Authenticate.php
Lines 24 to 27 in 4d76a68
触发容器构建
AuthManager
在容器构建
AuthManager
时,会触发__construct
laravel/vendor/laravel/framework/src/Illuminate/Auth/AuthManager.php
Lines 49 to 56 in 4d76a68
中间件的
handle
laravel/vendor/laravel/framework/src/Illuminate/Auth/Middleware/Authenticate.php
Lines 39 to 44 in 4d76a68
调用了中间件
authenticate
方法laravel/vendor/laravel/framework/src/Illuminate/Auth/Middleware/Authenticate.php
Lines 55 to 70 in 4d76a68
如果没登陆,报错
laravel/vendor/laravel/framework/src/Illuminate/Auth/Middleware/Authenticate.php
Lines 67 to 69 in 4d76a68
获取当前用户流程
如果手工在业务中使用
auth()->user()
会触发laravel/vendor/laravel/framework/src/Illuminate/Auth/AuthManager.php
Lines 290 to 293 in 4d76a68
AuthManager
的guard()
默认返回的是SessionGuard
,所以auth()->user()
调用的是laravel/vendor/laravel/framework/src/Illuminate/Auth/SessionGuard.php
Lines 113 to 151 in 4d76a68
其 131 行的
$this->provider->retrieveById($id)
调用了EloquentUserProvider::retrieveById
laravel/vendor/laravel/framework/src/Illuminate/Auth/EloquentUserProvider.php
Lines 46 to 53 in 4d76a68
$this->createModel()
返回的是$this->model
,是在laravel/vendor/laravel/framework/src/Illuminate/Auth/EloquentUserProvider.php
Lines 34 to 38 in 4d76a68
在
AuthManager
调用createUserProvider
后调用createEloquentProvider
方法时,laravel/vendor/laravel/framework/src/Illuminate/Auth/CreatesUserProviders.php
Lines 24 to 45 in 4d76a68
laravel/vendor/laravel/framework/src/Illuminate/Auth/CreatesUserProviders.php
Lines 80 to 83 in 4d76a68
其传进去的
$config
的获取逻辑为laravel/vendor/laravel/framework/src/Illuminate/Auth/CreatesUserProviders.php
Lines 54 to 59 in 4d76a68
也就是这里设定的
'model' => App\User::class
了laravel/config/auth.php
Lines 67 to 77 in 4d76a68
前面
getAuthIdentifierName
的方法,就是调用App\User::getAuthIdentifierName
来查找用户了。laravel/vendor/laravel/framework/src/Illuminate/Auth/EloquentUserProvider.php
Lines 50 to 52 in 4d76a68
The text was updated successfully, but these errors were encountered: