wernerdweight/cors-bundle

Symfony扩展包,用于处理跨源资源共享(CORS)头部。

安装: 938

依赖者: 0

建议者: 1

安全性: 0

星星: 2

关注者: 2

分支: 0

开放问题: 0

类型:symfony-bundle

2.0.0 2023-02-21 16:43 UTC

This package is auto-updated.

Last update: 2024-09-21 20:25:52 UTC


README

Symfony扩展包,用于处理跨源资源共享(CORS)头部。

Build Status Latest Stable Version Total Downloads License

安装

1. 使用Composer下载

composer require wernerdweight/cors-bundle

2. 启用扩展包

在您的Kernel中启用扩展包

    <?php
    // config/bundles.php
    return [
        // ...
        WernerDweight\CORSBundle\CORSBundle::class => ['all' => true],
    ];

配置

所有配置键都是可选的,默认值已在注释中说明。

# config/packages/cors.yaml
cors:
    access_control:
        # if true, Access-Control-Allow-Credentials header will be present and set to true
        allow_credentials: true # default false
        
        # list of allowed origins (present in Access-Control-Allow-Origin header)
        allow_origin:           # default none
            - '*'               # all origins or list specific origins (see next line)
            - 'https://my-origin.com'
        
        # list of allowed headers (present in Access-Control-Allow-Headers header)
        allow_headers:          # default none
            - 'Authorization'
            - 'Content-Type'
            - 'Cache-Control'
            - 'X-Requested-With'
        
        # list of exposed headers (present in Access-Control-Expose-Headers header)
        expose_headers:         # default none
            - 'Content-Disposition'
    
    # list of controllers to target
    target_controllers:         # default 'WernerDweight\CORSBundle\Contrtoller\CORSControllerInterface'
        - '*'                   # all controllers or list specific controllers (see next line)
        - 'My\Controller\SomeInterface'
        - 'Vendor\Bundle\Controller\SomeOtherInterface'

目标控制器

所有实现WernerDweight\CORSBundle\Controller\CORSControllerInterface的控制器将自动被选中(无需配置)。

如果您无法修改控制器(例如,它是供应商代码),您可以指定供应商控制器实现的接口(请注意,它也可能由其他控制器实现),或者指定控制器的类本身。

如果您想选择所有控制器,请使用*作为配置值target_controllers

使用

像往常一样使用您的API,您预先配置的头部将在指定控制器的响应中。

事件

以下事件被分发,因此您可以在过程中挂钩。有关如何使用事件的详细信息,请参阅官方Symfony文档

PreflightRequestInterceptedEvent (wds.cors_bundle.preflight_request_intercepted)
在返回预检请求的响应之前发出。包含请求和响应。您可能希望更新响应以包含一些任意头部或完全替换它(默认响应是一个简单的200: OK响应,带有配置的头部)。

GetResponseHeadersEvent (wds.cors_bundle.get_response_headers)
在将响应头部设置到响应之前发出。包含请求和头部(数组)。您可能想设置一些自定义头部。

许可证

此扩展包受MIT许可证的约束。请在扩展包的根目录中查看完整的许可证。