【问题标题】:Laravel CORS with FruitcakeLaravel CORS 与水果蛋糕
【发布时间】:2020-05-26 19:31:13
【问题描述】:

我用 laravel 后端制作 react 项目...我有一个 CORS 问题,我按照下面的链接执行所有操作,配水果蛋糕。

Laravel 6 CORS policy issue with API 但仍然无法正常工作。

cors.php:

        'paths' => ['api/*'],

    /*
    * Matches the request method. `[*]` allows all methods.
    */
    'allowed_methods' => ['*'],

    /*
     * Matches the request origin. `[*]` allows all origins.
     */
    'allowed_origins' => ['*'],

    /*
     * Matches the request origin with, similar to `Request::is()`
     */
    'allowed_origins_patterns' => [],

    /*
     * Sets the Access-Control-Allow-Headers response header. `[*]` allows all headers.
     */
    'allowed_headers' => ['*'],

    /*
     * Sets the Access-Control-Expose-Headers response header.
     */
    'exposed_headers' => false,

    /*
     * Sets the Access-Control-Max-Age response header.
     */
    'max_age' => false,

    /*
     * Sets the Access-Control-Allow-Credentials header.
     */
    'supports_credentials' => false,

而且,内核中间件是:

        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,

        \Fruitcake\Cors\HandleCors::class,
    ];

还有什么问题?

【问题讨论】:

    标签: reactjs laravel react-redux cors laravel-middleware


    【解决方案1】:
    php artisan config:clear
    php artisan route:clear
    php artisan cache:clear
    

    确保您的权限设置正确(例如,存储是可写的)

    【讨论】:

      【解决方案2】:

      Fruitcake\Cors\HandleCors::class 很麻烦。只需将其从各处删除,然后将这 3 个标头添加到顶部的 api.php 路由文件中即可。

      header('Access-Control-Allow-Methods: GET, POST, PATCH, PUT, DELETE, OPTIONS');
      header('Access-Control-Allow-Headers: Origin, Content-Type, X-Auth-Token, Authorization, Accept,charset,boundary,Content-Length');
      header('Access-Control-Allow-Origin: *');
      

      【讨论】:

      • 你能告诉我们为什么它是“麻烦的”吗?您的解决方案无法正确处理预检
      • 它带有一个注册为cors的路由中间件。正确的?那么使用它有什么好主意。通过将其作为路由或路由组中间件。它只是无法添加所需的头文件,直到您将其添加到内核文件中的全局中间件中。
      • 好的,但这是 laravel 的做法。顺便说一句,laravel 7 默认使用fruitcake 组件,因此没有理由避免它。你这样做的方式可能会在预检时给出 404,所以这不是一个解决方案。
      • 我也在 laravel 7 本身上使用过它。就我而言,我尝试了一切。但它没有用。所以如果你有它的工作,请发布一个答案。
      【解决方案3】:

      以下是使用fruitcake/laravel-cors 时的一些注意事项:

      • app/Http/Kernel.php中将HandleCors中间件放在$middleware的顶部:
      protected $middleware = [
          \Fruitcake\Cors\HandleCors::class,
          \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,
      ];
      

      将其放在底部或介于两者之间是行不通的,因为请求可能会被其他具有更高优先级的中间件拒绝。

      • 不要在控制器中死亡或退出。

      例如以下将不起作用:

      Route::get('/cors-test', function() {
         dd("This won't work");
      });
      

      因为Fruitcake\Cors\HandleCors::handle 方法添加了相关的标头AFTER处理请求:

      Fruitcake\Cors\HandleCors.php

      public function handle($request, Closure $next)
      {
          // --- omitted
      
          // Handle the request
          $response = $next($request); // <--- if you die here
      
          if ($request->getMethod() === 'OPTIONS') {
              $this->cors->varyHeader($response, 'Access-Control-Request-Method');
          }
          
          // you will never reach here
          return $this->addHeaders($request, $response);
      }
      

      dump 也不起作用

      • 更改app/config/cors.php后清除配置缓存:
      $ php artisan config:cache
      

      【讨论】:

      • 在我的例子中是 dump 在 FormRequest 方法中
      【解决方案4】:

      credentials: 'same-origin' 添加到react App 中的请求标头

      【讨论】:

        【解决方案5】:

        实际上,只需从您的代码中删除 dd 和 die 命令。

        【讨论】:

        • 感谢您的回答。事实上,@bravemaster 已经指出了这一点,另一方面,我在任何代码示例中都看不到线程所有者在执行 dd()die()
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-11-16
        • 2012-05-21
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多