mitsuki/cors
Composer 安装命令:
composer require mitsuki/cors
包简介
Official CORS listener for the Mitsuki PHP framework with IP whitelisting support
README 文档
README
Official CORS listener for the Mitsuki PHP framework with configurable IP whitelisting support. Production-ready for secure REST APIs, microservices, and mobile backends.
✨ Features
- Standard CORS headers support (Origin, Methods, Headers, Credentials, Max-Age)
- Configurable IP whitelisting via
.env(exact IPs + CIDR ranges:192.168.1.0/24) - Main request only (
isMainRequest()validation) - Symfony
kernel.responseevent listener - Production-ready CIDR validation (
ip2long+ subnet mask) - Zero runtime dependencies beyond core Mitsuki contracts
📦 Installation
composer require mitsuki/cors
Production Requirements:
- PHP
^8.1 mitsuki/listener-contracts:^1.0
Development Dependencies:
mitsuki/http:^1.0(unit tests only)pestphp/pest:^4.4(testing)
📋 Usage Examples
Development (Allow All)
CORS_ALLOWED_IPS=""
Production Security
# Localhost + Docker + VPS range CORS_ALLOWED_IPS="127.0.0.1,::1,192.168.1.0/24,10.96.0.0/12"
Generated Response Headers
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET, POST, PUT, DELETE, PATCH, OPTIONS
Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, Authorization
Access-Control-Allow-Credentials: true
Access-Control-Max-Age: 86400
🧪 Testing
# Install dev dependencies (including mitsuki/http for tests) composer install # Run full test suite ./vendor/bin/pest # Run specific tests ./vendor/bin/pest tests/Unit/CorsListenerTest.php
Test Coverage:
- Main vs sub-request handling
- Exact IP matching (
127.0.0.1) - CIDR range validation (
192.168.1.55∈192.168.1.0/24) - Fallback behavior (empty config = allow all)
- Full CORS headers verification
🏗️ Architecture
Mitsuki\Listeners\CorsListener implements ListenerResponseInterface
├── __construct(array $allowedIps = [])
├── onKernelResponse(ResponseEvent $event)
│ ├── if (!$event->isMainRequest()) return
│ ├── $clientIp = $request->getClientIp()
│ ├── if (!$this->isIpAllowed($clientIp)) return
│ └── $response->headers->set() // CORS headers
├── isIpAllowed(string $ip): bool
│ └── foreach($allowedIps) { CIDR/exact match }
└── ipInCidr(string $ip, string $cidr): bool
└── ip2long() + subnet mask logic
🔧 Advanced Usage
IPv6 Support
CORS_ALLOWED_IPS="2001:db8::/32,::1,127.0.0.1"
Custom Headers/Methods
// Extend the listener class CustomCorsListener extends CorsListener { protected function setCorsHeaders(Response $response): void { parent::setCorsHeaders($response); $response->headers->set('Access-Control-Allow-Headers', 'X-API-Key,Authorization'); } }
🎯 Perfect For
- REST APIs → Flutter/React/Vue SPAs
- Microservices → Docker/Kubernetes networking
- Secure deployments → VPS/hosting providers
- JWT/OAuth2 → API authentication flows
📁 Repository Structure
mitsuki/cors/
├── src/
│ └── CorsListener.php
├── tests/
│ └── Unit/
│ └── CorsListenerTest.php
├── composer.json
├── README.md
└── LICENSE
📄 License
MIT License © 2026 ZGenius Matondo
Made with ❤️ for the Mitsuki PHP framework
mitsuki/cors 适用场景与选型建议
mitsuki/cors 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 0 次下载、GitHub Stars 达 1, 最近一次更新时间为 2026 年 02 月 22 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「symfony」 「http」 「security」 「cors」 「listener」 「micro-framework」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 mitsuki/cors 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 mitsuki/cors 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 mitsuki/cors 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
repository php library
The bundle for easy using json-rpc api on your project
Provide a way to secure accesses to all routes of an symfony application.
It's a barebone security class written on PHP
Bundle Symfony DaplosBundle
Contao CMS integrity check for some files
统计信息
- 总下载量: 0
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 29
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-02-22