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面向对象全攻略 (二) 实例化对象 使用对象成员
Sep 30 PHP
PHP管理内存函数 memory_get_usage()使用介绍
Sep 23 PHP
PHP实现多进程并行操作的详解(可做守护进程)
Jun 18 PHP
PHP如何利用P3P实现跨域
Aug 24 PHP
zf框架的数据库追踪器使用示例
Mar 13 PHP
ThinkPHP中U方法的使用浅析
Jun 13 PHP
Codeigniter通过SimpleXML将xml转换成对象的方法
Mar 19 PHP
php脚本运行时的超时机制详解
Feb 17 PHP
PHP入门教程之字符串处理技巧总结(转换,过滤,解析,查找,截取,替换等)
Sep 11 PHP
php函数mkdir实现递归创建层级目录
Oct 27 PHP
PHP调用接口用post方法传送json数据的实例
May 31 PHP
php+ajax实现文件切割上传功能示例
Mar 03 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
免费手机号码归属地API查询接口和PHP使用实例分享
2014/04/10 PHP
thinkPHP使用pclzip打包备份mysql数据库的方法
2016/04/30 PHP
php实现在新浪云中使用imagick生成缩略图并上传的方法
2016/09/26 PHP
Laravel框架实现的上传图片到七牛功能详解
2019/09/06 PHP
YII2框架使用控制台命令的方法分析
2020/03/18 PHP
js下通过getList函数实现分页效果的代码
2010/09/17 Javascript
基于jquery tab切换(防止页面刷新)
2012/05/23 Javascript
基于jQuery.Hz2Py.js插件实现的汉字转拼音特效
2015/05/07 Javascript
jQuery EasyUI之DataGrid使用实例详解
2016/01/04 Javascript
jQuery实现手机自定义弹出输入框
2016/06/13 Javascript
PHP获取当前页面完整URL的方法
2016/12/02 Javascript
微信小程序 页面跳转和数据传递实例详解
2017/01/19 Javascript
基于vue实现分页/翻页组件paginator示例
2017/03/09 Javascript
ES6教程之for循环和Map,Set用法分析
2017/04/10 Javascript
详解vue.js的devtools安装
2017/05/26 Javascript
Javascript ES6中对象类型Sets的介绍与使用详解
2017/07/17 Javascript
JS实现table表格固定表头且表头随横向滚动而滚动
2017/10/26 Javascript
利用adb shell和node.js实现抖音自动抢红包功能(推荐)
2018/02/22 Javascript
Vue常用的几个指令附完整案例
2018/11/06 Javascript
wxPython 入门教程
2008/10/07 Python
Python中的面向对象编程详解(上)
2015/04/13 Python
python中函数传参详解
2016/07/03 Python
Python及PyCharm下载与安装教程
2017/11/18 Python
Python与人工神经网络:使用神经网络识别手写图像介绍
2017/12/19 Python
python实现TF-IDF算法解析
2018/01/02 Python
python在TXT文件中按照某一字符串取出该字符串所在的行方法
2018/12/10 Python
Python HTML解析模块HTMLParser用法分析【爬虫工具】
2019/04/05 Python
带你彻底搞懂python操作mysql数据库(cursor游标讲解)
2020/01/06 Python
Windows 平台做 Python 开发的最佳组合(推荐)
2020/07/27 Python
canvas烟花特效锦集
2018/01/17 HTML / CSS
台湾网购生鲜第一品牌:i3Fresh爱上新鲜
2017/10/26 全球购物
个人查摆问题整改措施
2014/10/04 职场文书
医学生自荐信范文
2015/03/05 职场文书
自荐信模板大全
2015/03/27 职场文书
2016见义勇为事迹材料汇总
2016/03/01 职场文书
Mac电脑OS系统下安装Nginx的详细教程
2022/04/14 Servers