mitsuki/cors 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

mitsuki/cors

最新稳定版本:v1.0.0

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.response event 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.55192.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

统计信息

  • 总下载量: 0
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 6
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-02-22

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固