国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - PHP教程 - laravel修改用戶模塊的密碼驗證實現

laravel修改用戶模塊的密碼驗證實現

2021-11-25 15:49if年少有為 PHP教程

本文主要介紹了laravel修改用戶模塊的密碼驗證實現,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

做項目的時候,用戶認證幾乎是必不可少的,如果我們的項目由于一些原因不得不使用 users 之外的用戶表進行認證,那么就需要多做一點工作來完成這個功能。

現在假設我們只需要修改登錄用戶的表,表名和表結構都與框架默認的表users不同,文檔沒有教我們如何去做,但是別慌,稍微看下框架實現用戶認證的源碼就能輕松實現。

首先,自定義一張表用來登錄,表結構和模擬數據如下:

表 admins

 

id login_name login_pass
1 admin 10$2MUhp7b6ghVOngb/.b/x6uuEW/yL3FqPKJztawrM0U577Clf07xda

 

從配置文件入手

用戶認證相關的配置都保存在config/auth.php文件中,先來看看配置文件的內容:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
    <?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' => App\User::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,
        ],
    ],
 
];

默認使用的守衛是web,而web守衛使用的認證驅動是session,用戶提供器是users。假設我們的需求只是將用戶的提供器由users改為admins,那么我們需要做兩步操作:

修改默認的用戶提供器,將provider=>'users'改為provider=>'admins'

?
1
2
3
4
5
6
'guards' => [
      'web' => [
          'driver' => 'session',
          'provider' => 'users',
      ],
  ],

配置admins提供器,假設依舊使用eloquent作為驅動,并創建好了admins表的模型

?
1
2
3
4
5
6
'providers' => [
        'admins' => [
            'driver' => 'eloquent',
            'model' => App\Admin::class
        ]
    ],

使用Auth門面的attempt方法進行登錄

SessionGuard 中的attempt方法:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//Illuminate\Auth\SessionGuard
 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;
    }

該方法中調用 UserProvider 接口的retrieveByCredentials方法檢索用戶,根據我們的配置,UserProvider接口的具體實現應該是EloquentUserProvider,因此,我們定位到EloquentUserProvider的retrieveByCredentials方法:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
//Illuminate\Auth\EloquentUserProvider
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();
    }

該方法會使用傳入的參數(不包含password)到我們配置的數據表中搜索數據,查詢到符合條件的數據之后返回對應的用戶信息,然后attempt方法會進行密碼校驗,校驗密碼的方法為:

?
1
2
3
4
5
6
7
8
9
10
11
12
//Illuminate\Auth\SessionGuard
/**
     * 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);
    }

進一步查看EloquentUserProvider中的validateCredentials方法

?
1
2
3
4
5
6
7
//Illuminate\Auth\EloquentUserProvider
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方法,正確獲取密碼字段:

?
1
2
3
4
5
// App\Admin
public function getAuthPassword()
{
    return $this->login_pass;
}

不出意外的話,這個時候就能使用admins表進行登錄了。

Larval 5.4的默認Auth登陸傳入郵件和用戶密碼到attempt 方法來認證,通過email 的值獲取,如果用戶被找到,經哈希運算后存儲在數據中的password將會和傳遞過來的經哈希運算處理的passwrod值進行比較。如果兩個經哈希運算的密碼相匹配那么將會為這個用戶開啟一個認證Session。

參考上面的分析,我們就需要對EloquentUserProvider中的validateCredentials方法進行重寫,步驟如下

1. 修改 App\Models\User.php 添加如下代碼

?
1
2
3
4
public function getAuthPassword()
    {
        return ['password' => $this->attributes['password'], 'salt' => $this->attributes['salt']];
    }

2. 建立一個自己的UserProvider.php 的實現

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<?php
namespace App\Foundation\Auth;
 
use Illuminate\Auth\EloquentUserProvider;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Support\Str;
 
/**
 * 重寫用戶密碼校驗邏輯
 * Class GfzxEloquentUserProvider
 * @package App\Foundation\Auth
 */
class GfzxEloquentUserProvider extends EloquentUserProvider
{
    /**
     * Validate a user against the given credentials.
     *
     * @param  \Illuminate\Contracts\Auth\Authenticatable $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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<?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
{
    .
    .
    .
 
    /**
     * 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

?
1
2
3
4
5
6
'providers' => [
     'users' => [
         'driver' => 'gfzx-eloquent',
         'model' => App\Models\User::class,
     ],
 ],

這是就可以用過salt+passwrod的方式密碼認證了

文章參考

laravel 修改用戶模塊密碼驗證

Laravel 中自定義用戶登錄的數據表

到此這篇關于laravel修改用戶模塊的密碼驗證實現的文章就介紹到這了,更多相關laravel修改用戶模塊的密碼驗證內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!

原文鏈接:https://www.cnblogs.com/ifme/p/11797159.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产电影一区二区三区 | 中文字幕一级毛片 | 亚洲精品一区 | 欧美.com | 欧美日韩亚洲一区 | 国产区在线观看 | 精品久久久久久久久久久久久久 | 国产一区二区免费 | 久久国产精品久久久久久电车 | 欧美1区2区3区 | 国产综合精品一区二区三区 | 成人看片免费 | 日韩中文视频 | 欧美亚洲日本 | 精品国产仑片一区二区三区 | 欧美成人精精品一区二区频 | 国产一区二区三区四区在线观看 | theporn国产在线精品 | 亚洲精品视频在线观看免费视频 | 91精品久久久久久久久久入口 | 亚洲一区二区中文字幕 | 色在线视频播放 | 亚洲一区二区免费看 | 一区二区三区av | 国产成人综合一区二区三区 | 青青草原综合久久大伊人精品 | 日本在线免费观看 | 中文字幕视频在线观看 | 精品久久久久久久久久久久 | 91丝袜| 亚洲综合中文字幕在线 | 91av视频在线 | 国产精品成人国产乱一区 | 一呦二呦三呦国产精品 | 久久久久久久久久久精 | 欧美一级在线观看 | 久久九精品 | 精品久久一区二区三区 | 日韩精品 电影一区 亚洲 | 免费在线一区二区 | 日韩不卡二区 |