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 27 PHP
php之Smarty模板使用方法示例详解
Jul 08 PHP
简单谈谈favicon
Jun 10 PHP
Ajax实现对静态页面的文章访问统计功能示例
Oct 10 PHP
php获取今日开始时间和结束时间的方法
Feb 27 PHP
php 二维数组快速排序算法的实现代码
Oct 17 PHP
PHP中PDO事务处理操作示例
May 02 PHP
作为PHP程序员你要知道的另外一种日志
Jul 30 PHP
实现PHP中session存储及删除变量
Oct 15 PHP
thinkphp5.1框架模板布局与模板继承用法分析
Jul 19 PHP
解决PHP curl或file_get_contents下载图片损坏或无法打开的问题
Oct 11 PHP
PHP rsa加密解密算法原理解析
Dec 09 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的责任链编程模式
2015/08/11 PHP
PHP实现生成带背景的图形验证码功能
2016/10/03 PHP
基于thinkPHP类的插入数据库操作功能示例
2017/01/06 PHP
PHP基于rabbitmq操作类的生产者和消费者功能示例
2018/06/16 PHP
json跟xml的对比分析
2008/06/10 Javascript
基于JavaScript实现生成名片、链接等二维码
2015/09/20 Javascript
js实现的鼠标滚轮滚动切换页面效果(类似360默认页面滚动切换效果)
2016/01/27 Javascript
利用Javascript获取选择文本所在的句子详解
2017/12/03 Javascript
Vue使用mixins实现压缩图片代码
2018/03/14 Javascript
nuxt.js 缓存实践
2018/06/25 Javascript
vue.js提交按钮时进行简单的if判断表达式详解
2018/08/08 Javascript
layui 对弹窗 form表单赋值的实现方法
2019/09/04 Javascript
Javascript异步编程async实现过程详解
2020/04/02 Javascript
vue动态加载SVG文件并修改节点数据的操作代码
2020/08/17 Javascript
详解JavaScript数据类型和判断方法
2020/09/04 Javascript
python中使用urllib2获取http请求状态码的代码例子
2014/07/07 Python
python中字典(Dictionary)用法实例详解
2015/05/30 Python
在Django的模型和公用函数中使用惰性翻译对象
2015/07/27 Python
解决python3在anaconda下安装caffe失败的问题
2017/06/15 Python
Python操作json的方法实例分析
2018/12/06 Python
python实现比对美团接口返回数据和本地mongo数据是否一致示例
2019/08/09 Python
python 变量初始化空列表的例子
2019/11/28 Python
python numpy矩阵信息说明,shape,size,dtype
2020/05/22 Python
Django限制API访问频率常用方法解析
2020/10/12 Python
荷兰优雅女装网上商店:Heine
2016/11/14 全球购物
应用化学专业本科生求职信
2013/09/29 职场文书
大学自我评价
2014/02/12 职场文书
高级编程求职信模板
2014/02/16 职场文书
投标保密承诺书
2014/05/19 职场文书
企业理念标语
2014/06/09 职场文书
教师四风自我剖析材料
2014/09/30 职场文书
一年级语文上册复习计划
2015/01/17 职场文书
辣妈辣妹观后感
2015/06/10 职场文书
Oracle11g r2 卸载干净重装的详细教程(亲测有效已重装过)
2021/06/04 Oracle
postgresql使用filter进行多维度聚合的解决方法
2021/07/16 PostgreSQL
Golang实现可重入锁的示例代码
2022/05/25 Golang