0

//内核.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 和来宾中间件有什么区别?

4

2 回答 2

2

auth 中间件- 用于会话身份验证。auth 中间件检查用户是否经过身份验证。如果用户没有会话,则此路由会将请求重定向到登录路由。

    /**
     * Get the path the user should be redirected to when they are not authenticated.
     *
     */
    protected function redirectTo($request)
    {
        if (! $request->expectsJson()) {
            return route('login');
        }
    }

来宾中间件- 此中间件逻辑是,如果用户已登录,则将其重定向到RouteServiceProvider::HOME.

    public function handle(Request $request, Closure $next, ...$guards)
    {
        $guards = empty($guards) ? [null] : $guards;

        foreach ($guards as $guard) {
            if (Auth::guard($guard)->check()) {
                return redirect(RouteServiceProvider::HOME);
            }
        }

        return $next($request);
    }

你可以设置RouteServiceProvider::HOME路径App\Providers\RouteServiceProvider

于 2021-07-14T04:56:54.650 回答
0

认证中间件

确定用户是否登录到任何警卫。如果未登录,则会抛出AuthenticationException并重定向到登录页面。

访客中间件

在这里它将检查用户是否登录到任何警卫。如果是,则重定向到RouteServiceProvider::HOME提到的页面。否则它传递到请求的页面。

如果您想保护您的路由免受非登录用户的影响,请使用auth middleware

于 2021-07-14T04:52:18.837 回答