




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
第laravel修改用戶模塊的密碼驗證實現|--------------------------------------------------------------------------
|AuthenticationDefaults
|--------------------------------------------------------------------------
|Thisoptioncontrolsthedefaultauthentication"guard"andpassword
|resetoptionsforyourapplication.Youmaychangethesedefaults
|asrequired,butthey'reaperfectstartformostapplications.
'defaults'=[
'guard'='web',
'passwords'='users',
|--------------------------------------------------------------------------
|AuthenticationGuards
|--------------------------------------------------------------------------
|Next,youmaydefineeveryauthenticationguardforyourapplication.
|Ofcourse,agreatdefaultconfigurationhasbeendefinedforyou
|herewhichusessessionstorageandtheEloquentuserprovider.
|Allauthenticationdrivershaveauserprovider.Thisdefineshowthe
|usersareactuallyretrievedoutofyourdatabaseorotherstorage
|mechanismsusedbythisapplicationtopersistyouruser'sdata.
|Supported:"session","token"
'guards'=[
'web'=[
'driver'='session',
'provider'='users',
'api'=[
'driver'='passport',
'provider'='users',
|--------------------------------------------------------------------------
|UserProviders
|--------------------------------------------------------------------------
|Allauthenticationdrivershaveauserprovider.Thisdefineshowthe
|usersareactuallyretrievedoutofyourdatabaseorotherstorage
|mechanismsusedbythisapplicationtopersistyouruser'sdata.
|Ifyouhavemultipleusertablesormodelsyoumayconfiguremultiple
|sourceswhichrepresenteachmodel/table.Thesesourcesmaythen
|beassignedtoanyextraauthenticationguardsyouhavedefined.
|Supported:"database","eloquent"
'providers'=[
'users'=[
'driver'='eloquent',
'model'=App\User::class,
//'users'=[
//'driver'='database',
//'table'='users',
//],
|--------------------------------------------------------------------------
|ResettingPasswords
|--------------------------------------------------------------------------
|Youmayspecifymultiplepasswordresetconfigurationsifyouhavemore
|thanoneusertableormodelintheapplicationandyouwanttohave
|separatepasswordresetsettingsbasedonthespecificusertypes.
|Theexpiretimeisthenumberofminutesthattheresettokenshouldbe
|consideredvalid.Thissecurityfeaturekeepstokensshort-livedso
|theyhavelesstimetobeguessed.Youmaychangethisasneeded.
'passwords'=[
'users'=[
'provider'='users',
'table'='password_resets',
'expire'=60,
默認使用的守衛(wèi)是web,而web守衛(wèi)使用的認證驅動是session,用戶提供器是users。假設我們的需求只是將用戶的提供器由users改為admins,那么我們需要做兩步操作:
修改默認的用戶提供器,將provider='users'改為provider='admins'
'guards'=[
'web'=[
'driver'='session',
'provider'='users',
配置admins提供器,假設依舊使用eloquent作為驅動,并創(chuàng)建好了admins表的模型
'providers'=[
'admins'=[
'driver'='eloquent',
'model'=App\Admin::class
使用Auth門面的attempt方法進行登錄
SessionGuard中的attempt方法:
//Illuminate\Auth\SessionGuard
publicfunctionattempt(array$credentials=[],$remember=false)
$this-fireAttemptEvent($credentials,$remember);
$this-lastAttempted=$user=$this-provider-retrieveByCredentials($credentials);
//IfanimplementationofUserInterfacewasreturned,we'llasktheprovider
//tovalidatetheuseragainstthegivencredentials,andiftheyarein
//factvalidwe'lllogtheusersintotheapplicationandreturntrue.
if($this-hasValidCredentials($user,$credentials)){
$this-login($user,$remember);
returntrue;
//Iftheauthenticationattemptfailswewillfireaneventsothattheuser
//maybenotifiedofanysuspiciousattemptstoaccesstheiraccountfrom
//anunrecognizeduser.Adevelopermaylistentothiseventasneeded.
$this-fireFailedEvent($user,$credentials);
returnfalse;
該方法中調用UserProvider接口的retrieveByCredentials方法檢索用戶,根據我們的配置,UserProvider接口的具體實現應該是EloquentUserProvider,因此,我們定位到EloquentUserProvider的retrieveByCredentials方法:
//Illuminate\Auth\EloquentUserProvider
publicfunctionretrieveByCredentials(array$credentials)
if(empty($credentials)||
(count($credentials)===1
array_key_exists('password',$credentials))){
return;
//Firstwewilladdeachcredentialelementtothequeryasawhereclause.
//Thenwecanexecutethequeryand,ifwefoundauser,returnitina
//EloquentUser"model"thatwillbeutilizedbytheGuardinstances.
$query=$this-createModel()-newQuery();
foreach($credentialsas$key=$value){
if(Str::contains($key,'password')){
continue;
if(is_array($value)||$valueinstanceofArrayable){
$query-whereIn($key,$value);
}else{
$query-where($key,$value);
return$query-first();
}
該方法會使用傳入的參數(不包含password)到我們配置的數據表中搜索數據,查詢到符合條件的數據之后返回對應的用戶信息,然后attempt方法會進行密碼校驗,校驗密碼的方法為:
//Illuminate\Auth\SessionGuard
*Determineiftheusermatchesthecredentials.
*@parammixed$user
*@paramarray$credentials
*@returnbool
protectedfunctionhasValidCredentials($user,$credentials)
return!is_null($user)$this-provider-validateCredentials($user,$credentials);
進一步查看EloquentUserProvider中的validateCredentials方法
//Illuminate\Auth\EloquentUserProvider
publicfunctionvalidateCredentials(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方法,正確獲取密碼字段:
//App\Admin
publicfunctiongetAuthPassword()
return$this-login_pass;
不出意外的話,這個時候就能使用admins表進行登錄了。
Larval5.4的默認Auth登陸傳入郵件和用戶密碼到attempt方法來認證,通過email的值獲取,如果用戶被找到,經哈希運算后存儲在數據中的password將會和傳遞過來的經哈希運算處理的passwrod值進行比較。如果兩個經哈希運算的密碼相匹配那么將會為這個用戶開啟一個認證Session。
參考上面的分析,我們就需要對EloquentUserProvider中的validateCredentials方法進行重寫,步驟如下
1.修改App\Models\User.php添加如下代碼
publicfunctiongetAuthPassword()
return['password'=$this-attributes['password'],'salt'=$this-attributes['salt']];
2.建立一個自己的UserProvider.php的實現
php
namespaceApp\Foundation\Auth;
useIlluminate\Auth\EloquentUserProvider;
useIlluminate\Contracts\Auth\Authenticatable;
useIlluminate\Support\Str;
*重寫用戶密碼校驗邏輯
*ClassGfzxEloquentUserProvider
*@packageApp\Foundation\Auth
classGfzxEloquentUserProviderextendsEloquentUserProvider
*Validateauseragainstthegivencredentials.
*@param\Illuminate\Contracts\Auth\Authenticatable$user
*@paramarray$credentials
*@returnbool
publicfunctionvalidateCredentials(Authenticatable$user,array$credentials)
$plain=$credentials['password'];
$authPassword=$user-getAuthPassword();
returnmd5($plain.$authPassword['salt'])==$authPassword['password'];
3.將UserProviders換成我們自己的GfzxEloquentUserProvide
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 區(qū)塊鏈智能合約代碼安全檢測與合規(guī)性審查協(xié)議
- 《契訶夫《藝術品》課件》
- 直播間家電產品選品與供應鏈服務合作協(xié)議
- 綠色環(huán)保物流配送車隊委托專業(yè)經營管理協(xié)議
- 專屬定制型退休養(yǎng)老私人理財規(guī)劃書
- 老齡房產抵押權代理協(xié)議
- 縣域知識產權管理
- 《核心構件解析教程》課件
- 全科醫(yī)學師資培訓體系構建
- 《皮膚病臨床癥狀》課件
- 2024年揚州大學輔導員考試真題
- 2025年上半年廣州市海珠區(qū)海幢街道招考康園工療站工作人員易考易錯模擬試題(共500題)試卷后附參考答案
- 預設理論在人工智能中的應用-深度研究
- CNAS-CL01:2018 檢測和校準實驗室能力認可準則
- 工業(yè)機器人在建筑行業(yè)的應用考核試卷
- 人體發(fā)育學 第十章 嬰幼兒情緒情感的發(fā)育
- 文化交流及藝術展覽合作合同
- 中國產教融合行業(yè)市場發(fā)展現狀及前景趨勢與投資分析研究報告(2024-2030版)
- GB/T 29912-2024城市物流配送汽車選型技術要求
- 2025年能源集團所屬遼寧能源煤電產業(yè)股份有限公司招聘筆試參考題庫附帶答案詳解
- 人教版五年級數學下冊全套試卷附完整答案
評論
0/150
提交評論