【问题标题】:Laravel CORS:: PreflightMissingAllowedOriginHeaderLaravel CORS:: PreflightMissingAllowedOriginHeader
【发布时间】:2021-09-22 20:50:24
【问题描述】:

我最近开始学习 Laravel。目前有一个与 CORS 相关的问题,这让我抓狂。

我按照Fruitcake cors package 的安装指南进行操作

我的 cors.php:

/*
|--------------------------------------------------------------------------
| Cross-Origin Resource Sharing (CORS) Configuration
|--------------------------------------------------------------------------
|
| Here you may configure your settings for cross-origin resource sharing
| or "CORS". This determines what cross-origin operations may execute
| in web browsers. You are free to adjust these settings as needed.
|
| To learn more: https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS
|
*/

'paths' => [],

'allowed_methods' => ['*'],

'allowed_origins' => ['*'],

'allowed_origins_patterns' => [],

'allowed_headers' => ['*'],

'exposed_headers' => false,

'max_age' => 0,

'supports_credentials' => false,

我的 kernel.php:

protected $middleware = [
    \Fruitcake\Cors\HandleCors::class,
    ForceJsonResponse::class,
    // \App\Http\Middleware\TrustHosts::class,
    \App\Http\Base\Middleware\TrustProxies::class,
    \App\Http\Base\Middleware\PreventRequestsDuringMaintenance::class,
    \Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
    \App\Http\Base\Middleware\TrimStrings::class,
    \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
];

/**
 * The application's route middleware groups.
 *
 * @var array
 */
protected $middlewareGroups = [
    'web' => [
        \App\Http\Base\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\Base\Middleware\VerifyCsrfToken::class,
        \Illuminate\Routing\Middleware\SubstituteBindings::class,
    ],

    'api' => [
        'throttle:api',
        \Illuminate\Routing\Middleware\SubstituteBindings::class,
    ],
];

/**
 * The application's route middleware.
 *
 * These middleware may be assigned to groups or used individually.
 *
 * @var array
 */
protected $routeMiddleware = [
    'auth' => \App\Http\Base\Middleware\Authenticate::class,
    'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
    'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class,
    'can' => \Illuminate\Auth\Middleware\Authorize::class,
    'guest' => \App\Http\Base\Middleware\RedirectIfAuthenticated::class,
    'password.confirm' => \Illuminate\Auth\Middleware\RequirePassword::class,
    'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class,
    'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
    'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
];

Laravel 运行在:http://127.0.0.1:8000 和我的前端在 http://localhost:8080/

不确定这是否是个问题。

/* 编辑 */

paths 使用通配符似乎确实解决了/playlist/{id} URL 上的CORS 问题。它不适用于/playlist/{id}/refresh 之类的 URL ..

【问题讨论】:

    标签: php laravel cors


    【解决方案1】:

    嗯,你有它适用于没有路径:

    'paths' => [],
    

    所以可能会添加他们应该能够访问的路径

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

    【讨论】:

    • 谢谢,这部分解决了问题。我确实使用了'paths' => ['*'],因为['api/*'] 不起作用。我现在解决了像 /playlists/1 这样的路径,但是像 /playlists/1/sync 这样的路径仍然给出了 CORS 错误..
    猜你喜欢
    • 2015-09-30
    • 2014-06-01
    • 1970-01-01
    • 2019-06-10
    • 2019-08-22
    • 2018-12-03
    • 2015-02-19
    • 2023-03-17
    • 2015-04-12
    相关资源
    最近更新 更多