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 和 XML: 使用expat函数(二)
Oct 09 PHP
PHP 编写大型网站问题集
May 07 PHP
PHPWind与Discuz截取字符函数substrs与cutstr性能比较
Dec 05 PHP
PHP静态调用非静态方法的应用分析
May 02 PHP
PHP 登录记住密码实现思路
May 07 PHP
PHP PDOStatement对象bindpram()、bindvalue()和bindcolumn之间的区别
Nov 20 PHP
PHP面试题之文件目录操作
Oct 15 PHP
PHP发送AT指令实例代码
May 26 PHP
Yii框架实现图片上传的方法详解
May 20 PHP
浅谈PHP进程管理
Mar 08 PHP
Thinkphp框架使用list_to_tree 实现无限级分类列出所有节点示例
Apr 04 PHP
php框架知识点的整理和补充
Mar 01 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 中dirname(_file_)讲解
2007/03/18 PHP
基于在生产环境中使用php性能测试工具xhprof的详解
2013/06/03 PHP
PHP邮件发送类PHPMailer用法实例详解
2014/09/22 PHP
Laravel使用消息队列需要注意的一些问题
2017/12/13 PHP
浅谈PHP SHA1withRSA加密生成签名及验签
2019/03/18 PHP
PHP从零开始打造自己的MVC框架之类的自动加载实现方法详解
2019/06/03 PHP
JS 获取span标签中的值的代码 支持ie与firefox
2009/08/24 Javascript
非常棒的10款jQuery 幻灯片插件
2011/06/14 Javascript
jquery获取及设置outerhtml的方法
2015/03/09 Javascript
avalonjs制作响应式瀑布流特效
2015/05/06 Javascript
js实现点击链接后延迟3秒再跳转的方法
2015/06/05 Javascript
JavaScript 性能优化小结
2015/10/12 Javascript
jQuery解析XML 详解及方法总结
2016/09/28 Javascript
Angular.js中用ng-repeat-start实现自定义显示
2016/10/18 Javascript
vue中用H5实现文件上传的方法实例代码
2017/05/27 Javascript
基于 Bootstrap Datetimepicker 联动
2017/08/03 Javascript
微信小程序顶部可滚动导航效果
2017/10/31 Javascript
vue动态配置模板 'component is'代码
2019/07/04 Javascript
python在指定目录下查找gif文件的方法
2015/05/04 Python
Python深度优先算法生成迷宫
2018/01/22 Python
python基于C/S模式实现聊天室功能
2019/01/09 Python
Python实现线性插值和三次样条插值的示例代码
2019/11/13 Python
python中使用you-get库批量在线下载bilibili视频的教程
2020/03/10 Python
python爬虫工具例举说明
2020/11/30 Python
Html5 APP中监听返回事件处理的方法示例
2018/03/15 HTML / CSS
Algenist奥杰尼官网:微藻抗衰老护肤品牌
2017/07/15 全球购物
2014年幼儿园元旦活动方案
2014/02/13 职场文书
网络信息安全承诺书
2014/03/26 职场文书
手术室护士长竞聘书
2014/03/31 职场文书
采购部长岗位职责
2014/06/13 职场文书
中职生自荐信范文
2014/06/15 职场文书
教师聘用意向书
2015/05/11 职场文书
信息技术研修心得体会
2016/01/08 职场文书
2016年大学生党员承诺书
2016/03/24 职场文书
2019学校请假条格式及范文
2019/06/25 职场文书
详解Java七大阻塞队列之SynchronousQueue
2021/09/04 Java/Android