softspring / response-headers
此组件为 Symfony 项目提供响应头配置
Requires
- php: >=8.1
- symfony/event-dispatcher: ^5.4|^6.0|^7.0
- symfony/http-foundation: ^5.4|^6.0|^7.0
Requires (Dev)
- friendsofphp/php-cs-fixer: 3.64.*
- phpstan/phpstan: ^1.10
- phpunit/phpunit: ^9.5 || ^10.0
- rector/rector: ^1.0
- symfony/expression-language: ^5.4|^6.0|^7.0
- symfony/http-kernel: ^5.4|^6.0|^7.0
- 5.3.x-dev
- v5.3.0-beta2
- v5.3.0-beta1
- v5.3.0-alpha21
- 5.2.x-dev
- v5.2.7
- v5.2.6
- v5.2.5
- v5.2.4
- v5.2.3
- v5.2.2
- v5.2.1
- v5.2.0
- v5.2.0-rc10
- v5.2.0-rc9
- v5.2.0-rc8
- v5.2.0-rc7
- v5.2.0-rc6
- v5.2.0-rc5
- v5.2.0-rc4
- v5.2.0-rc1
- 5.1.x-dev
- v5.1.25
- v5.1.24
- v5.1.23
- v5.1.22
- v5.1.21
- v5.1.20
- v5.1.19
- v5.1.18
- v5.1.17
- v5.1.16
- v5.1.15
- v5.1.14
- v5.1.13
- v5.1.12
- v5.1.11
- v5.1.10
- v5.1.9
- v5.1.8
- v5.1.7
- v5.1.6
- v5.1.5
- v5.1.2
- v5.1.1
- v5.1.0
- v5.0.6
- v5.0.5
- v5.0.4
- v5.0.3
- v5.0.2
- v5.0.1
- v5.0.0
- v5.0.0-rc1
- v5.0.0-beta1
- v5.0.0-alpha4
- v5.0.0-alpha3
- v5.0.0-alpha2
- v5.0.0-alpha1
- dev-dependabot/composer/friendsofphp/php-cs-fixer-3.60.staror-3.64.star
- dev-detached
This package is auto-updated.
Last update: 2024-09-25 14:22:37 UTC
README
此组件专为 Symfony 开发,允许通过配置来设置响应头。
安装
使用 Symfony Flex 的应用程序
打开命令行控制台,进入您的项目目录并执行
$ composer require softspring/response-headers
基本配置
创建配置文件
# config/packages/response_headers.yaml parameters: response_headers: X-Frame-Options: "SAMEORIGIN" X-Content-Type-Options: "nosniff" services: Softspring\Component\ResponseHeaders\EventListener\ResponseHeadersListener: tags: ['kernel.event_subscriber'] arguments: $headers: '%response_headers%'
使用条件
您可以为应用响应头设置一些条件。
配置服务
此功能需要表达式语言组件
$ composer require symfony/expression-language
然后您必须配置表达式语言服务
# config/packages/response_headers.yaml parameters: response_headers_global_conditions: [] response_headers: ... services: softspring.response_headers.expression_language: class: Symfony\Component\ExpressionLanguage\ExpressionLanguage arguments: - '@?Psr\Cache\CacheItemPoolInterface' Softspring\Component\ResponseHeaders\EventListener\ResponseHeadersListener: tags: ['kernel.event_subscriber'] arguments: $headers: '%response_headers%' $expressionLanguage: '@softspring.response_headers.expression_language' $globalConditions: '%response_headers_global_conditions%'
定义条件
现在您可以在应用响应头之前设置一个条件
# config/packages/response_headers.yaml parameters: response_headers: X-Frame-Options: value: "SAMEORIGIN" condition: "request.getPathInfo() matches '^/admin'" Access-Control-Allow-Origin: value: "*" condition: "request.getPathInfo() matches '^/api'"
定义全局条件
您还可以为所有头部设置全局条件
# config/packages/response_headers.yaml parameters: response_headers_global_conditions: - 'isMainRequest'
此全局条件建议用于避免为子请求设置头部,但这不是强制的。
构建条件
对于条件,可用 request 和 response 对象。还定义了一个 isMainRequest 变量。
查看 Symfony expression-language 文档。
头部配置参考
定义头部有几种方法
单值头部
# config/packages/response_headers.yaml parameters: response_headers: X-Frame-Options: "SAMEORIGIN"
此代码生成 x-frame-options: "SAMEORIGIN" 头部。
多值头部
多值头部将被合并为单个由分号分隔的字符串
# config/packages/response_headers.yaml parameters: response_headers: Feature-Policy: - "geolocation 'self'" - "vibrate 'none'"
此代码生成 feature-policy: "geolocation 'self'; vibrate 'none'" 头部。
值字段
您还可以在 value 字段中定义值
# config/packages/response_headers.yaml parameters: response_headers: X-Frame-Options: value: "SAMEORIGIN" Feature-Policy: value: - "geolocation 'self'" - "vibrate 'none'"
如果想要设置条件或替换行为,则此 value 字段是必须的。
条件
如前所述,头部可以通过条件进行限制
# config/packages/response_headers.yaml parameters: response_headers: X-Frame-Options: value: "SAMEORIGIN" condition: "request.getHost() == 'api.mydomain.com"
替换行为
Symfony 响应允许定义是否必须替换之前定义的头部值。
默认情况下,此替换行为被定义为 true。但您可以使用以下方式禁用它
# config/packages/response_headers.yaml parameters: response_headers: X-Frame-Options: value: "SAMEORIGIN" replace: false
常见安全头部
这是一个定义常见安全头部的示例
# config/packages/response_headers.yaml parameters: response_headers_global_conditions: - 'isMainRequest' response_headers: X-XSS-Protection: - "1" - "mode=block" X-Frame-Options: "SAMEORIGIN" X-Content-Type-Options: "nosniff" Strict-Transport-Security: - "max-age=31536000" - "includeSubDomains" Referrer-Policy: "same-origin" Feature-Policy: - "geolocation 'self'" - "vibrate 'none'" # ... include every feature the application uses. Content-Security-Policy: - "default-src 'none'" - "img-src 'self'" - "font-src 'self'" - "manifest-src 'self'" - "frame-src 'self'" - "script-src 'self' 'unsafe-inline'" - "style-src 'self' 'unsafe-inline'" - "connect-src 'self'"
查看 Content-Security-Policy 以包含您使用的每个基础 URL 的服务。还尝试避免 unsafe-inline 配置,这取决于您的项目。
许可证
此包在 MIT 许可证下。请参阅包中的完整许可证文件。