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 相关文章推荐
3种平台下安装php4经验点滴
Oct 09 PHP
修改了一个很不错的php验证码(支持中文)
Feb 14 PHP
php入门小知识
Mar 24 PHP
PHP中通过加号合并数组的一个简单方法分享
Jan 27 PHP
Ajax实时验证用户名/邮箱等是否已经存在的代码打包
Dec 01 PHP
php中sql注入漏洞示例 sql注入漏洞修复
Jan 24 PHP
php实现的百度搜索某地天气的小偷代码
Apr 23 PHP
Yii2增加验证码步骤详解
Apr 25 PHP
PHP写的简单数字验证码实例
May 23 PHP
php 生成加密公钥加密私钥实例详解
Jun 16 PHP
PHP实现微信对账单处理
Oct 01 PHP
laravel框架中控制器的创建和使用方法分析
Nov 23 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下判断数组中是否存在相同的值array_unique
2008/03/25 PHP
DedeCMS 核心类TypeLink.class.php摘要笔记
2010/04/07 PHP
PHP生成短网址的3种方法代码实例
2014/07/08 PHP
PHP获取表单所有复选框的值的方法
2014/08/28 PHP
php实现的中秋博饼游戏之掷骰子并输出结果功能详解
2017/11/06 PHP
使用PHPUnit进行单元测试并生成代码覆盖率报告的方法
2019/03/08 PHP
利用js跨页面保存变量做菜单的方法
2008/01/17 Javascript
jquery获取iframe中的dom对象(两种方法)
2013/07/02 Javascript
Javascript类型转换的规则实例解析
2016/02/23 Javascript
编写高质量JavaScript代码的基本要点
2016/03/02 Javascript
JS回调函数简单用法示例
2017/02/09 Javascript
Angular中响应式表单的三种更新值方法详析
2017/08/22 Javascript
javascript基于牛顿迭代法实现求浮点数的平方根【递归原理】
2017/09/28 Javascript
Element-ui table中过滤条件变更表格内容的方法
2018/03/02 Javascript
JS函数内部属性之arguments和this实例解析
2018/10/07 Javascript
vue-cli3.0实现一个多页面应用的历奇经历记录总结
2020/03/16 Javascript
Python实现把xml或xsl转换为html格式
2015/04/08 Python
Django中URL视图函数的一些高级概念介绍
2015/07/20 Python
TensorFlow数据输入的方法示例
2018/06/19 Python
django使用LDAP验证的方法示例
2018/12/10 Python
Python图像的增强处理操作示例【基于ImageEnhance类】
2019/01/03 Python
详解python读取和输出到txt
2019/03/29 Python
python 在sql语句中使用%s,%d,%f说明
2020/06/06 Python
python dict乱码如何解决
2020/06/07 Python
python在一个范围内取随机数的简单实例
2020/08/16 Python
Html5移动端弹幕动画实现示例代码
2018/08/27 HTML / CSS
兰蔻英国官网:Lancome英国
2019/04/30 全球购物
幼儿园秋游活动方案
2014/01/21 职场文书
商铺租赁意向书
2014/04/01 职场文书
技术合作协议书范本
2014/04/18 职场文书
2014财务年度工作总结
2014/11/11 职场文书
中国世界遗产导游词
2015/02/13 职场文书
Python字符串对齐方法使用(ljust()、rjust()和center())
2021/04/26 Python
Windows11性能真的上涨35%? 桌面酷睿i9实测结果公开
2021/11/21 数码科技
Smart 2 车辆代号 HC11 全新谍照曝光
2022/04/21 数码科技
详解Mysql数据库平滑扩容解决高并发和大数据量问题
2022/05/25 MySQL