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

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


谢谢你。


守着星空守着你
浏览 76回答 2
2回答

慕容森

如果要手动验证用户身份,可以轻松使用会话。有以下代码作为参考://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.&nbsp; &nbsp; &nbsp;session()->put('user_id', user id from database here);如果要检查用户是否经过身份验证,请将RedirectIfAuthenticated中间件修改为:<?phpnamespace App\Http\Middleware;use App\Providers\RouteServiceProvider;use Closure;use Illuminate\Support\Facades\Auth;class RedirectIfAuthenticated{&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Handle an incoming request.&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;* @param&nbsp; \Illuminate\Http\Request&nbsp; $request&nbsp; &nbsp; &nbsp;* @param&nbsp; \Closure&nbsp; $next&nbsp; &nbsp; &nbsp;* @param&nbsp; string|null&nbsp; $guard&nbsp; &nbsp; &nbsp;* @return mixed&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public function handle($request, Closure $next, $guard = null)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if (session()->has('user_id')) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return redirect(&nbsp; custom path here );&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return $next($request);&nbsp; &nbsp; }}当您想注销用户时,只需销毁会话密钥session()->forget('user_id');**注意:** 许多广播和插件使用 Laravel 的身份验证系统(Guards),如果您想将它们与自定义身份验证系统一起使用,您可能需要挂钩他们的代码

慕雪6442864

Laravel 提供了自定义会话驱动程序,您可以使用它们来创建或删除会话<?phpnamespace App\Extensions;class MongoSessionHandler implements \SessionHandlerInterface{&nbsp; &nbsp; public function open($savePath, $sessionName) {}&nbsp; &nbsp; public function close() {}&nbsp; &nbsp; public function read($sessionId) {}&nbsp; &nbsp; public function write($sessionId, $data) {}&nbsp; &nbsp; public function destroy($sessionId) {}&nbsp; &nbsp; public function gc($lifetime) {}}希望对您有所帮助,如果没有,请在下方评论。会帮你的。###### 更新 #######我认为你必须从 Laravel 进行自定义 HTTP 会话第 1 步:在您的数据库中为会话创建另一个表,如下所示;Schema::create('sessions', function ($table) {&nbsp; &nbsp; $table->string('id')->unique();&nbsp; &nbsp; $table->unsignedInteger('user_id')->nullable();&nbsp; &nbsp; $table->string('ip_address', 45)->nullable();&nbsp; &nbsp; $table->text('user_agent')->nullable();&nbsp; &nbsp; $table->text('payload');&nbsp; &nbsp; $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 () {&nbsp; &nbsp; return 'default';});第 4 步:删除会话,一段时间后,出于安全原因,您需要删除会话,然后您可以这样做。$value = $request->session()->pull('key', 'default');
打开App,查看更多内容
随时随地看视频慕课网APP