laravel修改用戶模塊的密碼驗(yàn)證實(shí)現(xiàn)
目錄
- 從配置文件入手
- 使用Auth門面的attempt方法進(jìn)行登錄
- 修改 Admin 模型
- 文章參考
做項(xiàng)目的時(shí)候,用戶認(rèn)證幾乎是必不可少的,如果我們的項(xiàng)目由于一些原因不得不使用 users 之外的用戶表進(jìn)行認(rèn)證,那么就需要多做一點(diǎn)工作來(lái)完成這個(gè)功能。
現(xiàn)在假設(shè)我們只需要修改登錄用戶的表,表名和表結(jié)構(gòu)都與框架默認(rèn)的表users不同,文檔沒(méi)有教我們?nèi)绾稳プ觯莿e慌,稍微看下框架實(shí)現(xiàn)用戶認(rèn)證的源碼就能輕松實(shí)現(xiàn)。
首先,自定義一張表用來(lái)登錄,表結(jié)構(gòu)和模擬數(shù)據(jù)如下:
表 admins
從配置文件入手
用戶認(rèn)證相關(guān)的配置都保存在config/auth.php文件中,先來(lái)看看配置文件的內(nèi)容:
<?php return [ /* |-------------------------------------------------------------------------- | Authentication Defaults |-------------------------------------------------------------------------- | | This option controls the default authentication "guard" and password | reset options for your application. You may change these defaults | as required, but they're a perfect start for most applications. | */ 'defaults' => [ 'guard' => 'web', 'passwords' => 'users', ], /* |-------------------------------------------------------------------------- | Authentication Guards |-------------------------------------------------------------------------- | | Next, you may define every authentication guard for your application. | Of course, a great default configuration has been defined for you | here which uses session storage and the Eloquent user provider. | | All authentication drivers have a user provider. This defines how the | users are actually retrieved out of your database or other storage | mechanisms used by this application to persist your user's data. | | Supported: "session", "token" | */ 'guards' => [ 'web' => [ 'driver' => 'session', 'provider' => 'users', ], 'api' => [ 'driver' => 'passport', 'provider' => 'users', ], ], /* |-------------------------------------------------------------------------- | User Providers |-------------------------------------------------------------------------- | | All authentication drivers have a user provider. This defines how the | users are actually retrieved out of your database or other storage | mechanisms used by this application to persist your user's data. | | If you have multiple user tables or models you may configure multiple | sources which represent each model / table. These sources may then | be assigned to any extra authentication guards you have defined. | | Supported: "database", "eloquent" | */ 'providers' => [ 'users' => [ 'driver' => 'eloquent', 'model' => AppUser::class, ], // 'users' => [ // 'driver' => 'database', // 'table' => 'users', // ], ], /* |-------------------------------------------------------------------------- | Resetting Passwords |-------------------------------------------------------------------------- | | You may specify multiple password reset configurations if you have more | than one user table or model in the application and you want to have | separate password reset settings based on the specific user types. | | The expire time is the number of minutes that the reset token should be | considered valid. This security feature keeps tokens short-lived so | they have less time to be guessed. You may change this as needed. | */ 'passwords' => [ 'users' => [ 'provider' => 'users', 'table' => 'password_resets', 'expire' => 60, ], ], ];
默認(rèn)使用的守衛(wèi)是web,而web守衛(wèi)使用的認(rèn)證驅(qū)動(dòng)是session,用戶提供器是users。假設(shè)我們的需求只是將用戶的提供器由users改為admins,那么我們需要做兩步操作:
修改默認(rèn)的用戶提供器,將provider=>'users'改為provider=>'admins'
'guards' => [ 'web' => [ 'driver' => 'session', 'provider' => 'users', ], ],
配置admins提供器,假設(shè)依舊使用eloquent作為驅(qū)動(dòng),并創(chuàng)建好了admins表的模型
'providers' => [ 'admins' => [ 'driver' => 'eloquent', 'model' => AppAdmin::class ] ],
使用Auth門面的attempt方法進(jìn)行登錄
SessionGuard 中的attempt方法:
//IlluminateAuthSessionGuard public function attempt(array $credentials = [], $remember = false) { $this->fireAttemptEvent($credentials, $remember); $this->lastAttempted = $user = $this->provider->retrieveByCredentials($credentials); // If an implementation of UserInterface was returned, we'll ask the provider // to validate the user against the given credentials, and if they are in // fact valid we'll log the users into the application and return true. if ($this->hasValidCredentials($user, $credentials)) { $this->login($user, $remember); return true; } // If the authentication attempt fails we will fire an event so that the user // may be notified of any suspicious attempts to access their account from // an unrecognized user. A developer may listen to this event as needed. $this->fireFailedEvent($user, $credentials); return false; }
該方法中調(diào)用 UserProvider 接口的retrieveByCredentials方法檢索用戶,根據(jù)我們的配置,UserProvider接口的具體實(shí)現(xiàn)應(yīng)該是EloquentUserProvider,因此,我們定位到EloquentUserProvider的retrieveByCredentials方法:
//IlluminateAuthEloquentUserProvider public function retrieveByCredentials(array $credentials) { if (empty($credentials) || (count($credentials) === 1 && array_key_exists('password', $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')) { continue; } if (is_array($value) || $value instanceof Arrayable) { $query->whereIn($key, $value); } else { $query->where($key, $value); } } return $query->first(); }
該方法會(huì)使用傳入的參數(shù)(不包含password)到我們配置的數(shù)據(jù)表中搜索數(shù)據(jù),查詢到符合條件的數(shù)據(jù)之后返回對(duì)應(yīng)的用戶信息,然后attempt方法會(huì)進(jìn)行密碼校驗(yàn),校驗(yàn)密碼的方法為:
//IlluminateAuthSessionGuard /** * Determine if the user matches the credentials. * * @param mixed $user * @param array $credentials * @return bool */ protected function hasValidCredentials($user, $credentials) { return ! is_null($user) && $this->provider->validateCredentials($user, $credentials); }
進(jìn)一步查看EloquentUserProvider中的validateCredentials方法
//IlluminateAuthEloquentUserProvider public function validateCredentials(UserContract $user, array $credentials) { $plain = $credentials['password']; return $this->hasher->check($plain, $user->getAuthPassword()); }
通過(guò)validateCredentials可以看出,提交的認(rèn)證數(shù)據(jù)中密碼字段名必須是password,這個(gè)無(wú)法自定義。同時(shí)可以看到,入?yún)?user必須實(shí)現(xiàn)IlluminateContractsAuthAuthenticatable接口(UserContract是別名)。
修改 Admin 模型
Admin模型必須實(shí)現(xiàn)IlluminateContractsAuthAuthenticatable接口,可以借鑒一下User模型,讓Admin直接繼承IlluminateFoundationAuthUser 就可以,然后重寫getAuthPassword方法,正確獲取密碼字段:
// AppAdmin public function getAuthPassword() { return $this->login_pass; }
不出意外的話,這個(gè)時(shí)候就能使用admins表進(jìn)行登錄了。
Larval 5.4的默認(rèn)Auth登陸傳入郵件和用戶密碼到attempt 方法來(lái)認(rèn)證,通過(guò)email 的值獲取,如果用戶被找到,經(jīng)哈希運(yùn)算后存儲(chǔ)在數(shù)據(jù)中的password將會(huì)和傳遞過(guò)來(lái)的經(jīng)哈希運(yùn)算處理的passwrod值進(jìn)行比較。如果兩個(gè)經(jīng)哈希運(yùn)算的密碼相匹配那么將會(huì)為這個(gè)用戶開(kāi)啟一個(gè)認(rèn)證Session。
參考上面的分析,我們就需要對(duì)EloquentUserProvider中的validateCredentials方法進(jìn)行重寫,步驟如下
1. 修改 AppModelsUser.php 添加如下代碼
public function getAuthPassword() { return ['password' => $this->attributes['password'], 'salt' => $this->attributes['salt']]; }
2. 建立一個(gè)自己的UserProvider.php 的實(shí)現(xiàn)
<?php namespace AppFoundationAuth; use IlluminateAuthEloquentUserProvider; use IlluminateContractsAuthAuthenticatable; use IlluminateSupportStr; /** * 重寫用戶密碼校驗(yàn)邏輯 * Class GfzxEloquentUserProvider * @package AppFoundationAuth */ class GfzxEloquentUserProvider extends EloquentUserProvider { /** * Validate a user against the given credentials. * * @param IlluminateContractsAuthAuthenticatable $user * @param array $credentials * @return bool */ 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 AppProviders; use AppFoundationAuthGfzxEloquentUserProvider; use Auth; use IlluminateSupportFacadesGate; use IlluminateFoundationSupportProvidersAuthServiceProvider as ServiceProvider; class AuthServiceProvider extends ServiceProvider { . . . /** * Register any authentication / authorization services. * * @return void */ 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' => AppModelsUser::class, ], ],
這是就可以用過(guò)salt+passwrod的方式密碼認(rèn)證了
文章參考
laravel 修改用戶模塊密碼驗(yàn)證
Laravel 中自定義用戶登錄的數(shù)據(jù)表
到此這篇關(guān)于laravel修改用戶模塊的密碼驗(yàn)證實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)laravel修改用戶模塊的密碼驗(yàn)證內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
版權(quán)聲明:
本站所有文章和圖片均來(lái)自用戶分享和網(wǎng)絡(luò)收集,文章和圖片版權(quán)歸原作者及原出處所有,僅供學(xué)習(xí)與參考,請(qǐng)勿用于商業(yè)用途,如果損害了您的權(quán)利,請(qǐng)聯(lián)系網(wǎng)站客服處理。