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

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

在 Laravel 6 上使用自定義身份驗證

在 Laravel 6 上使用自定義身份驗證

PHP
守著星空守著你 2022-10-14 14:56:56
我想手動驗證我公司的用戶。問題是,我在 Oracle 數據庫中有 2 個表,分別稱為 Student 和 Staff。至于 Student 表,我想到了覆蓋通過 auth 腳手架命令提供的內置 Auth 方法,因為用戶名和密碼直接存儲在表中。至于 Staff 表,密碼存儲在不同的列/表中,并使用存儲過程/包加密,因此獲得用戶驗證的唯一方法是調用僅返回 0 或 1 的包。我做了什么,我編寫了自己的路由,并在 LoginController 中添加了自己的函數。public function loginStaff(Request $req){    $username = Str::upper($req->input('username'));    $password = $req->input('password');    $users = PortalUser::where('ID', $username)->firstOrFail();    if ($users->user_type == 'STAFF'){       $queryResult = DB::select('select PACKAGE.validStaff(?,?) from dual',[$username, $password]);       if($queryResult == 1){              //this is where I would like to auth the user.              //using Auth::Attempt and Auth::Login will only run the default query       }}我已在控制器中成功返回值 1 和 0。那么有什么我想念的嗎?還是我應該使用 session() 方法自己手動設置會話?謝謝你。
查看完整描述

2 回答

?
慕容森

TA貢獻1853條經驗 獲得超18個贊

如果要手動驗證用戶身份,可以輕松使用會話。有以下代碼作為參考:


//this is where I would like to auth the user.

//using Auth::Attempt and Auth::Login will only run the default query


// typically you store it using the user ID, but you can modify the session using other values.     

session()->put('user_id', user id from database here);

如果要檢查用戶是否經過身份驗證,請將RedirectIfAuthenticated中間件修改為:


<?php


namespace App\Http\Middleware;


use App\Providers\RouteServiceProvider;

use Closure;

use Illuminate\Support\Facades\Auth;


class RedirectIfAuthenticated

{

    /**

     * Handle an incoming request.

     *

     * @param  \Illuminate\Http\Request  $request

     * @param  \Closure  $next

     * @param  string|null  $guard

     * @return mixed

     */

    public function handle($request, Closure $next, $guard = null)

    {

        if (session()->has('user_id')) {

            return redirect(  custom path here );

        }


        return $next($request);

    }

}

當您想注銷用戶時,只需銷毀會話密鑰


session()->forget('user_id');

**注意:** 許多廣播和插件使用 Laravel 的身份驗證系統(Guards),如果您想將它們與自定義身份驗證系統一起使用,您可能需要掛鉤他們的代碼


查看完整回答
反對 回復 2022-10-14
?
慕雪6442864

TA貢獻1812條經驗 獲得超5個贊

Laravel 提供了自定義會話驅動程序,您可以使用它們來創建或刪除會話


<?php


namespace App\Extensions;


class MongoSessionHandler implements \SessionHandlerInterface

{

    public function open($savePath, $sessionName) {}

    public function close() {}

    public function read($sessionId) {}

    public function write($sessionId, $data) {}

    public function destroy($sessionId) {}

    public function gc($lifetime) {}

}

希望對您有所幫助,如果沒有,請在下方評論。會幫你的。


###### 更新 #######


我認為你必須從 Laravel 進行自定義 HTTP 會話


第 1 步:在您的數據庫中為會話創建另一個表,如下所示;


Schema::create('sessions', function ($table) {

    $table->string('id')->unique();

    $table->unsignedInteger('user_id')->nullable();

    $table->string('ip_address', 45)->nullable();

    $table->text('user_agent')->nullable();

    $table->text('payload');

    $table->integer('last_activity');

});

Step 2 : 在會話中存儲數據,你通常會使用put方法或session助手;


// Via a request instance...

$request->session()->put('key', 'value');


// Via the global helper...

session(['key' => 'value']);

第 3 步:當您的函數返回 1 時獲取特定用戶的密鑰


$value = $request->session()->get('key', function () {

    return 'default';

});

第 4 步:刪除會話,一段時間后,出于安全原因,您需要刪除會話,然后您可以這樣做。


$value = $request->session()->pull('key', 'default');


查看完整回答
反對 回復 2022-10-14
  • 2 回答
  • 0 關注
  • 109 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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