laravel的api验证失败重定向(Laravel的Auth验证Token验证使用自定义Redis的例子)
类别:编程学习 浏览量:427
时间:2021-10-19 06:37:17 laravel的api验证失败重定向
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 ( 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(); } ... } |
实现代码
因为我们是需要在当前的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 ( 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验证Token验证使用自定义Redis的例子就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持开心学习网。
原文链接:https://blog.csdn.net/ljwaheng/article/details/88547994
您可能感兴趣
- laravel 前后端开发(在Laravel中实现使用AJAX动态刷新部分页面)
- laravel数据库管理教程(Laravel基础_关于view共享数据的示例讲解)
- laravel框架入门项目(在laravel框架中使用model层的方法)
- laravelblade组件(laravel 验证错误信息到 blade模板的方法)
- laravel终止函数执行(laravel 输出最后执行sql 附:whereIn的使用方法)
- laravel定时脚本(laravel实现按时间日期进行分组统计方法示例)
- laravel怎样封装增删改查(laravel框架上传图片实现实时预览功能)
- laravel常用的辅助函数介绍(Laravel向公共模板赋值方法总结)
- laravel 模型(Laravel 模型关联基础教程详解)
- laravel零基础(基于laravel Request的所有方法详解)
- laravel连接多个数据库(laravel实现一个上传图片的接口,并建立软链接,访问图片的方法)
- 安装laravel框架(laravel框架的安装与路由实例分析)
- laravel初始化(Laravel 登录后清空COOKIE的操作方法)
- laravel api 调试(laravel 实现设置时区的简单方法)
- laravel图文分离(Laravel 5.4前后台分离,通过不同的二级域名访问方法)
- laravel数据返回格式(laravel 关联关系遍历数组的例子)
- 朋友圈屏蔽你的人,可以直接看淡了(朋友圈屏蔽你的人)
- 金球奖只青睐那些会戴珠宝的女人(金球奖只青睐那些会戴珠宝的女人)
- 浙江省一个县,人口超40万,建县历史超1100年(浙江省一个县人口超40万)
- 五代十国南唐历代国君(五代十国南唐历代国君)
- 飞机引进工程师杨隆 匠人匠心,只争朝夕(飞机引进工程师杨隆)
- 三人行,她们是育人路上的 铁三角 团队(她们是育人路上的)
热门推荐
- js闭包可以解决哪些问题(JavaScript中let避免闭包造成问题)
- php7 入门(php7新特性的理解和比较总结)
- thinkphp5 api开发(thinkphp5框架前后端分离项目实现分页功能的方法分析)
- Visual Studio 编译中活动平台AnyCPU
- 阿里云服务器怎么连接远程桌面(如何阿里云服务器Windows系统远程桌面端口)
- axios 请求前校验参数(js实现axios限制请求队列)
- sqlserver触发器编写(SQLSever中的触发器基本语法与作用)
- html5播放代码(html5 video全屏播放/自动播放的实现示例)
- Ext.each的用法
- css3动画让元素绕着圆圈转动(css3动画过渡实现鼠标跟随导航效果)
排行榜
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9