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

时间:2017-07-02 10:55:41

标签: php authentication laravel-5 login-control

我想在laravel 5.4中为管理页面创建第二个身份验证。

首先让我来描述一下我的问题: 我有一个功能强大的用户登录(默认laravel auth)来自' web' -guard。现在我想为管理面板创建第二个身份验证。我有另一个表存储名称,一个令牌(类似于密码)和权限级别。

第二个/单独的表是系统为页面开发的依赖关系,因此我无法改变它。

我有管理面板的登录页面,但是当我尝试进行身份验证时,我会每次都重定向回登录。

我已经用谷歌搜索了整个事情并且遇到了一些很好的例子:

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

    • 其他链接在pastebin上的控制器粘贴(下面的链接)
  2. 但我无法弄明白。

    这是我已经做过的事情:

    • 添加了第二个后卫,名为' admin '在 config / auth.php

      '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,
          ]
      ],
      
    • 添加了所需的模型

      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';
          }
      }
      
    • routes / 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');
      
      });
      
    • app / Http / Middleware 下添加了一个新的中间件,名为' RedirectIfElevated '通过命令' php artisan make:middleware '

      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);
      }
      
    • 以及 Kernel.php

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

    • 并创建视图

      @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
      

    所以我需要回答的问题是:

    1. 我在哪里错过了什么?我在哪里陷入困境?
    2. 我希望你能帮我解决这个问题。谢谢你的帮助!

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

我使用以下自定义登录方法修复了该问题:

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