laravel修改用戶模塊的密碼驗證實現_第1頁
laravel修改用戶模塊的密碼驗證實現_第2頁
laravel修改用戶模塊的密碼驗證實現_第3頁
laravel修改用戶模塊的密碼驗證實現_第4頁
laravel修改用戶模塊的密碼驗證實現_第5頁
已閱讀5頁,還剩5頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

第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. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論