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 之入门篇
Dec 04 PHP
php下批量挂马和批量清马代码
Feb 27 PHP
解析PHP留言本模块主要功能的函数说明(代码可实现)
Jun 25 PHP
PHP jQuery表单,带验证具体实现方法
Feb 15 PHP
PHP四大安全策略
Mar 12 PHP
php中explode函数用法分析
Nov 15 PHP
PHP利用APC模块实现文件上传进度条的方法
Jan 26 PHP
php检查字符串中是否有外链的方法
Jul 29 PHP
PHP闭包函数详解
Feb 13 PHP
php5.2的curl-bug 服务器被php进程卡死问题排查
Sep 19 PHP
PHP 表单提交及处理表单数据详解及实例
Dec 27 PHP
php 读取文件夹下所有图片、文件的实例
Oct 17 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
收集的DedeCMS一些使用经验
2007/03/17 PHP
php中批量替换文件名的实现代码
2011/07/20 PHP
PHP使用数组实现队列
2012/02/05 PHP
PHP中设置时区方法小结
2012/06/03 PHP
如何利用PHP执行.SQL文件
2013/07/05 PHP
CI框架源码解读之URI.php中_fetch_uri_string()函数用法分析
2016/05/18 PHP
Yii2.0中的COOKIE和SESSION用法
2016/08/12 PHP
PHP反射API示例分享
2016/10/08 PHP
Laravel框架基于ajax实现二级联动功能示例
2019/01/17 PHP
Laravel框架源码解析之模型Model原理与用法解析
2020/05/14 PHP
Jquery Validation插件防止重复提交表单的解决方法
2010/03/05 Javascript
使用ExtJS技术实现的拖动树结点
2010/08/05 Javascript
使用JQuery实现智能表单验证功能
2016/03/08 Javascript
VUEJS实战之构建基础并渲染出列表(1)
2016/06/13 Javascript
微信小程序 定义全局数据、函数复用、模版等详细介绍
2016/10/27 Javascript
Vue+Element使用富文本编辑器的示例代码
2017/08/14 Javascript
Bootstrap table 实现树形表格联动选中联动取消功能
2019/09/30 Javascript
Vue按时间段查询数据组件使用详解
2020/08/21 Javascript
Ant Design的Table组件去除
2020/10/24 Javascript
Python基于list的append和pop方法实现堆栈与队列功能示例
2017/07/24 Python
11个Python Pandas小技巧让你的工作更高效(附代码实例)
2019/04/30 Python
用pycharm开发django项目示例代码
2019/06/13 Python
Pandas时间序列:重采样及频率转换方式
2019/12/26 Python
使用Tensorflow将自己的数据分割成batch训练实例
2020/01/20 Python
Python读取文件内容为字符串的方法(多种方法详解)
2020/03/04 Python
django 装饰器 检测登录状态操作
2020/07/02 Python
英国家电直销:Appliances Direct
2016/09/22 全球购物
手工制作的男士奢华英国鞋和服装之家:Goodwin Smith
2019/06/21 全球购物
交通安全演讲稿
2014/01/07 职场文书
带薪年假请假条
2014/02/04 职场文书
开业典礼主持词
2014/03/21 职场文书
交通事故委托书范本(2篇)
2014/09/21 职场文书
领导个人查摆剖析材料
2014/10/29 职场文书
个人德育工作总结
2015/03/05 职场文书
节水宣传标语口号
2015/12/26 职场文书
MySQL数据库简介与基本操作
2022/05/30 MySQL