【问题标题】:Disable "Cookie" header when responding json响应 json 时禁用“Cookie”标头
【发布时间】:2015-02-20 08:46:17
【问题描述】:

我想在响应为 json 时禁用(删除)“Cookie”标头。实际上,我可以使用 Laravel 4.2 在过滤器上设置 Config::set('session.driver', 'array')

如果我在 L5(版本 5.0.5)中进行,我在日志文件中出现以下错误。

[YYYY-MM-DD ..:..:..] local.ERROR: exception 'ErrorException' with message 'Undefined index: _sf2_meta' in /foo/bar/vendor/laravel/framework/src/Illuminate/Session/Store.php:280
Stack trace:
#0 /foo/bar/vendor/laravel/framework/src/Illuminate/Session/Store.php(280): Illuminate\Foundation\Bootstrap\HandleExceptions->handleError(8, 'Undefined index...', '/foo/bar/ve...', 280, Array)
#1 /foo/bar/vendor/laravel/framework/src/Illuminate/Session/Store.php(251): Illuminate\Session\Store->addBagDataToSession()
#2 /foo/bar/vendor/laravel/framework/src/Illuminate/Session/Middleware/StartSession.php(89): Illuminate\Session\Store->save()
#3 /foo/bar/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(129): Illuminate\Session\Middleware\StartSession->terminate(Object(Illuminate\Http\Request), Object(Illuminate\Http\JsonResponse))
#4 /foo/bar/public/index.php(57): Illuminate\Foundation\Http\Kernel->terminate(Object(Illuminate\Http\Request), Object(Illuminate\Http\JsonResponse))
#5 {main}

【问题讨论】:

标签: laravel-5


【解决方案1】:

好的,事实证明,不再可能在路由中间件中更改会话/cookie 驱动程序。您必须在 Illuminate\Session\Middleware\StartSession 中间件之前指定中间件。

解决方案: 1. 创建自己的中间件:

class ApiSession implements Middleware{
    public function handle($request, Closure $next){
        $path = $request->getPathInfo();

        if(strpos($path, '/api/') === 0){
            \Config::set('session.driver', 'array');
            \Config::set('cookie.driver', 'array');
        }

        return $next($request);
    }
}
  1. 在 Session 中间件之前的内核文件(app/Http/Kernel.php)中添加:

[..] ApiSession::class, // Check if an API request. If so, set session, cookie drivers Illuminate\Session\Middleware\StartSession::class, [..]

不好的部分是您不能将它与路由组一起使用。您必须通过检查当前 url 路径来检查是否应用了此中间件。

【讨论】:

  • 感谢您的回答,但抱歉我听不懂第二个。你能告诉我应该放哪个文件ApiSession::class
  • @hide.h 有一个文件发生了所有中间件映射:app/Http/Kernel.php
  • 感谢您的大提示!我现在可以禁用“Set-Cookie”标头,但我需要修改一些您建议的代码。无论如何,我会在这里发布我的解决方案! :)
  • 这有助于我摆脱 laravel_session 标头,但我还看到了 XSRF-TOKEN 标头。你有什么想法吗?
【解决方案2】:

无论如何,如果您想在所有请求上禁用 cookie,您可以按原样删除 if 语句:

// myapp/app/Http/Middleware/ApiSession.php

<?php

namespace App\Http\Middleware

use Closure;

class ApiSession {

         public function handle($request, closure $next){

               \Config::set('session.driver', 'array');
               \Config::set('cookie.driver', 'array');

         }
}

记得添加/修改您的 kernel.php,如“Mārtiņš Briedis”所示。 现在,您的应用不使用 cookie。

【讨论】:

    【解决方案3】:

    此代码 Config::set('session.driver' , 'array') 禁用 laravel_session ... set-cookie 标头集和上面的代码禁用了该 HEADER ,但 XRFF 存在另一个名为 Set-Cookie 的 HEADER -TOKEN ,要禁用此问题,您应该覆盖中间件 VerifyCsrfToken 。

    下面的代码在app->Http->MiddleWare->VerifyCsrfToken.php中添加,并将这个函数添加到VerifyCsrfToken类中

        protected function addCookieToResponse($request, $response)
    {
        $config = config('session');        
        if ($config['driver'] == 'array' || !$this->shouldPassThrough($request))
        {        
            return $response;
        }else
        {
            $response->headers->setCookie(
                new Cookie(
                    'XSRF-TOKEN', $request->session()->token(), time() + 60 * $config['lifetime'],
                    $config['path'], $config['domain'], $config['secure'], false
                )
            );
        }
        return $response;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-10-09
      • 1970-01-01
      • 2021-02-09
      • 1970-01-01
      • 2022-07-20
      • 1970-01-01
      • 2017-11-23
      相关资源
      最近更新 更多