Protect routes with URL Signer middleware
mainYou can protect specific routes by applying the ValidateSignature middleware. If a user accesses a protected route with an invalid or expired signature, the application will automatically abort with a 403 Forbidden status code.
1. Register the middleware
Add the middleware to your HTTP Kernel (e.g., in app/Http/Kernel.php):
protected $routeMiddleware = [
'signed-url' => \Spatie\\UrlSigner\\Laravel\\Middleware\\ValidateSignature::class,
];2. Apply to routes Use the middleware alias in your route definitions:
Route::get('protected-route', fn () => 'Hello secret world!')
->middleware('signed-url');// in app/Http/Kernel.php
protected $routeMiddleware = [
'signed-url' => \Spatie\UrlSigner\Laravel\Middleware\ValidateSignature::class,
];
// in routes/web.php
Route::get('protected-route', fn () => 'Hello secret world!')
->middleware('signed-url');