bairwell / middleware-cors
PSR-7 中间件层,用于提供 CORS(跨源请求安全)头部和安全措施。这个中间件不仅允许无效的 CORS 请求通过,而且在验证后还会主动阻止它们。
Requires
- php: ^7.0
- psr/http-message: ^1.0
- psr/log: ~1.0
Requires (Dev)
- monolog/monolog: ^1.13
- phpunit/phpunit: ^5.1
- squizlabs/php_codesniffer: ^2.5
This package is auto-updated.
Last update: 2024-08-29 04:05:16 UTC
README
这是一个 PHP 7 兼容的 Composer 库,提供了一个与 PSR-7 兼容的中间件层,用于处理 "CORS"(跨源请求安全/跨源 HTTP 请求/HTTP 访问控制)头部和安全。
这个库相对于其他 CORS 库提供了什么?
- PHP-7 类型声明。
- 作为一个 PSR-7 中间件,它与其他许多框架(如 Slim 3 和 Symfony)兼容。
- 对配置设置具有极大的灵活性(大多数可以设置为字符串、数组或回调)。
- 遵循 CORS 流程图 并积极拒绝无效请求。
- 仅在必要时发送适当的头部。
- 对于 CORS "OPTIONS" 请求,确保返回一个空白页面 204 "无内容" 页面,而不是返回不希望的内容体。
- 支持基于 PSR-3 的日志记录器,用于调试目的。
- 忽略非 CORS "OPTIONS" 请求(例如,在 REST 服务中)。CORS 请求由传入请求中存在 "Origin:" 头部来指示。
- 完全单元测试。
- 根据 MIT 许可证,允许你几乎做任何事情。
- 使用命名空间且完全面向对象。
- 阻止无效设置。
- 第三方要求最少(只需要定义文件 "psr/http-message" 和 "psr/log" 作为接口定义,以及 PHPUnit、PHPCodeSniffer 和 Monolog 用于开发和测试)。
安装
使用 Composer 安装最新版本
$ composer require bairwell/middleware-cors
或通过修改您的 composer.json
文件
{
"require": {
"bairwell/middleware-cors": "@stable"
}
}
或从 Github 仓库(这是分叉和贡献所必需的)
$ git clone git://github.com:bairwell/middleware-cors.git
使用方法
您可以使用这个 CORS 库,就像这样
$slim = new \Slim\App(); // use Slim3 as it supports PSR7 middleware // add CORs $slim->add(new MiddlewareCors()); // add routes $slim->run(); // get Slim running
但这并不会增加多少(因为它默认允许所有主机来源和方法)。
您可以通过
$slim = new \Slim\App(); // use Slim3 as it supports PSR7 middleware $config = [ 'origin' => '*.example.com' // allow all hosts ending example.com ]; // add CORs $slim->add(new MiddlewareCors($config)); // add routes $slim->run(); // get Slim running
或
$slim = new \Slim\App(); // use Slim3 as it supports PSR7 middleware $config = [ 'origin' => ['*.example.com', '*.example.com.test', 'example.com', 'dev.*'], 'allowCredentials' => true ]; $slim->add(new MiddlewareCors($config)); // add CORs // add routes $slim->run(); // get Slim running
使其稍微复杂一些,这将允许所有以 .example.com 或 *.example.com.test 结尾的来源,精确的 example.com 来源或任何以 dev 开头的域名。它还将允许允许凭证。
对于更复杂的集成,该集成依赖于 Slim 路由器来提供每个路由允许的实际方法,请参阅 tests/MiddlewareCors/FunctionalTests/SlimTest.php
建议的设置
// read the allowed methods for a route $corsAllowedMethods = function (ServerRequestInterface $request) use ($container) : array { // if this closure is called, make sure it has the route available in the container. /* @var RouterInterface $router */ $router = $container->get('router'); $routeInfo = $router->dispatch($request); $methods = []; // was the method called allowed? if ($routeInfo[0] === Dispatcher::METHOD_NOT_ALLOWED) { $methods = $routeInfo[1]; } else { // if it was, see if we can get the routes and then the methods from it. // @var \Slim\Route $route $route = $request->getAttribute('route'); // has the request get a route defined? is so use that if (null !== $route) { $methods = $route->getMethods(); } } // if we have methods, let's list them removing the OPTIONs one. if (0 === count($methods)) { // find the OPTIONs method $key = array_search('OPTIONS', $methods,true); // and remove it if set. if (false !== $key) { unset($methods[$key]); $methods = array_values($methods); } } return $methods; }; $cors = new MiddlewareCors([ 'origin' => ['*.example.com','example.com','*.example.com.test','192.168.*','10.*'], 'exposeHeaders' => '', 'maxAge' => 120, 'allowCredentials' => true, 'allowMethods' => $corsAllowedMethods, 'allowHeaders' => ['Accept', 'Accept-Language', 'Authorization', 'Content-Type','DNT','Keep-Alive','User-Agent','X-Requested-With','If-Modified-Since','Cache-Control','Origin'], ]); $slim->add($cors);
标准
应遵循以下 PHP FIG 标准
- PSR 1 - 基本编码标准
- PSR 2 - 编码风格指南
- PSR 3 - 日志接口
- PSR 4 - 自动加载标准
- PSR 5 - PHPDoc 标准 - (仍为草案)
- PSR 7 - HTTP 消息接口
- PSR 12 - 扩展编码风格指南 - (仍为草案)
标准检查
PHP Code Sniffer 指出潜在的编码标准问题。
vendor/bin/phpcs
PHP CS 默认将使用 phpcs.xml.dist
中的配置。
要查看正在运行的嗅探器,请添加 "-s"。
单元测试
PHPUnit 已安装用于单元测试(测试在 tests
目录中)
运行单元测试: vendor/bin/phpunit
要查看已运行的测试列表: vendor/bin/phpunit --tap
要限制运行的测试: vendor/bin/phpunit --filter 'MiddlewareCors\\Exceptions\\BadOrigin'
或者
vendor/bin/phpunit --filter 'ExceptionTest'
测试包含 "Exception" 的所有测试(例如): vendor/bin/phpunit --filter '(ExceptionTest::testEverything|ExceptionTest::testStub)'
测试 ExceptionTest 类中的两个 testEverything 和 testStub 方法(例如)。
许可证/License
许可协议为 MIT 许可。有关完整信息,请参阅 LICENSE.md。
Bairwell/MiddlewareCors 版权所有 © Bairwell Ltd/Richard Bairwell 2016。
支持开发
您可以通过多种方式帮助支持此库的开发
- 通过 Patreon 进行每月捐款的 "赞助"。
- 报告问题
- 通过 Github 进行更新
- 传播信息。
- 只需通过 Twitter 或通过 Bairwell Ltd 告诉我您对此的看法即可。