【问题标题】:Using custom authentication on Laravel 6在 Laravel 6 上使用自定义身份验证
【发布时间】:2020-03-02 09:09:21
【问题描述】:

我想手动验证我公司的用户。问题是,我在 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() 方法自己手动设置会话?

谢谢。

【问题讨论】:

    标签: php laravel oracle authentication laravel-6.2


    【解决方案1】:

    如果您想手动验证用户身份,您可以轻松使用会话。有以下代码作为参考:

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

    【讨论】:

    • 看起来很有希望!几天来,我一直在考虑以通常的 php 方式手动使用 session,但是查看 laravel auth 中的基本代码让我想我会错过它的任何有用功能。你认为我需要警卫吗?顺便说一句,我将无法使用 Auth::check 之类的功能对吗?
    • 您是否打算为您的应用程序使用广播器或 API?
    • 我确实做到了。不是现在,但我们有一些当前的应用程序(将迁移到这个 laravel 基础),它们将从广播功能中受益匪浅。不过,API 也是必须的,因为我们在不同的平台上有多个其他应用程序。我希望有一个解决方法。
    • 我认为你应该等待另一个更好的答案:)
    • 很酷,谢谢。明天我会在办公室尝试你的方法并报告。目前看来,这似乎是我的唯一方法。
    【解决方案2】:

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

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

    【讨论】:

    • 听起来不错。但我在那里看到了 MongoDB 这个名字。甲骨文有这样的东西吗?在 Google 上似乎找不到。
    • 您可以在帖子中关注其他答案,但如果您希望拥有 Facebook 类型的会话(允许您登录),那么您应该在单独的表中添加会话。另外,请注意在特定日期或时间之后添加会话到期,这是一个很好的做法。
    猜你喜欢
    • 2015-02-23
    • 2020-06-17
    • 2014-03-30
    • 2016-04-18
    • 1970-01-01
    • 2020-03-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多