简体   繁体   English

laravel 5.4,添加第二个身份验证(管理面板)

[英]laravel 5.4, adding second authentication (admin panel)

i want to create a second authentication in laravel 5.4 for an administration page. 我想在laravel 5.4中为管理页面创建第二个身份验证。


First of all let me describe my problem: I have a functionable user login (default laravel auth) via 'web'-guard. 首先,让我描述一下我的问题: 我通过'web'-guard进行了有效的用户登录(默认为laravel auth)。 Now i want to create a second authentication for the admin panel. 现在,我想为管理面板创建第二个身份验证。 I have another table which is storing the name, a token (which is something like a password) and an authority level. 我还有另一个表,用于存储名称,令牌(类似于密码)和权限级别。

The second/separate table is a dependency given by the system the page is developed for so i can't change that. 第二个/单独的表是页面开发的系统给定的依赖项,因此我无法更改。

I have the login page for the administration panel but when i try to authenticate i get redirected back to the login everytime. 我有管理面板的登录页面,但是当我尝试进行身份验证时,每次都会重定向回到登录名。


I already googled the whole thing and came across some good examples: 我已经搜索了整件事,并遇到了一些很好的例子:

  1. https://jamesmcfadden.co.uk/custom-authentication-in-laravel-with-guards-and-user-service-providers https://jamesmcfadden.co.uk/custom-authentication-in-laravel-with-guards-and-user-service-providers

    • other links are in the controller paste on pastebin (link down below) 其他链接在控制器中粘贴在pastebin上(下面的链接向下)

But i wasn't able to figure it out. 但是我无法弄清楚。


Here's what i did already: 这是我已经做过的:

  • Added a second guard named ' admin ' in config/auth.php config / auth.php中添加了名为“ admin ”的第二个防护

     'guards' => [ 'web' => [ 'driver' => 'session', 'provider' => 'users', ], 'admin' => [ 'driver' => 'session', 'provider' => 'admin', ] ], 'providers' => [ 'users' => [ 'driver' => 'eloquent', 'model' => App\\User::class, ], 'admin' => [ 'driver' => 'eloquent', 'model' => App\\Admin::class, ] ], 
  • Added the needed model 添加了所需的模型

     namespace App; use Illuminate\\Notifications\\Notifiable; use Illuminate\\Foundation\\Auth\\User as Authenticatable; class Admin extends Authenticatable { use Notifiable; protected $fillable = [ 'mID', 'mAccount', 'mName', 'mServerIP', 'mAuthority', 'mToken' ]; protected $hidden = [ 'mContactIP', 'mToken' ]; protected $table = 'administration'; protected $connection = 'common'; public $timestamps = false; public function getAuthIdentifierName() { return 'mAccount'; } } 
  • Added necessary routes in routes/web.php route / web.php中添加了必要的路由

     Route::group(['prefix' => 'admin'], function () { Route::get('/login','Auth\\ElevationController@showLoginForm')->middleware('web'); Route::post('/login','Auth\\ElevationController@elevate'); Route::get('/logout','Auth\\ElevationController@demote'); Route::get('/', function (){return redirect('admin/dashboard');}); Route::get('/dashboard', 'AdminController@index'); }); 
  • Added a new middleware under app/Http/Middleware named ' RedirectIfElevated ' via the command ' php artisan make:middleware ' 增加了一个新的中间件 应用程序/ HTTP /中间件命名通过命令“RedirectIfElevated“PHP的工匠制作:中间件

     public function handle($request, Closure $next, $guard = 'admin') { if (!Auth::guard($guard)->check()) { if(!Auth::guard('web')->check()) { return redirect('/'); } return redirect('/admin/login'); } return $next($request); } 
  • and in Kernel.php 并在Kernel.php中

     protected $routeMiddleware = [ . . . 'admin' => \\WarShape\\Http\\Middleware\\RedirectIfElevated::class, ]; 
  • finally i created my Controller : https://pastebin.com/s6iJgFAB 最后我创建了我的控制器https : //pastebin.com/s6iJgFAB

  • and created the view 并创建了视图

     @extends('layouts.app') @section('content') <div class="container"> <div class="row"> <div class="col-md-8 col-md-offset-2"> <div class="panel panel-default"> <div class="panel-heading">Elevation</div> <div class="panel-body"> <form class="form-horizontal" role="form" method="POST" action="{{ url('/admin/login') }}"> {{ csrf_field() }} <div class="form-group{{ $errors->has('password') ? ' has-error' : '' }}"> <label for="mToken" class="col-md-4 control-label">Token</label> <div class="col-md-6"> <input id="mToken" type="password" class="form-control" name="mToken" required> @if ($errors->has('password')) <span class="help-block"> <strong>{{ $errors->first('password') }}</strong> </span> @endif </div> </div> <div class="form-group{{ $errors->has('g-recaptcha-response') ? ' has-error' : '' }}"> <label for="recaptcha" class="col-md-4 control-label">Solve Captcha <br> & Elevate!</label> <div class="col-md-6"> {!! app('captcha')->display($attributes = [], $lang = app()->getLocale()) !!} @if ($errors->has('g-recaptcha-response')) <span class="help-block"> <strong>{{ $errors->first('g-recaptcha-response') }}</strong> </span> @endif </div> </div> <input type="hidden" name="mAccount" value="{{ Auth::guard('web')->user()->login }}"> <div class="form-group"> <div class="col-md-8 col-md-offset-4"> <button type="submit" class="btn btn-primary"> Elevate </button> </div> </div> </form> </div> </div> </div> </div> </div> @endsection 

So the question i need an answer to is: 因此,我需要回答的问题是:

  1. Where did i miss something? 我在哪里错过了什么? Where did i mess up? 我在哪里弄糟?

I hope you can help me with this & thanks for your help! 希望您能为此提供帮助,并感谢您的帮助!

很抱歉,如果我没有回答您的问题,但是您不能在用户表中添加一个简单的列,例如is_admin并仅授权is_admin = 1用户使用中间件访问管理面板,而不是两次登录?

I fixed that with the following custom login method: 我使用以下自定义登录方法修复了该问题:

public function elevate(Request $request)
{
    // login
    $this->validateLogin($request);
    $admin = Admin::where('mAccount', '=', Auth::guard('web')->user()->login)
       ->where('mToken', '=', $request->input('mToken'))->first();
    if($admin){
       Auth::guard('admin')->login($admin);
        return redirect('/admin/dashboard');
    }
    else{
        throw new \ErrorException('Elevation failed!');
    }
}

protected function validateLogin(Request $request)
{
    $this->validate($request, [
        'mToken' => 'required|string|min:8',
        'g-recaptcha-response' => 'required|captcha'
    ]);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM