濮阳杆衣贸易有限公司

主頁(yè) > 知識(shí)庫(kù) > Laravel的Auth驗(yàn)證Token驗(yàn)證使用自定義Redis的例子

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

熱門(mén)標(biāo)簽:德陽(yáng)中江如何申請(qǐng)400開(kāi)頭電話(huà) 沛縣400電話(huà)辦理 江蘇電商外呼系統(tǒng)運(yùn)營(yíng)商 AI電話(huà)機(jī)器人OEM貼牌 智能電話(huà)機(jī)器人好公司門(mén)薩維 聊城電話(huà)外呼系統(tǒng)公司 青白江地圖標(biāo)注 銅川電話(huà)機(jī)器人價(jià)格 辦理重慶400電話(huà)

背景

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

調(diào)研

config/auth.php文件中發(fā)現(xiàn)用戶(hù)的驅(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類(lèi),重寫(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用戶(hù)的auth的驅(qū)動(dòng)為redis。

后續(xù)

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

auth('web')->once($credentials);然后看是在
Illuminate\Auth\SessionGuard文件中用到了RedisUserProvider文件中retrieveByCredentials方法中對(duì)用戶(hù)進(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è)參考,也希望大家多多支持腳本之家。

您可能感興趣的文章:
  • PHP的Laravel框架結(jié)合MySQL與Redis數(shù)據(jù)庫(kù)的使用部署
  • Laravel框架使用Redis的方法詳解
  • laravel使用Redis實(shí)現(xiàn)網(wǎng)站緩存讀取的方法詳解
  • 關(guān)于 Laravel Redis 多個(gè)進(jìn)程同時(shí)取隊(duì)列問(wèn)題詳解
  • Redis在Laravel項(xiàng)目中的應(yīng)用實(shí)例詳解
  • Laravel框架實(shí)現(xiàn)redis集群的方法分析
  • Laravel如何使用Redis共享Session
  • laravel配置Redis多個(gè)庫(kù)的實(shí)現(xiàn)方法
  • laravel項(xiàng)目利用twemproxy部署redis集群的完整步驟
  • laravel中Redis隊(duì)列監(jiān)聽(tīng)中斷的分析

標(biāo)簽:山南 鷹潭 赤峰 三亞 南寧 濟(jì)寧 烏魯木齊 迪慶

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Laravel的Auth驗(yàn)證Token驗(yàn)證使用自定義Redis的例子》,本文關(guān)鍵詞  Laravel,的,Auth,驗(yàn)證,Token,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《Laravel的Auth驗(yàn)證Token驗(yàn)證使用自定義Redis的例子》相關(guān)的同類(lèi)信息!
  • 本頁(yè)收集關(guān)于Laravel的Auth驗(yàn)證Token驗(yàn)證使用自定義Redis的例子的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    缙云县| 琼海市| 喜德县| 哈尔滨市| 淮南市| 肃宁县| 河间市| 冕宁县| 锡林郭勒盟| 五原县| 阿巴嘎旗| 白玉县| 易门县| 新丰县| 饶河县| 徐州市| 平乡县| 济源市| 涿州市| 揭阳市| 启东市| 绵竹市| 莒南县| 稷山县| 石河子市| 江安县| 石泉县| 江门市| 嵩明县| 资源县| 六枝特区| 弋阳县| 吉首市| 年辖:市辖区| 金寨县| 潮州市| 柳江县| 牡丹江市| 仁怀市| 双江| 五常市|