spatie / laravel-cors
Requires
- php: ^7.2|^8.0
- illuminate/support: 5.5.*|5.6.*|5.7.*|5.8.*|^6.0
Requires (Dev)
- orchestra/testbench: 3.5.*|3.6.*|3.7.*|3.8.*|^4.0
- phpunit/phpunit: ^8.0
README
我们已弃用此包,因为 Laravel 7 引入了原生的 CORS 支持。只有在使用 Laravel 6 或更低版本时才应使用此包。
在 Laravel 应用程序中发送 CORS 头
此包将为您的 Laravel 或 Lumen 应用的响应添加 CORS 头。有关 CORS 的更多信息,请参阅 Mozilla CORS 文档。
此包支持预检请求,并且易于配置以满足您的需求。
安装
Laravel
您可以通过 Composer 安装此包
composer require spatie/laravel-cors
包将自动注册其服务提供者。
必须将提供的 Spatie\Cors\Cors
中间件注册到全局中间件组中。
// app/Http/Kernel.php protected $middleware = [ ... \Spatie\Cors\Cors::class ];
php artisan vendor:publish --provider="Spatie\Cors\CorsServiceProvider" --tag="config"
这是在 config/cors.php
发布的配置文件的默认内容
return [ /* * A cors profile determines which origins, methods, headers are allowed for * a given requests. The `DefaultProfile` reads its configuration from this * config file. * * You can easily create your own cors profile. * More info: https://github.com/spatie/laravel-cors/#creating-your-own-cors-profile */ 'cors_profile' => Spatie\Cors\CorsProfile\DefaultProfile::class, /* * This configuration is used by `DefaultProfile`. */ 'default_profile' => [ 'allow_credentials' => false, 'allow_origins' => [ '*', ], 'allow_methods' => [ 'POST', 'GET', 'OPTIONS', 'PUT', 'PATCH', 'DELETE', ], 'allow_headers' => [ 'Content-Type', 'X-Auth-Token', 'Origin', 'Authorization', ], 'expose_headers' => [ 'Cache-Control', 'Content-Language', 'Content-Type', 'Expires', 'Last-Modified', 'Pragma', ], 'forbidden_response' => [ 'message' => 'Forbidden (cors).', 'status' => 403, ], /* * Preflight request will respond with value for the max age header. */ 'max_age' => 60 * 60 * 24, ], ];
Lumen
您可以通过 Composer 安装此包
composer require spatie/laravel-cors
从供应商目录复制配置文件
cp vendor/spatie/laravel-cors/config/cors.php config/cors.php
在 bootstrap/app.php
中注册配置文件、中间件和服务提供者
$app->configure('cors'); $app->middleware([ Spatie\Cors\Cors::class, ]); $app->register(Spatie\Cors\CorsServiceProvider::class);
使用
安装中间件后,您的 API 路由现在应该会收到适当的 CORS 头。也会处理预检请求。如果收到不允许的请求,Laravel 将返回 403
响应。
此包的默认配置允许来自任何来源(表示为 '*'
)的所有请求。您可能至少想要指定一些与您的项目相关的来源。如果您想允许来自 https://spatie.be
和 https://laravel.net.cn
的请求,请将这些域名添加到配置文件中
// config/cors.php ... 'default_profile' => [ 'allow_origins' => [ 'https://spatie.be', 'https://laravel.net.cn', ], ... ...
例如,如果您想允许来自特定域的所有子域的请求,您可以使用通配符星号 (*
) 并指定它
// config/cors.php ... 'default_profile' => [ 'allow_origins' => [ 'https://spatie.be', 'https://laravel.net.cn', 'https://*.spatie.be', 'https://*.laravel.com', ], ... ...
创建自己的 CORS 配置文件
假设您已经在用户模型中添加了一个 allowed_domains
列。在这种情况下,简单的 DefaultProfile
读取配置文件的方法将不足以满足需求。幸运的是,编写自己的 CORS 配置文件非常简单,它只是一个扩展 Spatie\Cors\DefaultProfile
的类。
以下是一个快速示例,其中假设您已经为用户模型添加了 allowed_domains
列
namespace App\Services\Cors; use Spatie\Cors\CorsProfile\DefaultProfile; class UserBasedCorsProfile extends DefaultProfile { public function allowOrigins(): array { return Auth::user()->allowed_domains; } }
您可以通过编辑配置文件中的 forbidden_response
数组来覆盖请求被禁止时返回的默认 HTTP 状态码和消息
'forbidden_response' => [ 'message' => 'Your request failed', 'status' => 400, ],
别忘了在配置文件中注册您的配置文件。
// config/cors.php ... 'cors_profile' => App\Services\Cors\UserBasedCorsProfile::class, ...
在上面的示例中,我们覆盖了 allowOrigins
方法,但当然您也可以选择覆盖 DefaultProfile
中存在的任何方法。
测试
composer test
变更日志
请参阅变更日志获取最近更改的详细信息。
贡献
请参阅贡献指南以获取详细信息。
安全
如果您发现任何安全相关的问题,请通过电子邮件freek@spatie.be联系,而不是使用问题跟踪器。
替代方案
- barryvdh/laravel-cors:一个经过测试和验证的包。我们的包是Barry的优秀包基本功能的现代化重写。我们创建了自己的解决方案,因为我们需要我们的配置非常灵活。
邮卡软件
您可以自由使用此包,但如果它进入您的生产环境,我们非常感谢您从您家乡给我们寄一张明信片,并说明您正在使用我们的哪些包。
我们的地址是:Spatie,Samberstraat 69D,2060 安特卫普,比利时。
我们将在我们的公司网站上发布所有收到的明信片。
鸣谢
支持我们
Spatie是一家位于比利时的安特卫普的网页设计公司。您可以在我们的网站上找到我们所有开源项目的概述。
您的业务是否依赖于我们的贡献?请与我们联系,并在Patreon上支持我们。所有承诺都将专门用于分配人力资源以维护和新酷的东西。
许可证
MIT许可证(MIT)。请参阅许可证文件以获取更多信息。