【问题标题】:laravel 5.4, adding second authentication (admin panel)laravel 5.4,添加二次认证(管理面板)
【发布时间】:2017-07-02 10:55:40
【问题描述】:

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


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

第二个/单独的表是开发页面的系统给定的依赖项,所以我无法更改它。

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


我已经用谷歌搜索了整件事,并找到了一些很好的例子:

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

    • 其他链接在控制器中粘贴到 pastebin(链接在下面)

但我无法弄清楚。


这是我已经做过的:

  • config/auth.php

    中添加了第二个名为“admin”的 guard
    '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

    中添加了必要的routes
    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');
    
    });
    
  • 通过命令 'php artisan make:middleware' 在 app/Http/Middleware 下添加了一个名为 'RedirectIfElevated' 的新 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. 我在哪里错过了什么?我在哪里搞砸了?

希望您能帮我解决这个问题,感谢您的帮助!

【问题讨论】:

    标签: php authentication laravel-5 login-control


    【解决方案1】:

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

    【讨论】:

    • 这是最常见的方式。我在网上多次看到它。但我必须使用第二个表,因为它为另一个我无法更改的应用程序存储必要的数据。我还考虑过编写自己的登录类,但即便如此我也不知道如何将数据从数据库传递到身份验证守卫/会话。对此有何建议?
    【解决方案2】:

    我使用以下自定义登录方法解决了这个问题:

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

    【讨论】:

      猜你喜欢
      • 2015-06-20
      • 2018-01-13
      • 1970-01-01
      • 2016-06-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-19
      相关资源
      最近更新 更多