MikeFrancis/laravel-secureheaders

Laravel 的安全头部包装器。

v1.4.2 2021-05-19 09:57 UTC

This package is auto-updated.

Last update: 2024-09-07 17:53:23 UTC


README

Packagist Build Status codecov

Laravel 的安全头部包装器。

基于 aidantwoods/SecureHeaders.

安装

在您的 composer.json 中要求 mikefrancis/laravel-secureheaders 包并更新依赖项

composer require mikefrancis/laravel-secureheaders

如果您使用的是 Laravel 5.5+,则已启用包发现。对于 Laravel 5.4,请将服务提供者添加到您的 config/app.php 提供者数组中

MikeFrancis\LaravelSecureHeaders\ServiceProvider::class,

用法

要将更多安全头部添加到整个应用程序中,请在 app/Http/Kernel.php 类的 $middleware 属性中添加 ApplySecureHeaders 中间件

protected $middleware = [
    // ...
    \MikeFrancis\LaravelSecureHeaders\ApplySecureHeaders::class,
];

配置

config/secure-headers.php 中已设置了一些合理的默认值,但如果你想更改这些值,请使用以下命令将文件复制到您自己的应用程序的配置中

php artisan vendor:publish --provider="MikeFrancis\LaravelSecureHeaders\ServiceProvider"

一个典型的配置可能如下所示

<?php

return [
    // Safe Mode
    'safeMode' => false,

    // HSTS Strict-Transport-Security
    'hsts' => [
        'enabled' => true,
    ],

    // Content Security Policy
    'csp' => [
        'default' => [
            'self',
        ],
        'img-src' => [
            '*', // Allow images from anywhere
        ],
        'style-src' => [
            'self',
            'unsafe-inline', // Allow inline styles
            'https://fonts.googleapis.com', // Allow stylesheets from Google Fonts
        ],
        'font-src' => [
            'self',
            'https://fonts.gstatic.com', // Allow fonts from the Google Fonts CDN
        ],
    ],
];

有关内容安全策略指令及其值的完整参考,请参阅 content-security-policy.com.