亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Laravel 7 身份驗證嘗試

Laravel 7 身份驗證嘗試

PHP
一只甜甜圈 2023-08-26 17:32:10
根據 Laravel 7.x 文檔,我嘗試為我的應用程序創建手動身份驗證。文檔顯示如下:<?phpnamespace App\Http\Controllers;use Illuminate\Http\Request;use Illuminate\Support\Facades\Auth;class LoginController extends Controller{    public function authenticate(Request $request)    {        $credentials = $request->only('email', 'password');        if (Auth::attempt($credentials)) {            // Authentication passed...            return redirect()->intended('dashboard');        }    }}我有自己的用戶表:sys_users==========user_acc character varying(20) NOT NULL, -- Primary Keyfull_name character varying(300) NOT NULL,is_suspended boolean DEFAULT FALSE,CONSTRAINT PK_SysUsers PRIMARY KEY (user_acc),要登錄,用戶需要輸入以下數據:用戶名密碼訪問模塊我嘗試對控制器進行一些自定義(尚未完成):class LoginController extends Controller{    public function authenticate(Request $request)    {        $request->validate([            'username' => 'required',            'password' => 'required',        ]);                $credentials = $request->only('username', 'password', 'module');        if (Auth::attempt($credentials)) {            return redirect()->route($request->module);        }    }}我想在上面的自定義代碼中添加的是:(i)通過查詢表來檢查用戶名是否存在于表中sys_users,(ii)使用POP3檢查密碼(我已經準備好了phpmailer庫和代碼),以及( iii)通過查詢我準備的另一個表來檢查用戶是否有權訪問這些模塊。問題是:我不知道將所有代碼放在哪里。如果可能的話,我想將它們放在Auth類的attempt方法中,但我似乎找不到所謂的方法。文檔非常缺乏,沒有提供詳細的解釋。如果不知何故無法修改類attempt中的方法Auth,我應該如何進行身份驗證過程?
查看完整描述

1 回答

?
jeck貓

TA貢獻1909條經驗 獲得超7個贊

您想要做的是實現您自己的用戶提供程序,以使用外部 POP3 電子郵件系統驗證您的用戶憑據。

我不認為您需要自定義您的 Guard,因為我假設您仍然希望StatefulGuard默認的SessionGuardGuard 會檢查并存儲有關會話中身份驗證狀態的信息。

我認為除了如何驗證所提供的憑據之外,您可能還需要所有其他默認行為。

也許./app/Providers/您可以在文件夾中創建:

PopThreeUserProvider.php

namespace App\Providers;


use Illuminate\Auth\EloquentUserProvider;

use Illuminate\Contracts\Auth\Authenticatable as UserContract;


class PopThreeUserProvider extends EloquentUserProvider

{

? ? /**

? ? ?* Validate a user against the given credentials.

? ? ?*

? ? ?* @param? \Illuminate\Contracts\Auth\Authenticatable? $user

? ? ?* @param? array? $credentials

? ? ?* @return bool

? ? ?*/

? ? public function validateCredentials(UserContract $user, array $credentials)

? ? {

? ? ? ? // Implement your pop3 authentication here, I have left the default code

? ? ? ? // for the Eloquent provider below

? ? ? ? $plain = $credentials['password'];


? ? ? ? return $this->hasher->check($plain, $user->getAuthPassword());

? ? }

}

現在在你的./app/Providers/AuthServiceProvider.php


class AuthServiceProvider extends ServiceProvider

{

? ? ...


? ? /**

? ? ?* Register any application authentication / authorization services.

? ? ?*

? ? ?* @return void

? ? ?*/

? ? public function boot()

? ? {

? ? ? ? ...


? ? ? ? Auth::provider('pop3', function ($app, array $config) {

? ? ? ? ? ? return new PopThreeUserProvider($app->make('hash'), $config['model']);

? ? ? ? });


? ? ? ? ...

? ? }


? ? ...

}

現在在你的config/auth.php:


? ? ...


? ? 'providers' => [

? ? ? ? 'users' => [

? ? ? ? ? ? 'driver' => 'pop3',

? ? ? ? ],

? ? ],


? ? ...

當然,這一切都假設您擁有:


class User extends Authenticatable

{

? ? protected $table = 'sys_users';

? ? protected $primaryKey = 'user_acc';

? ? protected $incrementing = false;

? ? ...


查看完整回答
反對 回復 2023-08-26
  • 1 回答
  • 0 關注
  • 140 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號