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 相关文章推荐
一个简洁的多级别论坛
Oct 09 PHP
PHP+MYSQL 出现乱码的解决方法
Aug 08 PHP
通俗易懂的php防注入代码
Apr 07 PHP
PHP仿博客园 个人博客(2) 数据库增添改删
Jul 05 PHP
PHP无限分类(树形类)
Sep 28 PHP
PHP实现采集中国天气网未来7天天气
Oct 15 PHP
php实现过滤UBB代码的类
Mar 12 PHP
微信公众平台之快递查询功能用法实例
Apr 14 PHP
支持中文的PHP按字符串长度分割成数组代码
May 17 PHP
php截取视频指定帧为图片
May 16 PHP
浅谈ThinkPHP5.0版本和ThinkPHP3.2版本的区别
Jun 17 PHP
php+js实现点赞功能的示例详解
Aug 07 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实现的在线人员函数库
2008/04/09 PHP
php array_search() 函数使用
2010/04/13 PHP
PHP 计算代码执行耗时的代码修正网上普遍错误
2011/05/14 PHP
php中替换字符串中的空格为逗号','的方法
2014/06/09 PHP
JS中如何设置readOnly的值
2013/12/25 Javascript
在页面上用action传递参数到后台出现乱码的解决方法
2013/12/31 Javascript
两行代码轻松搞定JavaScript日期验证
2016/08/03 Javascript
判断横屏竖屏(三种)
2017/02/13 Javascript
js date 格式化
2017/02/15 Javascript
Vue.js 2.0学习教程之从基础到组件详解
2017/04/24 Javascript
AngularJS 购物车全选/取消全选功能的实现方法
2017/08/14 Javascript
关于Promise 异步编程的实例讲解
2017/09/01 Javascript
深入浅析vue-cli@3.0 使用及配置说明
2019/05/08 Javascript
微信小程序缓存过期时间的使用详情
2019/05/12 Javascript
js中位数不足自动补位扩展padLeft、padRight实现代码
2020/04/06 Javascript
[01:00:14]2018DOTA2亚洲邀请赛 4.6 淘汰赛 VP vs TNC 第三场
2018/04/10 DOTA
python 获取文件列表(或是目录例表)
2009/03/25 Python
python中的__slots__使用示例
2015/02/26 Python
Python脚本暴力破解栅栏密码
2015/10/19 Python
轻松实现python搭建微信公众平台
2016/02/16 Python
对python中的os.getpid()和os.fork()函数详解
2019/08/08 Python
用Python制作mini翻译器的实现示例
2020/08/17 Python
PyQt5多线程防卡死和多窗口用法的实现
2020/09/15 Python
css3实现多个元素依次显示效果
2017/12/12 HTML / CSS
Html5获取高德地图定位天气的方法
2019/12/26 HTML / CSS
公益活动邀请函
2014/02/05 职场文书
亲子拓展活动方案
2014/02/20 职场文书
车队司机自我鉴定
2014/03/02 职场文书
应聘教师自荐书
2014/06/16 职场文书
活动总结报告怎么写
2014/07/03 职场文书
秋季运动会开幕词
2015/01/28 职场文书
浅谈Python中的函数(def)及参数传递操作
2021/05/25 Python
Mysql 用户权限管理实现
2021/05/25 MySQL
Python+Appium自动化测试的实战
2021/06/30 Python
python中__slots__节约内存的具体做法
2021/07/04 Python
pytest实现多进程与多线程运行超好用的插件
2022/07/15 Python