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 来访国内外IP判断代码并实现页面跳转
Dec 18 PHP
zf框架的Filter过滤器使用示例
Mar 13 PHP
php通过function_exists检测函数是否存在的方法
Mar 18 PHP
WordPress的文章自动添加关键词及关键词的SEO优化
Mar 01 PHP
支付宝支付开发――当面付条码支付和扫码支付实例
Nov 04 PHP
php字符集转换
Jan 23 PHP
Thinkphp结合ajaxFileUpload实现异步图片传输示例
Mar 13 PHP
基于thinkPHP3.2实现微信接入及查询token值的方法
Apr 18 PHP
PHP基于DOMDocument解析和生成xml的方法分析
Jul 17 PHP
利用PHP访问MySql数据库的逻辑操作以及增删改查的实例讲解
Aug 30 PHP
PHP排序算法之简单选择排序(Simple Selection Sort)实例分析
Apr 20 PHP
Smarty缓存机制实例详解【三种缓存方式】
Jul 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
德劲1102收音机的打理维修案例
2021/03/02 无线电
PHP set_time_limit(0)长连接的实现分析
2010/03/02 PHP
浅析php插件 HTMLPurifier HTML解析器
2013/07/01 PHP
部署PHP时的4个配置修改说明
2015/10/19 PHP
php在windows环境下获得cpu内存实时使用率(推荐)
2018/02/08 PHP
ThinkPHP中图片按比例切割的代码实例
2019/03/08 PHP
PHP消息队列实现及应用详解【队列处理订单系统和配送系统】
2019/05/20 PHP
JavaScript EasyPager 分页函数
2011/05/25 Javascript
结合JQ1.9通过js正则判断各种浏览器版本的方法
2013/12/30 Javascript
js动态删除div元素基本思路及实现代码
2014/05/08 Javascript
利用Angularjs和Bootstrap前端开发案例实战
2016/08/27 Javascript
jQuery 获取select选中值及清除选中状态
2016/12/13 Javascript
jQuery实现倒计时重新发送短信验证码功能示例
2017/01/12 Javascript
Vue编写多地区选择组件
2017/08/21 Javascript
vue2.0 实现页面导航提示引导的方法
2018/03/13 Javascript
vue中动态设置meta标签和title标签的方法
2018/07/11 Javascript
使用jQuery给Table动态增加行、清空table的方法
2018/09/05 jQuery
React Native开发封装Toast与加载Loading组件示例
2018/09/08 Javascript
Python中的生成器和yield详细介绍
2015/01/09 Python
12步教你理解Python装饰器
2016/02/25 Python
python 默认参数问题的陷阱
2016/02/29 Python
spyder常用快捷键(分享)
2017/07/19 Python
python虚拟环境virtualenv的使用教程
2017/10/20 Python
Python序列循环移位的3种方法推荐
2018/04/09 Python
Tensorflow 多线程设置方式
2020/02/06 Python
python语言实现贪吃蛇游戏
2020/11/13 Python
德国珠宝和手表在线商店:VALMANO
2019/03/24 全球购物
电信专业毕业生推荐信
2013/11/18 职场文书
陈欧广告词
2014/03/14 职场文书
函授本科个人自我鉴定
2014/03/25 职场文书
护士工作失误检讨书
2014/09/14 职场文书
2014年生产部工作总结
2014/12/17 职场文书
民间借贷纠纷案件代理词
2015/05/26 职场文书
处罚决定书范文
2015/06/24 职场文书
大学生暑假实习总结
2015/07/13 职场文书
ajax请求前端跨域问题原因及解决方案
2021/10/16 Javascript