【问题标题】:Laravel Sessions / Auth broken after update - Laravel 6Laravel Sessions / Auth 更新后损坏 - Laravel 6
【发布时间】:2020-01-25 20:51:20
【问题描述】:

我最近将 Laravel 和依赖项从 5.7 更新到了 6.0,但在用户登录 Laravel 并且 Session 在整个应用程序中持续存在时遇到了问题。

我可以使用手动登录用户

Auth()->loginUsingId(theID) 在我试图访问用户数据但用户未在整个应用程序中登录的控制器中。我想知道我的会话是否完全损坏,或者它是否与中间件有关。我通过 Laravel 使用标准文件系统方法进行会话存储。

我在中间件中没有我的路由,因为我希望我的 API 路由打开并在控制器中处理权限。

这可能与一些配置问题有关,但我似乎无法找到它。我还尝试重新下载 Laravel 6 并将我的代码添加到其中,但问题仍然存在。

没有发布太多代码,因为知道什么是最相关的,但如果有用的话可以。

kernel.php:

namespace App\Http;

use Illuminate\Foundation\Http\Kernel as HttpKernel;

class Kernel extends HttpKernel
{
/**
 * The application's global HTTP middleware stack.
 *
 * These middleware are run during every request to your application.
 *
 * @var array
 */
protected $middleware = [
    \App\Http\Middleware\TrustProxies::class,
    \App\Http\Middleware\CheckForMaintenanceMode::class,
    \Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
    \App\Http\Middleware\TrimStrings::class,
    \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
];

/**
 * The application's route middleware groups.
 *
 * @var array
 */

protected $middlewareGroups = [
    'web' => [
        \App\Http\Middleware\EncryptCookies::class,
        \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
        \Illuminate\Session\Middleware\StartSession::class,
        // \Illuminate\Session\Middleware\AuthenticateSession::class,
        \Illuminate\View\Middleware\ShareErrorsFromSession::class,
        \App\Http\Middleware\VerifyCsrfToken::class,
        \Illuminate\Routing\Middleware\SubstituteBindings::class,
    ],

    'api' => [
        'throttle:60,1',
        'bindings',
    ],
];

/**
 * The application's route middleware.
 *
 * These middleware may be assigned to groups or used individually.
 *
 * @var array
 */
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,
];

/**
 * The priority-sorted list of middleware.
 *
 * This forces non-global middleware to always be in the given order.
 *
 * @var array
 */
protected $middlewarePriority = [
    \Illuminate\Session\Middleware\StartSession::class,
    \Illuminate\View\Middleware\ShareErrorsFromSession::class,
    \App\Http\Middleware\Authenticate::class,
    \Illuminate\Routing\Middleware\ThrottleRequests::class,
    \Illuminate\Session\Middleware\AuthenticateSession::class,
    \Illuminate\Routing\Middleware\SubstituteBindings::class,
    \Illuminate\Auth\Middleware\Authorize::class,
];

【问题讨论】:

  • "我在中间件中没有路由" -> 您需要一些会话中间件,例如 \Illuminate\Session\Middleware\StartSession 才能使用会话。
  • 我的项目中有那个文件——还需要什么?
  • 你能添加app/Http/Kernel.phpLoginController吗? “添加我的代码”是什么意思?您只是复制并粘贴了整个文件夹,还是将代码片段添加到了新源?另外,您使用的是什么会话驱动程序?
  • Mm idk,我只知道他们更改了默认登录表单,也许在这里阅读有关它的新文档:laravel.com/docs/6.x/authentication#authentication-quickstart 基本上在 5 中是 php artisan make:auth 现在是 composer require laravel/ui --dev @987654329 @
  • @IlGala 我正在使用文件会话驱动程序。是的,我从 Laravel 源代码的新克隆开始,并将我的代码添加到其中,但仍然没有解决方案。我认为这可能与 Kernel.php

标签: php laravel session


【解决方案1】:

所以我最终发现是这样的:

我的所有 api 路由都有一个 api.php 文件。在大多数情况下,我为每个特定于域的路由(例如,api/user.php)都有单独的文件,并且我将这些文件包含在主 api.php 文件中。

这些域路由模式之一是“api/settings”。当我将我的设置路由移动到他们自己的文件中并将其包含在 api.php 中时,这就是破坏一切的原因。我不知道为什么,但是将单个路由放回 api.php (而不是单独的文件包含)解决了这个问题。我想这是与 Laravel 的某种路由冲突。不知道。那个花了我一段时间。

【讨论】:

    【解决方案2】:

    我有同样的问题,认为它与 Laravel 6 有关。对我来说,结果是我为我的 users 表使用了非标准主键,并且需要在 User 模型上声明它。

    对于会话故障排除,请记住对 config/session.php 的更改会擦除当前会话。我发现在测试之间运行 php artisan config:clearphp artisan cache:clear 很有帮助。

    【讨论】:

      猜你喜欢
      • 2020-09-18
      • 1970-01-01
      • 2017-06-23
      • 1970-01-01
      • 1970-01-01
      • 2016-04-06
      • 2018-11-04
      • 1970-01-01
      • 2019-03-14
      相关资源
      最近更新 更多