使用 Laravel Auth 中间件

Using Laravel Auth middleware

Laravel 5.1 确实只有很少的文档.. 我需要清楚地了解如何使用 Auth middileware 保护路由..

文档告诉我们添加 "middleware" => "auth" 参数到路由。 或者可以做

    public function __construct() 
    {
      $this->middleware('auth');
    }

但是如何使用 Auth 中间件进行实际用户身份验证和自动重定向到 /login 从受保护的路由??

在 Kernel.php 中 - 在受保护的 $routeMiddleware 下注册了中间件,如下所示:

/**
 * The application's route middleware.
 *
 * @var array
 */
protected $routeMiddleware = [
    'auth' => 'App\Http\Middleware\Authenticate',
    'auth.basic' => 'Illuminate\Auth\Middleware\AuthenticateWithBasicAuth',
    'guest' => 'App\Http\Middleware\RedirectIfAuthenticated',
];

您可以看到 'auth' 已注册使用 App\Http\Middleware\Authenticate。

然后你可以按照这个路径 - 如果你打开 /app/Http/Middleware/Authenticate.php, 你会发现 public 函数句柄:

    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if ($this->auth->guest())
        {
            if ($request->ajax())
            {
                return response('Unauthorized.', 401);
            }
            else
            {
                return redirect()->guest('auth/login');
            }
        }

        return $next($request);
    }

这里是管理重定向的地方,你可以根据自己的需要修改它,或者你可以创建自定义中间件。

最后 - 正如文档中所写 - 在需要进行身份验证的控制器中,您将添加

public function __construct() 
{
  $this->middleware('auth');
}

如果提供的中间件不适合您的需要,您可以创建自定义中间件。

在 laravel 5.2 如果你想隐藏注册表单或登录表单视图你应该使用你的中间件:

$this->middleware('mymiddleware', ['only' => ['register', 'showRegistrationForm', 'login', 'showLoginForm']]);

$this->middleware('mymiddleware', ['except' => ['register', 'showRegistrationForm', 'login', 'showLoginForm']]);

这是因为注册和登录路由是 AuthController 上的 post 方法,而 showXxxxForm 是表单视图。

希望对大家有所帮助。

在Laravel中,中间件用于某些路由只有用户登录才能访问,否则会重定向到登录页面。

Auth::routes();
Route::middleware(['auth'])->group(function () {
//After Login the routes are accept by the loginUsers...

}
Route::middleware(['admin'])->group(function(){
//the Admin is logged in to access the Routes...
}

//使用中间件进行登录认证

1)制作中间件:

php artisan make:middleware adminAuth

2) 写入中间件文件:

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class loginAuth
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle(Request $request, Closure $next)
    {
        $isAuthenticatedAdmin = (Auth::check());

        //This will be excecuted if the new authentication fails.
        if (!$isAuthenticatedAdmin){

            return redirect()->route('login')->with('message', 'Authentication Error.');
        }
        return $next($request);

    }
}

3) 在

行下方添加app/http/kernal.php
protected $routeMiddleware = [
  'adminAuth' => \App\Http\Middleware\AdminAuth::class //Registering New Middleware
];

4)在中间件中添加路由:

Route::get('login',[AuthController::class,'index'])->name('login'); //named route

Route::get('dashboard',function(){
    return view('admin-page.dashboard');
})->middleware("adminAuth");