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学习笔记 PHP面向对象的程序设计
Jun 13 PHP
php数组函数序列之ksort()对数组的元素键名进行升序排序,保持索引关系
Nov 02 PHP
浅析php插件 HTMLPurifier HTML解析器
Jul 01 PHP
浅析Mysql 数据回滚错误的解决方法
Aug 05 PHP
PHP的preg_match匹配字符串长度问题解决方法
May 03 PHP
PHP PDOStatement对象bindpram()、bindvalue()和bindcolumn之间的区别
Nov 20 PHP
PHP使用内置dir类实现目录遍历删除
Mar 31 PHP
详解WordPress开发中wp_title()函数的用法
Jan 07 PHP
Zend Framework+smarty用法实例详解
Mar 19 PHP
php实现文件预览功能
May 23 PHP
php获取用户真实IP和防刷机制的实例代码
Nov 28 PHP
PHP Laravel中的Trait使用方法
Jan 20 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 结果集的分页实现代码
2009/03/10 PHP
PHP中MVC模式的模板引擎开发经验分享
2011/03/23 PHP
在PHP中使用redis
2013/11/04 PHP
phpcms的分类名称和类别名称的调用
2017/01/05 PHP
快速排序 php与javascript的不同之处
2011/02/22 Javascript
jquery构造器的实现代码小结
2011/05/16 Javascript
最新28个很棒的jQuery 教程
2011/05/28 Javascript
用JavaScript实现动画效果的方法
2013/07/20 Javascript
js控制input框只读实现示例
2014/01/20 Javascript
JavaScript中的无阻塞加载性能优化方案
2014/10/10 Javascript
jQuery实现响应浏览器缩放大小并改变背景颜色
2014/10/31 Javascript
jQuery中val()方法用法实例
2014/12/25 Javascript
深入理解JavaScript系列(27):设计模式之建造者模式详解
2015/03/03 Javascript
Bootstrap每天必学之表格
2015/11/23 Javascript
JavaScript模拟push
2016/03/06 Javascript
Three.js学习之Lamber材质和Phong材质
2016/08/04 Javascript
JS获取当前地理位置的方法
2017/10/25 Javascript
微信小程序实现带缩略图轮播效果
2018/11/04 Javascript
vue增加强缓存和版本号的实现方法
2019/05/01 Javascript
用node.js写一个jenkins发版脚本
2019/05/21 Javascript
JS 数组基本用法入门示例解析
2020/01/16 Javascript
JS实现audio音频剪裁剪切复制播放与上传(步骤详解)
2020/07/28 Javascript
开源Web应用框架Django图文教程
2017/03/09 Python
安装python时MySQLdb报错的问题描述及解决方法
2018/03/20 Python
利用python和百度地图API实现数据地图标注的方法
2019/05/13 Python
Python中的asyncio代码详解
2019/06/10 Python
python 寻找离散序列极值点的方法
2019/07/10 Python
使用Pandas对数据进行筛选和排序的实现
2019/07/29 Python
Python 爬虫实现增加播客访问量的方法实现
2019/10/31 Python
Python matplotlib修改默认字体的操作
2020/03/05 Python
python+requests实现接口测试的完整步骤
2020/10/27 Python
html5移动端自适应布局的实现
2020/04/15 HTML / CSS
幼儿园五一活动方案
2014/02/07 职场文书
挂靠协议书范本
2014/04/22 职场文书
3的组成教学反思
2014/04/30 职场文书
陶瓷类经典广告语集锦
2019/10/25 职场文书