为了账号安全,请及时绑定邮箱和手机立即绑定

在 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 关注
  • 75 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信