gpt4 book ai didi

Laravel Passport Set session

转载 作者:行者123 更新时间:2023-12-03 15:57:18 25 4
gpt4 key购买 nike

当我尝试通过 laravel 护照设置 session 时出现以下错误

"message": "未根据请求设置 session 存储。",
"异常": "运行时异常",

最佳答案

这可以通过多种方式完成。

1.添加StartSession auth:api之后的中间件.

这是最直接的解决方案。在 auth:api 后面添加以下三行.

EncryptCookies::class,
AddQueuedCookiesToResponse::class,
StartSession::class,

然后删除 \Illuminate\View\Middleware\ShareErrorsFromSession::classmiddlewarePriority protected 属性(property)。没有这个 StartSession中间件将获得控制权 之前 auth更重要的是,在 EncryptCookies 之前中间件,基本上,总是会导致一个新的 session 。
<?php

namespace App\Http;

class Kernel extends HttpKernel
{
// Copy this values from
// \Illuminate\Foundation\Http\Kernel::$middlewarePriority
// then remove or comment line with StartSession. Without it,
// StartSession middleware will get control right before Auth.
// Which, basically, will create a new session because at this
// time cookies are still encrypted.
protected $middlewarePriority = [
// \Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\Illuminate\Auth\Middleware\Authenticate::class,
\Illuminate\Session\Middleware\AuthenticateSession::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
\Illuminate\Auth\Middleware\Authorize::class,
];

/**
* The application's route middleware groups.
*
* @var array
*/
protected $middlewareGroups = [
'web' => [
...
],
'api' => [
'throttle:120,1',
'bindings',
'auth:api', // https://laravel.com/docs/5.7/passport#protecting-routes

// Add the following three middleware right after `auth`
// in order to have a session.
EncryptCookies::class,
AddQueuedCookiesToResponse::class,
StartSession::class,
],
];
}

2. 创建您自己的 StartSession中间件。

拥有自己的中间件来启动 session 将使您无需覆盖 middlewarePriority .

首先,创建一个新类。
<?php

namespace App\Http\Middleware;

use Illuminate\Contracts\Foundation\Application;
use Illuminate\Session\Middleware\StartSession;
use Illuminate\Session\SessionManager;

class StartSessionShared extends StartSession
{
public function __construct(Application $app, SessionManager $manager)
{
parent::__construct($manager);
$app->singleton(StartSessionShared::class);
}
}

然后在 auth:api 后面添加以下三行.
EncryptCookies::class,
AddQueuedCookiesToResponse::class,
StartSessionShared::class,

此方法中的一个重要注意事项是调用 $app->singleton .如果没有它,Laravel 将始终创建此类的新实例。哪个会导致 \Illuminate\Session\Middleware\StartSession::terminate跳过保存 session 的方法。

3. 创建您自己的 StartSessionReadonly中间件。

如果您只想分享来自 web 的 session ,这是一个不错的选择
守卫到 api保护并且不打算以任何方式改变其值。这是我的情况。

创建以下 StartSessionReadonly中间件。然后在 api中使用守卫而不是 StartSession和两个它的 friend 。
<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Contracts\Encryption\DecryptException;
use Illuminate\Contracts\Encryption\Encrypter;
use Illuminate\Contracts\Session\Session;
use Illuminate\Http\Request;
use Illuminate\Session\Middleware\StartSession;
use Illuminate\Session\SessionManager;

/**
* Middleware for sharing session between `web` and `api` guards.
* Since the latter is essentially stateless, the session from
* `web` is shared as readonly.
*
* @package App\Http\Middleware
*/
class StartSessionReadonly extends StartSession
{
protected $encrypter;

public function __construct(Encrypter $encrypter, SessionManager $manager)
{
parent::__construct($manager);
$this->encrypter = $encrypter;
}

public function handle($request, Closure $next)
{
// If a session driver has been configured, we will need to start the session here
// so that the data is ready for an application. Note that the Laravel sessions
// do not make use of PHP "native" sessions in any way since they are crappy.
if ($this->sessionConfigured()) {
$request->setLaravelSession($this->startSession($request));
}

return $next($request);
}

public function getSession(Request $request)
{
return tap($this->manager->driver(), function (Session $session) use ($request) {
$payload = $request->cookies->get($session->getName());
$unserialize = EncryptCookies::serialized($session->getName());
try {
$session->setId($this->encrypter->decrypt($payload, $unserialize));
}
catch (DecryptException $exception) {
}
});
}
}

更新后 app/Http/Kernel.php您将拥有所有 api 的只读 session 。
<?php

namespace App\Http;

class Kernel extends HttpKernel
{
[...]

/****
* The application's route middleware groups.
*
* @var array
*/
protected $middlewareGroups = [
[...]
'api' => [
'throttle:120,1',
'bindings',
'auth:api', // https://laravel.com/docs/5.7/passport#protecting-routes
StartSessionReadonly::class,
],
];

关于Laravel Passport Set session ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51915982/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com