亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Laravel的Auth驗(yàn)證Token驗(yàn)證使用自定義Redis的例子

 更新時(shí)間:2019年09月30日 10:48:14   作者:llllllwwwww  
今天小編就為大家分享一篇Laravel的Auth驗(yàn)證Token驗(yàn)證使用自定義Redis的例子,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧

背景

項(xiàng)目用戶量逐漸增大,接口調(diào)用次數(shù)越來(lái)越多,所以決定使用Redis存token,緩解數(shù)據(jù)庫(kù)壓力

調(diào)研

config/auth.php文件中發(fā)現(xiàn)用戶的驅(qū)動(dòng)使用的是EloquentUserProvider服務(wù)提供器,然后查找EloquentUserProvider.php 然后發(fā)現(xiàn)在vendor/laravel/framework/src/Illuminate/Auth文件下存在該文件

<?php
 
namespace Illuminate\Auth;
 
use Illuminate\Support\Str;
use Illuminate\Contracts\Auth\UserProvider;
use Illuminate\Contracts\Hashing\Hasher as HasherContract;
use Illuminate\Contracts\Auth\Authenticatable as UserContract;
 
class EloquentUserProvider implements UserProvider
{
 /**
  * The hasher implementation.
  *
  * @var \Illuminate\Contracts\Hashing\Hasher
  */
 protected $hasher;
 
 /**
  * The Eloquent user model.
  *
  * @var string
  */
 protected $model;
 
 /**
  * Create a new database user provider.
  *
  * @param \Illuminate\Contracts\Hashing\Hasher $hasher
  * @param string $model
  * @return void
  */
 public function __construct(HasherContract $hasher, $model)
 {
  $this->model = $model;
  $this->hasher = $hasher;
 }
 
 /**
  * Retrieve a user by their unique identifier.
  *
  * @param mixed $identifier
  * @return \Illuminate\Contracts\Auth\Authenticatable|null
  */
 public function retrieveById($identifier)
 {
  return $this->createModel()->newQuery()->find($identifier);
 }
 ...
  /**
  * Retrieve a user by the given credentials.
  *
  * @param array $credentials
  * @return \Illuminate\Contracts\Auth\Authenticatable|null
  */
 public function retrieveByCredentials(array $credentials)
 {
  if (empty($credentials)) {
   return;
  }
 
  // First we will add each credential element to the query as a where clause.
  // Then we can execute the query and, if we found a user, return it in a
  // Eloquent User "model" that will be utilized by the Guard instances.
  $query = $this->createModel()->newQuery();
 
  foreach ($credentials as $key => $value) {
   if (! Str::contains($key, 'password')) {
    $query->where($key, $value);
   }
  }
 
  return $query->first();
 }
...
}

實(shí)現(xiàn)代碼

因?yàn)槲覀兪切枰诋?dāng)前的Auth驗(yàn)證基礎(chǔ)之上添加一層Redis緩存,所以最簡(jiǎn)單的辦法繼承EloquentUserProvider類,重寫(xiě)

retrieveByCredentials方法所以我們新建RedisUserProvider.php文件

<?php
namespace App\Providers;
 
use Illuminate\Auth\EloquentUserProvider;
use Cache;
 
class RedisUserProvider extends EloquentUserProvider
{
 
 public function __construct($hasher, $model)
 {
  parent::__construct($hasher, $model);
 }
 /**
  * Retrieve a user by the given credentials.
  *
  * @param array $credentials
  * @return \Illuminate\Contracts\Auth\Authenticatable|null
  */
 public function retrieveByCredentials(array $credentials)
 {
 
  if (!isset($credentials['token'])) {
   return;
  }
 
  $token = $credentials['token'];
  $redis = Cache::getRedis();
  $userId = $redis->get($token);
  
  return $this->retrieveById($userId);
 }
}

然后在AuthServiceProvider.php文件下修改如下代碼

 public function boot(GateContract $gate)
 {
  $this->registerPolicies($gate);
 
  //將redis注入Auth中
  Auth::provider('redis',function($app, $config){
   return new RedisUserProvider($app['hash'], $config['model']);
  });
 }

修改config/auth.php用戶的auth的驅(qū)動(dòng)為redis。

后續(xù)

改完代碼以后發(fā)現(xiàn)無(wú)法正常登錄,一直提示用戶或密碼錯(cuò)誤。。。然后看看了下用戶認(rèn)證方法是

auth('web')->once($credentials);然后看是在
Illuminate\Auth\SessionGuard文件中用到了RedisUserProvider文件中retrieveByCredentials方法中對(duì)用戶進(jìn)行密碼驗(yàn)證,

于是修改RedisUserProvider文件

<?php
namespace App\Providers;
 
use Illuminate\Auth\EloquentUserProvider;
use Illuminate\Support\Str;
use Illuminate\Contracts\Auth\Authenticatable as UserContract;
use Cache;
 
class RedisUserProvider extends EloquentUserProvider
{
 
 public function __construct($hasher, $model)
 {
  parent::__construct($hasher, $model);
 }
 /**
  * Retrieve a user by the given credentials.
  *
  * @param array $credentials
  * @return \Illuminate\Contracts\Auth\Authenticatable|null
  */
 public function retrieveByCredentials(array $credentials)
 {
 
  if (empty($credentials)) {
   return;
  }
  if(isset($credentials['phone']) && isset($credentials['password'])){
   // First we will add each credential element to the query as a where clause.
   // Then we can execute the query and, if we found a user, return it in a
   // Eloquent User "model" that will be utilized by the Guard instances.
   $query = $this->createModel()->newQuery();
 
   foreach ($credentials as $key => $value) {
    if (! Str::contains($key, 'password')) {
     $query->where($key, $value);
    }
   }
 
   return $query->first();
  }
 
  $token = $credentials['token'];
  $redis = Cache::getRedis();
  $userId = $redis->get($token);
 
  return $this->retrieveById($userId);
 }
}

然后登錄成功啦!皆大歡喜!

以上這篇Laravel的Auth驗(yàn)證Token驗(yàn)證使用自定義Redis的例子就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • laravel 5.4中實(shí)現(xiàn)無(wú)限級(jí)分類的方法示例

    laravel 5.4中實(shí)現(xiàn)無(wú)限級(jí)分類的方法示例

    最近在工作中遇到一個(gè)需求,是要在laravel 5.4中實(shí)現(xiàn)無(wú)限級(jí)分類,但發(fā)現(xiàn)網(wǎng)上這個(gè)的資料較少,所以只能自己來(lái)實(shí)現(xiàn)了,下面這篇文章主要給大家介紹了關(guān)于在laravel 5.4中實(shí)現(xiàn)無(wú)限級(jí)分類的方法示例,需要的朋友可以參考借鑒,下面來(lái)一起看看吧。
    2017-07-07
  • laravel安裝zend opcache加速器教程

    laravel安裝zend opcache加速器教程

    這篇文章主要介紹了laravel安裝end opcache加速器教程,需要的朋友可以參考下
    2015-03-03
  • 怎么樣可以把 phpinfo()屏蔽掉?

    怎么樣可以把 phpinfo()屏蔽掉?

    怎么樣可以把 phpinfo()屏蔽掉?...
    2006-11-11
  • php+jQuery+Ajax簡(jiǎn)單實(shí)現(xiàn)頁(yè)面異步刷新

    php+jQuery+Ajax簡(jiǎn)單實(shí)現(xiàn)頁(yè)面異步刷新

    這篇文章主要為大家詳細(xì)介紹了php+jQuery+Ajax簡(jiǎn)單實(shí)現(xiàn)頁(yè)面異步刷新,,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-08-08
  • PHP調(diào)用接口API封裝的例子

    PHP調(diào)用接口API封裝的例子

    今天小編就為大家分享一篇PHP調(diào)用接口API封裝的例子,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-10-10
  • PHP判斷JSON對(duì)象是否存在的方法(推薦)

    PHP判斷JSON對(duì)象是否存在的方法(推薦)

    這篇文章主要介紹了PHP判斷JSON對(duì)象是否存在的方法(推薦)的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2016-07-07
  • Yii框架多語(yǔ)言站點(diǎn)配置方法分析【中文/英文切換站點(diǎn)】

    Yii框架多語(yǔ)言站點(diǎn)配置方法分析【中文/英文切換站點(diǎn)】

    這篇文章主要介紹了Yii框架多語(yǔ)言站點(diǎn)配置方法,結(jié)合實(shí)例形式分析了基于Yii框架實(shí)現(xiàn)中文/英文切換的多語(yǔ)言站點(diǎn)相關(guān)配置方法與操作注意事項(xiàng),需要的朋友可以參考下
    2020-04-04
  • Laravel學(xué)習(xí)筆記之Artisan命令生成自定義模板的方法

    Laravel學(xué)習(xí)筆記之Artisan命令生成自定義模板的方法

    這篇文章主要介紹了Laravel學(xué)習(xí)筆記之Artisan命令生成自定義模板的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-11-11
  • Laravel5.1 框架響應(yīng)基本用法實(shí)例分析

    Laravel5.1 框架響應(yīng)基本用法實(shí)例分析

    這篇文章主要介紹了Laravel5.1 框架響應(yīng)基本用法,結(jié)合實(shí)例形式分析了laravel5.1框架基本響應(yīng)、自定義響應(yīng)頭、返回值、重定向等相關(guān)使用技巧,需要的朋友可以參考下
    2020-01-01
  • phalcon框架使用指南

    phalcon框架使用指南

    Phalcon是一款php中國(guó)外非常的流程的框架了,但在國(guó)內(nèi)Phalcon框架還不怎么樣了,小編今天來(lái)為各位介紹phalcon框架使用教程,希望下文可以幫助到各位
    2016-02-02

最新評(píng)論