做项目的时候,用户认证几乎是必不可少的,如果我们的项目由于一些原因不得不使用 users
之外的用户表进行认证,那么就需要多做一点工作来完成这个功能。
现在假设我们只需要修改登录用户的表,表名和表结构都与框架默认的表users
不同,文档没有教我们如何去做,但是别慌,稍微看下框架实现用户认证的源码就能轻松实现。
首先,自定义一张表用来登录,表结构和模拟数据如下:
表 admins
id login_name login_pass 1 admin 2y2y10$2MUhp7b6ghVOngb/.b/x6uuEW/yL3FqPKJztawrM0U577Clf07xda
从配置文件入手
用户认证相关的配置都保存在config/auth.php文件中,先来看看配置文件的内容:
| <?php |
| |
| return [ |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| 'defaults' => [ |
| 'guard' => 'web', |
| 'passwords' => 'users', |
| ], |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| 'guards' => [ |
| 'web' => [ |
| 'driver' => 'session', |
| 'provider' => 'users', |
| ], |
| |
| 'api' => [ |
| 'driver' => 'passport', |
| 'provider' => 'users', |
| ], |
| ], |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| 'providers' => [ |
| 'users' => [ |
| 'driver' => 'eloquent', |
| 'model' => App\User::class, |
| ], |
| |
| |
| |
| |
| |
| ], |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| 'passwords' => [ |
| 'users' => [ |
| 'provider' => 'users', |
| 'table' => 'password_resets', |
| 'expire' => 60, |
| ], |
| ], |
| |
| ]; |
默认使用的守卫是web,而web守卫使用的认证驱动是session,用户提供器是users。假设我们的需求只是将用户的提供器由users改为admins,那么我们需要做两步操作:
- 修改默认的用户提供器,将
provider=>'users'
改为provider=>'admins'
| 'guards' => [ |
| 'web' => [ |
| 'driver' => 'session', |
| 'provider' => 'users', |
| ], |
| ], |
- 配置
admins
提供器,假设依旧使用eloquent
作为驱动,并创建好了admins
表的模型
| 'providers' => [ |
| 'admins' => [ |
| 'driver' => 'eloquent', |
| 'model' => App\Admin::class |
| ] |
| ], |
使用Auth
门面的attempt
方法进行登录
SessionGuard
中的attempt
方法:
| |
| public function attempt(array $credentials = [], $remember = false) |
| { |
| $this->fireAttemptEvent($credentials, $remember); |
| |
| $this->lastAttempted = $user = $this->provider->retrieveByCredentials($credentials); |
| |
| |
| |
| |
| if ($this->hasValidCredentials($user, $credentials)) { |
| $this->login($user, $remember); |
| |
| return true; |
| } |
| |
| |
| |
| |
| $this->fireFailedEvent($user, $credentials); |
| |
| return false; |
| } |
该方法中调用 UserProvider
接口的retrieveByCredentials
方法检索用户,根据我们的配置,UserProvider
接口的具体实现应该是EloquentUserProvider
,因此,我们定位到EloquentUserProvider
的retrieveByCredentials
方法:
| |
| public function retrieveByCredentials(array $credentials) |
| { |
| if (empty($credentials) || |
| (count($credentials) === 1 && |
| array_key_exists('password', $credentials))) { |
| return; |
| } |
| |
| |
| |
| |
| $query = $this->createModel()->newQuery(); |
| |
| foreach ($credentials as $key => $value) { |
| if (Str::contains($key, 'password')) { |
| continue; |
| } |
| |
| if (is_array($value) || $value instanceof Arrayable) { |
| $query->whereIn($key, $value); |
| } else { |
| $query->where($key, $value); |
| } |
| } |
| |
| return $query->first(); |
| } |
该方法会使用传入的参数(不包含password
)到我们配置的数据表中搜索数据,查询到符合条件的数据之后返回对应的用户信息,然后attempt
方法会进行密码校验,校验密码的方法为:
| |
| |
| |
| |
| |
| |
| |
| |
| protected function hasValidCredentials($user, $credentials) |
| { |
| return ! is_null($user) && $this->provider->validateCredentials($user, $credentials); |
| } |
进一步查看EloquentUserProvider
中的validateCredentials
方法
| |
| public function validateCredentials(UserContract $user, array $credentials) |
| { |
| $plain = $credentials['password']; |
| |
| return $this->hasher->check($plain, $user->getAuthPassword()); |
| } |
通过validateCredentials
可以看出,提交的认证数据中密码字段名必须是password
,这个无法自定义。同时可以看到,入参$user
必须实现Illuminate\Contracts\Auth\Authenticatable
接口(UserContract
是别名)。
修改 Admin
模型
Admin模型必须实现Illuminate\Contracts\Auth\Authenticatable
接口,可以借鉴一下User
模型,让Admin
直接继承Illuminate\Foundation\Auth\User
就可以,然后重写getAuthPassword
方法,正确获取密码字段:
| |
| public function getAuthPassword() |
| { |
| return $this->login_pass; |
| } |
不出意外的话,这个时候就能使用admins
表进行登录了。
Larval 5.4的默认Auth登陆传入邮件和用户密码到attempt 方法来认证,通过email 的值获取,如果用户被找到,经哈希运算后存储在数据中的password将会和传递过来的经哈希运算处理的passwrod值进行比较。如果两个经哈希运算的密码相匹配那么将会为这个用户开启一个认证Session。
参考上面的分析,我们就需要对EloquentUserProvider
中的validateCredentials
方法进行重写,步骤如下:
1. 修改 App\Models\User.php 添加如下代码
| public function getAuthPassword() |
| { |
| return ['password' => $this->attributes['password'], 'salt' => $this->attributes['salt']]; |
| } |
2. 建立一个自己的UserProvider.php 的实现
| <?php |
| namespace App\Foundation\Auth; |
| |
| use Illuminate\Auth\EloquentUserProvider; |
| use Illuminate\Contracts\Auth\Authenticatable; |
| use Illuminate\Support\Str; |
| |
| |
| |
| |
| |
| |
| class GfzxEloquentUserProvider extends EloquentUserProvider |
| { |
| |
| |
| |
| |
| |
| |
| |
| public function validateCredentials(Authenticatable $user, array $credentials) |
| { |
| $plain = $credentials['password']; |
| $authPassword = $user->getAuthPassword(); |
| return md5($plain . $authPassword['salt']) == $authPassword['password']; |
| } |
| } |
3. 将User Providers换成我们自己的GfzxEloquentUserProvider
修改 app/Providers/AuthServiceProvider.php
| <?php |
| |
| namespace App\Providers; |
| |
| use App\Foundation\Auth\GfzxEloquentUserProvider; |
| use Auth; |
| use Illuminate\Support\Facades\Gate; |
| use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider; |
| |
| class AuthServiceProvider extends ServiceProvider |
| { |
| |
| |
| |
| |
| |
| public function boot() |
| { |
| $this->registerPolicies(); |
| |
| Auth::provider('gfzx-eloquent', function ($app, $config) { |
| return new GfzxEloquentUserProvider($this->app['hash'], $config['model']); |
| }); |
| } |
| } |
4. 修改 config/auth.php
| 'providers' => [ |
| 'users' => [ |
| 'driver' => 'gfzx-eloquent', |
| 'model' => App\Models\User::class, |
| ], |
| ], |
| |
这是就可以用过salt+passwrod的方式密码认证了
文章来源