中間件有:1、Authenticate;2、CheckForMaintenanceMode;3、EncryptCookies;4、RedirectIfAuthenticated;5、TrimStrings;6、TrustProxies等等。
本教程操作環(huán)境:windows7系統(tǒng)、Laravel6版、Dell G3電腦。
Laravel自帶的中間件
Laravel 自帶了一些中間件,包括身份驗(yàn)證、CSRF 保護(hù)等。Laravel 具體啟用了哪些中間件,可通過(guò) appHttpKernel.php 文件查看。對(duì)于以 AppHttpMiddleware 開(kāi)頭的中間件(位于 app/Http/Middleware 目錄)是我們可以對(duì)其行為進(jìn)行定制的中間件。
Authenticate 中間件
源文件:appHttpMiddlewareHttpMiddlewareAuthenticate.php
<?php namespace AppHttpMiddleware; use IlluminateAuthMiddlewareAuthenticate as Middleware; class Authenticate extends Middleware { /** * Get the path the user should be redirected to when they are not authenticated. * * @param IlluminateHttpRequest $request * @return string */ protected function redirectTo($request) { if (! $request->expectsJson()) { return route('login'); } } }
作用:
用戶身份驗(yàn)證。可修改 redirectTo 方法,返回未經(jīng)身份驗(yàn)證的用戶應(yīng)該重定向到的路徑。
CheckForMaintenanceMode 中間件
源文件 :appHttpMiddlewareCheckForMaintenanceMode.php
<?php namespace AppHttpMiddleware; use IlluminateFoundationHttpMiddlewareCheckForMaintenanceMode as Middleware; class CheckForMaintenanceMode extends Middleware { /** * The URIs that should be reachable while maintenance mode is enabled. * * @var array */ protected $except = [ // ]; }
作用:
檢測(cè)項(xiàng)目是否處于 維護(hù)模式??赏ㄟ^(guò) $except 數(shù)組屬性設(shè)置在維護(hù)模式下仍能訪問(wèn)的網(wǎng)址。
EncryptCookies 中間件
源文件:appHttpMiddlewareEncryptCookies.php
<?php namespace AppHttpMiddleware; use IlluminateCookieMiddlewareEncryptCookies as Middleware; class EncryptCookies extends Middleware { /** * The names of the cookies that should not be encrypted. * * @var array */ protected $except = [ // ]; }
作用
對(duì) Cookie 進(jìn)行加解密處理與驗(yàn)證??赏ㄟ^(guò) $except 數(shù)組屬性設(shè)置不做加密處理的 cookie。
RedirectIfAuthenticated 中間件
源文件:appHttpMiddlewareRedirectIfAuthenticated.php
<?php namespace AppHttpMiddleware; use Closure; use IlluminateSupportFacadesAuth; class RedirectIfAuthenticated { /** * Handle an incoming request. * * @param IlluminateHttpRequest $request * @param Closure $next * @param string|null $guard * @return mixed */ public function handle($request, Closure $next, $guard = null) { if (Auth::guard($guard)->check()) { return redirect('/home'); } return $next($request); } }
作用:
當(dāng)請(qǐng)求頁(yè)是 注冊(cè)、登錄、忘記密碼 時(shí),檢測(cè)用戶是否已經(jīng)登錄,如果已經(jīng)登錄,那么就重定向到首頁(yè),如果沒(méi)有就打開(kāi)相應(yīng)界面??梢栽?handle 方法中定制重定向到的路徑。
TrimStrings 中間件
源文件:appHttpMiddlewareTrimStrings.php
<?php namespace AppHttpMiddleware; use IlluminateFoundationHttpMiddlewareTrimStrings as Middleware; class TrimStrings extends Middleware { /** * The names of the attributes that should not be trimmed. * * @var array */ protected $except = [ 'password', 'password_confirmation', ]; }
作用:
對(duì)請(qǐng)求參數(shù)內(nèi)容進(jìn)行 前后空白字符清理??赏ㄟ^(guò) $except 數(shù)組屬性設(shè)置不做處理的參數(shù)。
TrustProxies 中間件
源文件:appHttpMiddlewareTrustProxies.php
<?php namespace AppHttpMiddleware; use IlluminateHttpRequest; use FideloperProxyTrustProxies as Middleware; class TrustProxies extends Middleware { /** * The trusted proxies for this application. * * @var array|string */ protected $proxies; /** * The headers that should be used to detect proxies. * * @var int */ protected $headers = Request::HEADER_X_FORWARDED_ALL; }
作用:
配置可信代理??赏ㄟ^(guò) $proxies 屬性設(shè)置可信代理列表,$headers 屬性設(shè)置用來(lái)檢測(cè)代理的 HTTP 頭字段。
VerifyCsrfToken 中間件
源文件:appHttpMiddlewareVerifyCsrfToken.php
<?php namespace AppHttpMiddleware; use IlluminateFoundationHttpMiddlewareVerifyCsrfToken as Middleware; class VerifyCsrfToken extends Middleware { /** * Indicates whether the XSRF-TOKEN cookie should be set on the response. * * @var bool */ protected $addHttpCookie = true; /** * The URIs that should be excluded from CSRF verification. * * @var array */ protected $except = [ // ]; }
作用:
驗(yàn)證請(qǐng)求里的令牌是否與存儲(chǔ)在會(huì)話中令牌匹配??赏ㄟ^(guò) $except 數(shù)組屬性設(shè)置不做 CSRF 驗(yàn)證的網(wǎng)址。