中间件,用于将请求重定向到 https 并添加 Strict-Transport-Security 头部

v2.0.2 2024-01-12 17:53 UTC

This package is auto-updated.

Last update: 2024-09-12 19:24:42 UTC


README

Latest Version on Packagist Software License Testing Total Downloads

如果请求是 http,则将中间件重定向到 https 并添加 严格传输安全 头部以保护免受协议降级攻击和 cookie 窃取。

需求

安装

此软件包可以通过 Composer 以 middlewares/https 的方式安装和自动加载。

composer require middlewares/https

示例

$dispatcher = new Dispatcher([
	(new Middlewares\Https())
		->includeSubdomains()
]);

$response = $dispatcher->dispatch(new ServerRequest());

用法

此中间件接受一个 Psr\Http\Message\ResponseFactoryInterface 作为构造函数参数,用于创建重定向响应。如果未定义,则将使用 Middleware\Utils\Factory 自动检测。

$responseFactory = new MyOwnResponseFactory();

//Detect the response factory automatically
$https = new Middlewares\Https();

//Use a specific factory
$htts = new Middlewares\Https($responseFactory);

maxAge

此选项允许定义 Strict-Transport-Security 头部中 max-age 指令的值。默认为 31536000(1 年)。

$threeYears = 31536000 * 3;

$https = (new Middlewares\Https())->maxAge($threeYears);

includeSubdomains

默认情况下,不包含 includeSubDomains 指令在 Strict-Transport-Security 头部中。使用此功能可以更改此行为。

$https = (new Middlewares\Https())->includeSubdomains();

preload

默认情况下,不包含 preload 指令在 Strict-Transport-Security 头部中。使用此功能可以更改此行为。

$https = (new Middlewares\Https())->preload();

checkHttpsForward

启用此选项将忽略包含 X-Forwarded-Proto: httpsX-Forwarded-Port: 443 头部的请求。如果网站位于 https 负载均衡器后面,则特别有用。

$https = (new Middlewares\Https())->checkHttpsForward();

redirect

此选项返回从 httphttps 的重定向响应。默认启用。

//Disable redirections
$https = (new Middlewares\Https())->redirect(false);

请参阅 变更日志 了解有关最近更改的更多信息,以及 贡献指南 了解贡献细节。

MIT 许可证 (MIT)。请参阅 许可证 了解更多信息。