背景
项目用户量逐渐增大,接口调用次数越来越多,所以决定使用Redis存token,缓解数据库压力
调研
在config/auth.php文件中发现用户的驱动使用的是EloquentUserProvider服务提供器,然后查找EloquentUserProvider.php 然后发现在vendor/laravel/framework/src/Illuminate/Auth文件下存在该文件
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
<?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文件
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
<?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文件下修改如下代码
?
|
1
2
3
4
5
6
7
8
9
|
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。
后续
改完代码以后发现无法正常登录,一直提示用户或密码错误。。。然后看看了下用户认证方法是
?
|
1
|
auth('web')->once($credentials);然后看是在
|
?
|
1
|
Illuminate\\Auth\\SessionGuard文件中用到了RedisUserProvider文件中retrieveByCredentials方法中对用户进行密码验证,
|
于是修改RedisUserProvider文件
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
<?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的例子就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持快网idc。
原文链接:https://blog.csdn.net/ljwaheng/article/details/88547994
相关文章
猜你喜欢
- ASP.NET自助建站系统中如何实现多语言支持? 2025-06-10
- 64M VPS建站:如何选择最适合的网站建设平台? 2025-06-10
- ASP.NET本地开发时常见的配置错误及解决方法? 2025-06-10
- ASP.NET自助建站系统的数据库备份与恢复操作指南 2025-06-10
- 个人网站服务器域名解析设置指南:从购买到绑定全流程 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-06-04 83
-
2025-05-29 62
-
2025-05-25 37
-
2025-05-29 12
-
2025-05-27 40
热门评论

