Laravel框架用户登陆身份验证实现方法详解


Posted in PHP onSeptember 14, 2017

本文实例讲述了Laravel框架用户登陆身份验证实现方法。分享给大家供大家参考,具体如下:

laravel中检测用户是否登录,有以下的代码:

if ( !Auth::guest() )
{
  return Redirect::to('/dashboard');
}

Auth::guest是如何调用的呢?

laravel用了Facade模式,相关门面类在laravel/framework/src/Illuminate/Support/Facades文件夹定义的,看下Auth类的定义:

class Auth extends Facade {
  /**
   * Get the registered name of the component.
   *
   * @return string
   */
  protected static function getFacadeAccessor() { return 'auth'; }
}

laravel框架中,Facade模式使用反射,相关方法其实调用app['auth']中的方法,app['auth']是什么时候创建的呢,

AuthServiceProvider::register方法会注册:

$this->app->bindShared('auth', function($app)
{
  // Once the authentication service has actually been requested by the developer
  // we will set a variable in the application indicating such. This helps us
  // know that we need to set any queued cookies in the after event later.
  $app['auth.loaded'] = true;
  return new AuthManager($app);
});

那为什么最终会调到哪里呢,看下堆栈:

Illuminate\Support\Facades\Auth::guest()
Illuminate\Support\Facades\Facade::__callStatic
Illuminate\Auth\AuthManager->guest()
Illuminate\Support\Manager->__call
public function __call($method, $parameters)
{
    return call_user_func_array(array($this->driver(), $method), $parameters);
}

看下driver的代码:

public function driver($driver = null)
{
    $driver = $driver ?: $this->getDefaultDriver();
    // If the given driver has not been created before, we will create the instances
    // here and cache it so we can return it next time very quickly. If there is
    // already a driver created by this name, we'll just return that instance.
    if ( ! isset($this->drivers[$driver]))
    {
      $this->drivers[$driver] = $this->createDriver($driver);
    }
    return $this->drivers[$driver];
}

没有会调用getDefaultDrive方法

/**
* Get the default authentication driver name.
*
* @return string
*/
public function getDefaultDriver()
{
    return $this->app['config']['auth.driver'];
}

最终调用的是配置文件中配置的driver,如果配的是

'driver' => 'eloquent'

则调用的是

public function createEloquentDriver()
{
    $provider = $this->createEloquentProvider();
    return new Guard($provider, $this->app['session.store']);
}

所以Auth::guest最终调用的是Guard::guest方法

这里的逻辑先从session中取用户信息,奇怪的是session里只保存的是用户ID,然后拿这个ID来从数据库中取用户信息

public function user()
{
    if ($this->loggedOut) return;
    // If we have already retrieved the user for the current request we can just
    // return it back immediately. We do not want to pull the user data every
    // request into the method because that would tremendously slow an app.
    if ( ! is_null($this->user))
    {
      return $this->user;
    }
    $id = $this->session->get($this->getName());
    // First we will try to load the user using the identifier in the session if
    // one exists. Otherwise we will check for a "remember me" cookie in this
    // request, and if one exists, attempt to retrieve the user using that.
    $user = null;
    if ( ! is_null($id))
    {
      //provider为EloquentUserProvider
     $user = $this->provider->retrieveByID($id);
    }
    // If the user is null, but we decrypt a "recaller" cookie we can attempt to
    // pull the user data on that cookie which serves as a remember cookie on
    // the application. Once we have a user we can return it to the caller.
    $recaller = $this->getRecaller();
    if (is_null($user) && ! is_null($recaller))
    {
      $user = $this->getUserByRecaller($recaller);
    }
    return $this->user = $user;
}

希望本文所述对大家基于Laravel框架的PHP程序设计有所帮助。

PHP 相关文章推荐
php 正则匹配函数体
Aug 25 PHP
PHP高级OOP技术演示
Aug 27 PHP
PHP面向对象法则
Feb 23 PHP
php解决抢购秒杀抽奖等大流量并发入库导致的库存负数的问题
Jun 19 PHP
php文件上传简单实现方法
Jan 24 PHP
ThinkPHP模型详解
Jul 27 PHP
Yii数据库缓存实例分析
Mar 29 PHP
PHP使用自定义方法实现数组合并示例
Jul 07 PHP
php实时倒计时功能实现方法详解
Feb 27 PHP
PHP 文件锁与进程锁的使用示例
Aug 07 PHP
PHP5.5新特性之yield理解与用法实例分析
Jan 11 PHP
PHP如何通过date() 函数格式化显示时间
Nov 13 PHP
LNMP部署laravel以及xhprof安装使用教程
Sep 14 #PHP
Laravel框架实现redis集群的方法分析
Sep 14 #PHP
ThinkPHP开发--使用七牛云储存
Sep 14 #PHP
PHP使用微信开发模式实现搜索已发送图文及匹配关键字回复的方法
Sep 13 #PHP
PHP memcache在微信公众平台的应用方法示例
Sep 13 #PHP
深入解析Laravel5.5中的包自动发现Package Auto Discovery
Sep 13 #PHP
PHP 实现公历日期与农历日期的互转换
Sep 13 #PHP
You might like
php使用mkdir创建多级目录入门例子
2014/05/10 PHP
PHP+Mysql基于事务处理实现转账功能的方法
2015/07/08 PHP
php获取网站百度快照日期的方法
2015/07/29 PHP
CI框架AR操作(数组形式)实现插入多条sql数据的方法
2016/05/18 PHP
php微信公众号开发模式详解
2016/11/28 PHP
PHP解耦的三重境界(浅谈服务容器)
2017/03/13 PHP
jQuery应用之jQuery链用法实例
2015/01/19 Javascript
IE下使用jQuery重置iframe地址时内存泄露问题解决办法
2015/02/05 Javascript
Ext JS框架中日期函数的用法及日期选择控件的实现
2016/05/21 Javascript
ES6所改良的javascript“缺陷”问题
2016/08/23 Javascript
jquery控制页面的展开和隐藏实现方法(推荐)
2016/10/15 Javascript
使用UrlConnection实现后台模拟http请求的简单实例
2017/01/04 Javascript
JavaScript实现两个select下拉框选项左移右移
2017/03/09 Javascript
vue项目国际化vue-i18n的安装使用教程
2018/03/14 Javascript
React Router V4使用指南(精讲)
2018/09/17 Javascript
jQuery实现动态加载瀑布流
2020/09/01 jQuery
[01:14]TI珍贵瞬间系列(六):冠军
2020/08/30 DOTA
python模块简介之有序字典(OrderedDict)
2016/12/01 Python
Python正则表达式匹配中文用法示例
2017/01/17 Python
更改Python的pip install 默认安装依赖路径方法详解
2018/10/27 Python
Pandas GroupBy对象 索引与迭代方法
2018/11/16 Python
python实时获取外部程序输出结果的方法
2019/01/12 Python
Python3实现配置文件差异对比脚本
2019/11/18 Python
Python面向对象编程基础实例分析
2020/01/17 Python
python是怎么被发明的
2020/06/15 Python
PyTorch预训练Bert模型的示例
2020/11/17 Python
Python图像处理之膨胀与腐蚀的操作
2021/02/07 Python
CSS3 二级导航菜单的制作的示例
2018/04/02 HTML / CSS
Foot Locker加拿大官网:美国知名运动产品零售商
2019/07/21 全球购物
高中毕业生个人自我鉴定
2013/11/24 职场文书
个人委托函范文
2015/01/29 职场文书
兵马俑导游词
2015/02/02 职场文书
2015毕业生简历自我评价
2015/03/02 职场文书
在人间读书笔记
2015/06/30 职场文书
《藏戏》教学反思
2016/02/23 职场文书
为什么不建议在go项目中使用init()
2021/04/12 Golang