有关于laravel框架中Session的疑惑

Laravel8.x框架中

     'api'       => [
            // \Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class,
            'throttle:global',
            \Illuminate\Routing\Middleware\SubstituteBindings::class,
            \App\Http\Middleware\EncryptCookies::class,
            \Illuminate\Session\Middleware\StartSession::class,
            \Illuminate\Session\Middleware\AuthenticateSession::class,

每次请求这个路由组内的接口,为啥每次都会存入一个_token的序列化进入session呢?
session的driver是redis的,但是存入的key值为啥是读取了CACHE_PREFIX缓存的前缀

存入的数据类型与这种

a:3:{s:6:"_token";s:40:"8eJbSWEsdsads2IR1sr55yBdvpSQuMIvTFuZze";s:9:"_previous";a:1:{s:3:"url";s:57:"http://www.xxxx.com/get?page=1&per_page=20";}s:6:"_flash";a:2:{s:3:"old";a:0:{}s:3:"new";a:0:{}}}
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
讨论数量: 4

session配置的是redis的驱动呀,但是为啥存入的key会是cache的prefix

10个月前 评论
  1. _token 问题可以看下这个 CSRF 保护《Laravel 10 中文文档》
  2. session 使用 redis 驱动,从源码来看其实是 CacheRedisStore,配置文件有解释
// 配置文件文件 config/session.php

/*
|--------------------------------------------------------------------------
| Session Cache Store
|--------------------------------------------------------------------------
|
| While using one of the framework's cache driven session backends you may
| list a cache store that should be used for these sessions. This value
| must match with one of the application's configured cache "stores".
|
| Affects: "apc", "dynamodb", "memcached", "redis"
|
*/

'store' => env('SESSION_STORE'),
// vendor/laravel/framework/src/Illuminate/Session/SessionManager.php

/**
 * Create an instance of the Redis session driver.
 *
 * @return \Illuminate\Session\Store
 */
protected function createRedisDriver()
{
    $handler = $this->createCacheHandler('redis');

    $handler->getCache()->getStore()->setConnection(
        $this->config->get('session.connection')
    );

    return $this->buildSession($handler);
}
10个月前 评论
CodeUndefined (楼主) 10个月前

疑惑解决了! 通过观察Laravel框架源码,发现中间件StartSession中会对于每个中间件内的请求存入session 具体方法如下

/**
     * Handle the given request within session state.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Illuminate\Contracts\Session\Session  $session
     * @param  \Closure  $next
     * @return mixed
     */
    protected function handleStatefulRequest(Request $request, $session, 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.
        $request->setLaravelSession(
            $this->startSession($request, $session)
        );

        $this->collectGarbage($session);

        $response = $next($request);

        $this->storeCurrentUrl($request, $session);

        $this->addCookieToResponse($response, $session);

        // Again, if the session has been configured we will need to close out the session
        // so that the attributes may be persisted to some storage medium. We will also
        // add the session identifier cookie to the application response headers now.
        $this->saveSession($request);

        return $response;
    }

这就解释为什么每次请求都会往session存入一个_token值了。至于为啥会去读cache的前缀就在另外一步。

/**
     * Create a new session middleware.
     *
     * @param  \Illuminate\Session\SessionManager  $manager
     * @param  callable|null  $cacheFactoryResolver
     * @return void
     */
    public function __construct(SessionManager $manager, callable $cacheFactoryResolver = null)
    {
        $this->manager = $manager;
        $this->cacheFactoryResolver = $cacheFactoryResolver;
    }

具体就是这个SessionManager 的类在处理会根据不同配置创建不同的缓存驱动 至于为什么是调用的是默认的cache驱动我还在研究中

10个月前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!