设为首页收藏本站
网站公告 | 这是第一条公告
     

 找回密码
 立即注册
缓存时间18 现在时间18 缓存数据 你我最后竟然也平凡到自命不凡。

你我最后竟然也平凡到自命不凡。 -- 像我这样的人

查看: 1369|回复: 3

laravel修改用户模块的密码验证实现

[复制链接]

  离线 

TA的专栏

  • 打卡等级:热心大叔
  • 打卡总天数:231
  • 打卡月天数:0
  • 打卡总奖励:4009
  • 最近打卡:2025-03-13 10:31:57
等级头衔

等級:晓枫资讯-上等兵

在线时间
4 小时

积分成就
威望
0
贡献
446
主题
394
精华
0
金钱
5304
积分
904
注册时间
2023-1-7
最后登录
2025-5-31

发表于 2023-2-12 20:00:25 | 显示全部楼层 |阅读模式
做项目的时候,用户认证几乎是必不可少的,如果我们的项目由于一些原因不得不使用 users 之外的用户表进行认证,那么就需要多做一点工作来完成这个功能。
现在假设我们只需要修改登录用户的表,表名和表结构都与框架默认的表users不同,文档没有教我们如何去做,但是别慌,稍微看下框架实现用户认证的源码就能轻松实现。
首先,自定义一张表用来登录,表结构和模拟数据如下:
表 admins
            id            login_name            login_pass                                    1            admin            2y10$2MUhp7b6ghVOngb/.b/x6uuEW/yL3FqPKJztawrM0U577Clf07xda        

从配置文件入手


用户认证相关的配置都保存在config/auth.php文件中,先来看看配置文件的内容:
  1.         <?php
  2.         
  3.         return [
  4.         
  5.             /*
  6.             |--------------------------------------------------------------------------
  7.             | Authentication Defaults
  8.             |--------------------------------------------------------------------------
  9.             |
  10.             | This option controls the default authentication "guard" and password
  11.             | reset options for your application. You may change these defaults
  12.             | as required, but they're a perfect start for most applications.
  13.             |
  14.             */
  15.         
  16.             'defaults' => [
  17.                 'guard' => 'web',
  18.                 'passwords' => 'users',
  19.             ],
  20.         
  21.             /*
  22.         |--------------------------------------------------------------------------
  23.         | Authentication Guards
  24.         |--------------------------------------------------------------------------
  25.         |
  26.         | Next, you may define every authentication guard for your application.
  27.         | Of course, a great default configuration has been defined for you
  28.         | here which uses session storage and the Eloquent user provider.
  29.         |
  30.         | All authentication drivers have a user provider. This defines how the
  31.         | users are actually retrieved out of your database or other storage
  32.         | mechanisms used by this application to persist your user's data.
  33.         |
  34.         | Supported: "session", "token"
  35.         |
  36.         */
  37.    
  38.         'guards' => [
  39.             'web' => [
  40.                 'driver' => 'session',
  41.                 'provider' => 'users',
  42.             ],
  43.    
  44.             'api' => [
  45.                 'driver' => 'passport',
  46.                 'provider' => 'users',
  47.             ],
  48.         ],
  49.    
  50.         /*
  51.         |--------------------------------------------------------------------------
  52.         | User Providers
  53.         |--------------------------------------------------------------------------
  54.         |
  55.         | All authentication drivers have a user provider. This defines how the
  56.         | users are actually retrieved out of your database or other storage
  57.         | mechanisms used by this application to persist your user's data.
  58.         |
  59.         | If you have multiple user tables or models you may configure multiple
  60.         | sources which represent each model / table. These sources may then
  61.         | be assigned to any extra authentication guards you have defined.
  62.         |
  63.         | Supported: "database", "eloquent"
  64.         |
  65.         */
  66.    
  67.         'providers' => [
  68.             'users' => [
  69.                 'driver' => 'eloquent',
  70.                 'model' => App\User::class,
  71.             ],
  72.    
  73.             // 'users' => [
  74.             //     'driver' => 'database',
  75.             //     'table' => 'users',
  76.             // ],
  77.         ],
  78.    
  79.         /*
  80.         |--------------------------------------------------------------------------
  81.         | Resetting Passwords
  82.         |--------------------------------------------------------------------------
  83.         |
  84.         | You may specify multiple password reset configurations if you have more
  85.         | than one user table or model in the application and you want to have
  86.         | separate password reset settings based on the specific user types.
  87.         |
  88.         | The expire time is the number of minutes that the reset token should be
  89.         | considered valid. This security feature keeps tokens short-lived so
  90.         | they have less time to be guessed. You may change this as needed.
  91.         |
  92.         */
  93.    
  94.         'passwords' => [
  95.             'users' => [
  96.                 'provider' => 'users',
  97.                 'table' => 'password_resets',
  98.                 'expire' => 60,
  99.             ],
  100.         ],
  101.    
  102.     ];
复制代码
默认使用的守卫是web,而web守卫使用的认证驱动是session,用户提供器是users。假设我们的需求只是将用户的提供器由users改为admins,那么我们需要做两步操作:
修改默认的用户提供器,将provider=>'users'改为provider=>'admins'
  1.           'guards' => [
  2.                 'web' => [
  3.                     'driver' => 'session',
  4.                     'provider' => 'users',
  5.                 ],
  6.             ],
复制代码
配置admins提供器,假设依旧使用eloquent作为驱动,并创建好了admins表的模型
  1.     'providers' => [
  2.             'admins' => [
  3.                 'driver' => 'eloquent',
  4.                 'model' => App\Admin::class
  5.             ]
  6.         ],
复制代码
使用Auth门面的attempt方法进行登录


SessionGuard 中的attempt方法:
  1.     //Illuminate\Auth\SessionGuard
  2.      public function attempt(array $credentials = [], $remember = false)
  3.         {
  4.             $this->fireAttemptEvent($credentials, $remember);
  5.    
  6.             $this->lastAttempted = $user = $this->provider->retrieveByCredentials($credentials);
  7.    
  8.             // If an implementation of UserInterface was returned, we'll ask the provider
  9.             // to validate the user against the given credentials, and if they are in
  10.             // fact valid we'll log the users into the application and return true.
  11.             if ($this->hasValidCredentials($user, $credentials)) {
  12.                 $this->login($user, $remember);
  13.    
  14.                 return true;
  15.             }
  16.    
  17.             // If the authentication attempt fails we will fire an event so that the user
  18.             // may be notified of any suspicious attempts to access their account from
  19.             // an unrecognized user. A developer may listen to this event as needed.
  20.             $this->fireFailedEvent($user, $credentials);
  21.    
  22.             return false;
  23.         }
复制代码
该方法中调用 UserProvider 接口的retrieveByCredentials方法检索用户,根据我们的配置,UserProvider接口的具体实现应该是EloquentUserProvider,因此,我们定位到EloquentUserProvider的retrieveByCredentials方法:
  1.     //Illuminate\Auth\EloquentUserProvider
  2.     public function retrieveByCredentials(array $credentials)
  3.         {
  4.             if (empty($credentials) ||
  5.                (count($credentials) === 1 &&
  6.                 array_key_exists('password', $credentials))) {
  7.                 return;
  8.             }
  9.    
  10.             // First we will add each credential element to the query as a where clause.
  11.             // Then we can execute the query and, if we found a user, return it in a
  12.             // Eloquent User "model" that will be utilized by the Guard instances.
  13.             $query = $this->createModel()->newQuery();
  14.    
  15.             foreach ($credentials as $key => $value) {
  16.                 if (Str::contains($key, 'password')) {
  17.                     continue;
  18.                 }
  19.    
  20.                 if (is_array($value) || $value instanceof Arrayable) {
  21.                     $query->whereIn($key, $value);
  22.                 } else {
  23.                     $query->where($key, $value);
  24.                 }
  25.             }
  26.    
  27.             return $query->first();
  28.         }
复制代码
该方法会使用传入的参数(不包含password)到我们配置的数据表中搜索数据,查询到符合条件的数据之后返回对应的用户信息,然后attempt方法会进行密码校验,校验密码的方法为:
  1.     //Illuminate\Auth\SessionGuard
  2.     /**
  3.          * Determine if the user matches the credentials.
  4.          *
  5.          * @param  mixed  $user
  6.          * @param  array  $credentials
  7.          * @return bool
  8.          */
  9.         protected function hasValidCredentials($user, $credentials)
  10.         {
  11.             return ! is_null($user) && $this->provider->validateCredentials($user, $credentials);
  12.         }
复制代码
进一步查看EloquentUserProvider中的validateCredentials方法
  1.     //Illuminate\Auth\EloquentUserProvider
  2.     public function validateCredentials(UserContract $user, array $credentials)
  3.     {
  4.         $plain = $credentials['password'];
  5.    
  6.         return $this->hasher->check($plain, $user->getAuthPassword());
  7.     }
复制代码
通过validateCredentials可以看出,提交的认证数据中密码字段名必须是password,这个无法自定义。同时可以看到,入参$user必须实现Illuminate\Contracts\Auth\Authenticatable接口(UserContract是别名)。

修改 Admin 模型


Admin模型必须实现Illuminate\Contracts\Auth\Authenticatable接口,可以借鉴一下User模型,让Admin直接继承Illuminate\Foundation\Auth\User 就可以,然后重写getAuthPassword方法,正确获取密码字段:
  1.     // App\Admin
  2.     public function getAuthPassword()
  3.     {
  4.         return $this->login_pass;
  5.     }
复制代码
不出意外的话,这个时候就能使用admins表进行登录了。
Larval 5.4的默认Auth登陆传入邮件和用户密码到attempt 方法来认证,通过email 的值获取,如果用户被找到,经哈希运算后存储在数据中的password将会和传递过来的经哈希运算处理的passwrod值进行比较。如果两个经哈希运算的密码相匹配那么将会为这个用户开启一个认证Session。
参考上面的分析,我们就需要对EloquentUserProvider中的validateCredentials方法进行重写,步骤如下
1. 修改 App\Models\User.php 添加如下代码
  1.     public function getAuthPassword()
  2.         {
  3.             return ['password' => $this->attributes['password'], 'salt' => $this->attributes['salt']];
  4.         }
复制代码
2. 建立一个自己的UserProvider.php 的实现
  1.     <?php
  2.     namespace App\Foundation\Auth;
  3.    
  4.     use Illuminate\Auth\EloquentUserProvider;
  5.     use Illuminate\Contracts\Auth\Authenticatable;
  6.     use Illuminate\Support\Str;
  7.    
  8.     /**
  9.      * 重写用户密码校验逻辑
  10.      * Class GfzxEloquentUserProvider
  11.      * @package App\Foundation\Auth
  12.      */
  13.     class GfzxEloquentUserProvider extends EloquentUserProvider
  14.     {
  15.         /**
  16.          * Validate a user against the given credentials.
  17.          *
  18.          * @param  \Illuminate\Contracts\Auth\Authenticatable $user
  19.          * @param  array $credentials
  20.          * @return bool
  21.          */
  22.         public function validateCredentials(Authenticatable $user, array $credentials)
  23.         {
  24.             $plain = $credentials['password'];
  25.             $authPassword = $user->getAuthPassword();
  26.             return md5($plain . $authPassword['salt']) == $authPassword['password'];
  27.         }
  28.     }
复制代码
3. 将User Providers换成我们自己的GfzxEloquentUserProvider
修改 app/Providers/AuthServiceProvider.php
  1.     <?php
  2.    
  3.     namespace App\Providers;
  4.    
  5.     use App\Foundation\Auth\GfzxEloquentUserProvider;
  6.     use Auth;
  7.     use Illuminate\Support\Facades\Gate;
  8.     use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
  9.    
  10.     class AuthServiceProvider extends ServiceProvider
  11.     {
  12.         .
  13.         .
  14.         .
  15.    
  16.         /**
  17.          * Register any authentication / authorization services.
  18.          *
  19.          * @return void
  20.          */
  21.         public function boot()
  22.         {
  23.             $this->registerPolicies();
  24.    
  25.             Auth::provider('gfzx-eloquent', function ($app, $config) {
  26.                 return new GfzxEloquentUserProvider($this->app['hash'], $config['model']);
  27.             });
  28.         }
  29.     }
复制代码
4. 修改 config/auth.php
  1.        'providers' => [
  2.             'users' => [
  3.                 'driver' => 'gfzx-eloquent',
  4.                 'model' => App\Models\User::class,
  5.             ],
  6.         ],
复制代码
这是就可以用过salt+passwrod的方式密码认证了

文章参考


laravel 修改用户模块密码验证
Laravel 中自定义用户登录的数据表

到此这篇关于laravel修改用户模块的密码验证实现的文章就介绍到这了,更多相关laravel修改用户模块的密码验证内容请搜索晓枫资讯以前的文章或继续浏览下面的相关文章希望大家以后多多支持晓枫资讯!

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
晓枫资讯-科技资讯社区-免责声明
免责声明:以上内容为本网站转自其它媒体,相关信息仅为传递更多信息之目的,不代表本网观点,亦不代表本网站赞同其观点或证实其内容的真实性。
      1、注册用户在本社区发表、转载的任何作品仅代表其个人观点,不代表本社区认同其观点。
      2、管理员及版主有权在不事先通知或不经作者准许的情况下删除其在本社区所发表的文章。
      3、本社区的文章部分内容可能来源于网络,仅供大家学习与参考,如有侵权,举报反馈:点击这里给我发消息进行删除处理。
      4、本社区一切资源不代表本站立场,并不代表本站赞同其观点和对其真实性负责。
      5、以上声明内容的最终解释权归《晓枫资讯-科技资讯社区》所有。
http://bbs.yzwlo.com 晓枫资讯--游戏IT新闻资讯~~~

  离线 

TA的专栏

  • 打卡等级:即来则安
  • 打卡总天数:23
  • 打卡月天数:0
  • 打卡总奖励:322
  • 最近打卡:2025-03-09 09:42:44
等级头衔

等級:晓枫资讯-列兵

在线时间
0 小时

积分成就
威望
0
贡献
0
主题
0
精华
0
金钱
358
积分
48
注册时间
2023-1-13
最后登录
2025-3-9

发表于 2024-11-24 03:04:38 | 显示全部楼层
感谢楼主分享。
http://bbs.yzwlo.com 晓枫资讯--游戏IT新闻资讯~~~

  离线 

TA的专栏

等级头衔

等級:晓枫资讯-列兵

在线时间
0 小时

积分成就
威望
0
贡献
0
主题
0
精华
0
金钱
15
积分
10
注册时间
2022-12-24
最后登录
2022-12-24

发表于 2025-3-4 18:11:32 | 显示全部楼层
顶顶更健康!!!
http://bbs.yzwlo.com 晓枫资讯--游戏IT新闻资讯~~~

  离线 

TA的专栏

等级头衔

等級:晓枫资讯-列兵

在线时间
0 小时

积分成就
威望
0
贡献
0
主题
0
精华
0
金钱
17
积分
14
注册时间
2022-12-26
最后登录
2022-12-26

发表于 2025-4-14 21:33:19 | 显示全部楼层
路过,支持一下
http://bbs.yzwlo.com 晓枫资讯--游戏IT新闻资讯~~~
严禁发布广告,淫秽、色情、赌博、暴力、凶杀、恐怖、间谍及其他违反国家法律法规的内容。!晓枫资讯-社区
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

1楼
2楼
3楼
4楼

手机版|晓枫资讯--科技资讯社区 本站已运行

CopyRight © 2022-2025 晓枫资讯--科技资讯社区 ( BBS.yzwlo.com ) . All Rights Reserved .

晓枫资讯--科技资讯社区

本站内容由用户自主分享和转载自互联网,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责。

如有侵权、违反国家法律政策行为,请联系我们,我们会第一时间及时清除和处理! 举报反馈邮箱:点击这里给我发消息

Powered by Discuz! X3.5

快速回复 返回顶部 返回列表