Laravel的Auth验证Token验证使用自定义Redis的例子
发布:smiling 来源: PHP粉丝网 添加日期:2021-12-25 16:22:32 浏览: 评论:0
今天小编就为大家分享一篇Laravel的Auth验证Token验证使用自定义Redis的例子,具有很好的参考价值,希望对大家有所帮助,一起跟随小编过来看看吧。
背景
项目用户量逐渐增大,接口调用次数越来越多,所以决定使用Redis存token,缓解数据库压力
调研
在config/auth.php文件中发现用户的驱动使用的是EloquentUserProvider服务提供器,然后查找EloquentUserProvider.php 然后发现在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 (emptyempty($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();
- }
- ...
- }
实现代码
因为我们是需要在当前的Auth验证基础之上添加一层Redis缓存,所以最简单的办法继承EloquentUserProvider类,重写
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的驱动为redis。
后续
改完代码以后发现无法正常登录,一直提示用户或密码错误。。。然后看看了下用户认证方法是
auth('web')->once($credentials);然后看是在
Illuminate\Auth\SessionGuard文件中用到了RedisUserProvider文件中retrieveByCredentials方法中对用户进行密码验证,
于是修改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 (emptyempty($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);
- }
- }
然后登录成功啦!皆大欢喜!
Tags: Laravel验证 Token验证 Redis
相关文章
- ·laravel 验证错误信息到 blade模板的方法(2021-12-24)
- ·基于Laravel 5.2 regex验证的正确写法(2021-12-24)
- ·Laravel 自带的Auth验证登录方法(2021-12-25)
- ·laravel unique验证、确认密码confirmed验证以及密码修改验证的方法(2022-01-05)
- ·PHP的Laravel框架结合MySQL与Redis数据库的使用部署(2021-07-20)
- ·CI框架中redis缓存相关操作文件示例代码(2021-08-06)
- ·CI框架(CodeIgniter)操作redis的方法详解(2021-08-31)
- ·Laravel如何使用Redis共享Session(2021-09-03)
- ·laravel使用Redis实现网站缓存读取的方法详解(2021-09-05)
- ·php框架CodeIgniter使用redis的方法分析(2021-09-08)
- ·Laravel框架使用Redis的方法详解(2021-09-21)
- ·Yii2框架redis基本应用示例(2021-10-16)
- ·PHP实现负载均衡session共享redis缓存操作示例(2021-10-26)
- ·laravel配置Redis多个库的实现方法(2021-11-16)
- ·Yii框架的redis命令使用方法简单示例(2022-01-04)
推荐文章
热门文章
最新评论文章
- 写给考虑创业的年轻程序员(10)
- PHP新手上路(一)(7)
- 惹恼程序员的十件事(5)
- PHP邮件发送例子,已测试成功(5)
- 致初学者:PHP比ASP优秀的七个理由(4)
- PHP会被淘汰吗?(4)
- PHP新手上路(四)(4)
- 如何去学习PHP?(2)
- 简单入门级php分页代码(2)
- php中邮箱email 电话等格式的验证(2)