Ajout de sécurité sur le https laravel

This commit is contained in:
2026-03-30 21:59:34 +02:00
parent 0da54295a7
commit a73cd76d86
3 changed files with 34 additions and 2 deletions
+27
View File
@@ -0,0 +1,27 @@
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;
class SecurityHeaders
{
/**
* Handle an incoming request.
*/
public function handle(Request $request, Closure $next): Response
{
$response = $next($request);
// Ajouter les headers de sécurité
$response->headers->set('Strict-Transport-Security', 'max-age=31536000; includeSubDomains', true);
$response->headers->set('X-Content-Type-Options', 'nosniff', true);
$response->headers->set('X-Frame-Options', 'SAMEORIGIN', true);
$response->headers->set('X-XSS-Protection', '1; mode=block', true);
$response->headers->set('Referrer-Policy', 'strict-origin-when-cross-origin', true);
return $response;
}
}
+5 -1
View File
@@ -3,6 +3,7 @@
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\URL;
class AppServiceProvider extends ServiceProvider
{
@@ -19,6 +20,9 @@ class AppServiceProvider extends ServiceProvider
*/
public function boot(): void
{
//
// Forcer HTTPS en production et staging
if (!app()->environment('local')) {
URL::forceScheme('https');
}
}
}
+2 -1
View File
@@ -3,6 +3,7 @@
use Illuminate\Foundation\Application;
use Illuminate\Foundation\Configuration\Exceptions;
use Illuminate\Foundation\Configuration\Middleware;
use App\Http\Middleware\SecurityHeaders;
return Application::configure(basePath: dirname(__DIR__))
->withRouting(
@@ -11,7 +12,7 @@ return Application::configure(basePath: dirname(__DIR__))
health: '/up',
)
->withMiddleware(function (Middleware $middleware): void {
//
$middleware->append(SecurityHeaders::class);
})
->withExceptions(function (Exceptions $exceptions): void {
//