# 认证中间件

在例9-3中，您了解了如何检查访问者是否已登录，如果没有则重定向。 您可以在应用程序的每个路径上执行这些类型的检查，但很快就会变得乏味。 事实证明，路由中间件（请参阅第10章了解有关它们如何工作的更多信息）非常适合限制访客或经过身份验证的用户的某些路由。

Laravel为我们提供了现成的中间件，可以在App\Http\Kernel查看提供了哪些中间件。

```php
protected $routeMiddleware = [
    'auth' => \App\Http\Middleware\Authenticate::class,
    'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
    'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
    'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class,
    'can' => \Illuminate\Auth\Middleware\Authorize::class,
    'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
    'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class,
    'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
    'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
];
```

四个默认路由中间件与认真相关：

*auth*

限制对经过身份验证的用户的路由访问

*auth.basic*

使用基于HTTP限制对经过身份验证的用户的访问

*guest*

限制对未经身份验证的用户的访问。

*can*

通常用户认证用于需要认证的部分，认证过的用户不需要看到(类似登录表单)，auth.basic是一个不太常用的中间件。

示例9-8显示了一些受auth中间件保护的路由的示例

{% code title="Example 9-8. Sample routes protected by auth middleware" %}

```php
Route::middleware('auth')->group(function () { 
    Route::get('account', 'AccountController@dashboard');
});
Route::get('login', 'Auth\LoginController@getLogin')->middleware('guest');
```

{% endcode %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://liseen315.gitbook.io/laravel/yong-hu-ren-zheng-yu-shou-quan/ren-zheng-zhong-jian-jian.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
