如何在Laravel 5.2中使用多重身份验证

如何在Laravel 5.2中使用多重身份验证,第1张

如何在Laravel 5.2中使用多重身份验证

您需要两个表,

users
admins
在命令后运行Run命令以创建内置身份验证

php artisan make:auth

两种模型:Users(已存在)和Admin

<?phpnamespace App;use IlluminateFoundationAuthUser as Authenticatable;class Admin extends Authenticatable{}

现在打开config / auth.php并进行以下更改

'guards' => [        'web' => [ 'driver' => 'session', 'provider' => 'users',        ],        'admin' => [ 'driver' => 'session', 'provider' => 'admins',        ],    ],'providers' => [        'users' => [ 'driver' => 'eloquent', 'model' => AppUser::class,        ],         'admins' => [  'driver' => 'eloquent',  'model' => AppAdmin::class,         ],    ],'passwords' => [        'users' => [ 'provider' => 'users', 'email' => 'auth.emails.password', 'table' => 'password_resets', 'expire' => 60,        ],        'admins' => [ 'provider' => 'admins', 'email' => 'auth.emails.password', 'table' => 'password_resets', 'expire' => 60,        ],    ],

创建一个新的中间件

RedirectIfNotAdmin

<?phpnamespace AppHttpMiddleware;use Closure;use IlluminateSupportFacadesAuth;class RedirectIfNotAdmin{        public function handle($request, Closure $next, $guard = 'admin')    {        if (!Auth::guard($guard)->check()) { return redirect('/admin/login');        }        return $next($request);    }}

Kernel.php中的更改

protected $middleware = [        IlluminateFoundationHttpMiddlewareCheckForMaintenanceMode::class,        IlluminateSessionMiddlewareStartSession::class,        IlluminateViewMiddlewareShareErrorsFromSession::class,    ];protected $middlewareGroups = [        'web' => [ AppHttpMiddlewareEncryptcookies::class, IlluminatecookieMiddlewareAddQueuedcookiesToResponse::class, //IlluminateSessionMiddlewareStartSession::class, //IlluminateViewMiddlewareShareErrorsFromSession::class, AppHttpMiddlewareVerifyCsrfToken::class,        ],        'api' => [ 'throttle:60,1',        ],    ];protected $routeMiddleware = [        'auth' => AppHttpMiddlewareAuthenticate::class,        'auth.basic' => IlluminateAuthMiddlewareAuthenticateWithBasicAuth::class,        'guest' => AppHttpMiddlewareRedirectIfAuthenticated::class,        'throttle' => IlluminateRoutingMiddlewareThrottleRequests::class,        'admin' => AppHttpMiddlewareRedirectIfNotAdmin::class,    ];

创建一个新文件夹Http / Controller / Adminauth并从Http / Controller / Auth文件夹复制文件

打开文件Http / Controller / Adminauth / AuthController.php并进行以下更改

<?phpnamespace AppHttpControllersAdminauth;use AppAdmin;use Validator;use AppHttpControllersController;use IlluminateFoundationAuthThrottlesLogins;use IlluminateFoundationAuthAuthenticatesAndRegistersUsers;use Auth;class AuthController extends Controller{    use AuthenticatesAndRegistersUsers, ThrottlesLogins;    protected $redirectTo = '/admin';    protected $guard = 'admin';    public function showLoginForm()    {        if (Auth::guard('admin')->check())        { return redirect('/admin');        }        return view('admin.auth.login');    }    public function showRegistrationForm()    {        return view('admin.auth.register');    }    public function resetPassword()    {        return view('admin.auth.passwords.email');    }    public function logout(){        Auth::guard('admin')->logout();        return redirect('/admin/login');    }}

创建新文件夹Http / Controller / admin,从Http / Controller /复制该文件夹中的Controller.php文件

创建新文件Http / Controller / admin / employee.php

<?phpnamespace AppHttpControllersadmin;use IlluminateHttpRequest;use IlluminateRoutingController;use Auth;use AppAdmin;class Employee extends Controller{    public function __construct(){        $this->middleware('admin');   }    public function index(){        return view('admin.home');    }}

移至资源/视图创建新的文件夹资源/视图/管理员副本

resources/views/auth, resources/views/layouts &resources/views/home.blade.php

并发布

resources/views/admin
并打开admin文件夹中的每个文件,并在每个路径之前添加admin,现在该路径应如下所示

@extends('admin.layouts.app')

和您的Http / routes.php看起来像

<?phpRoute::get('/', function () {    return view('welcome');});Route::get('/admin/login','AdminauthAuthController@showLoginForm');Route::post('/admin/login','AdminauthAuthController@login');Route::get('/admin/password/reset','AdminauthPasswordController@resetPassword');Route::group(['middleware' => ['admin']], function () {    //Login Routes...    Route::get('/admin/logout','AdminauthAuthController@logout');    // Registration Routes...    Route::get('admin/register', 'AdminauthAuthController@showRegistrationForm');    Route::post('admin/register', 'AdminauthAuthController@register');    Route::get('/admin', 'AdminEmployee@index');});Route::group(['middleware' => 'web'], function () {    Route::auth();    Route::get('/home', 'HomeController@index');});

多数民众赞成在浏览器中打开您的网站,并检查并为管理员yoursiteurl / admin

请享用....



欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5441425.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-11
下一篇 2022-12-11

发表评论

登录后才能评论

评论列表(0条)

保存