在 Laravel 6 上使用自定义身份验证

Using custom authentication on Laravel 6

我想手动验证我公司的用户。问题是,我有 2 tables,在 Oracle 数据库中称为 Student 和 Staff。

至于学生 table,我想覆盖通过 auth 脚手架命令提供的内置 Auth 方法,因为用户名和密码直接存储在 table.

至于员工 table,密码存储在不同的 column/table 中并使用存储的 procedure/package 加密,因此获得用户验证的唯一方法是调用包只有 returns 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()方法手动设置session?

谢谢。

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 步:在您的数据库中为会话创建另一个 table,像这样;

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');
});

第二步:在session中存储数据,一般会使用put方法或者session helper;

// Via a request instance...
$request->session()->put('key', 'value');

// Via the global helper...
session(['key' => 'value']);

步骤 3:当您的函数 returns 1

时获取特定用户的密钥
$value = $request->session()->get('key', function () {
    return 'default';
});

第四步:删除session,过段时间为了安全需要删除session才可以。

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

如果你想手动验证用户,你可以很容易地使用会话。有以下代码作为参考:

//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),如果您想将它们与您的自定义身份验证系统一起使用,您可能需要连接到他们的代码中