How to Customize Access Denied Responses
In Symfony, you can throw an
Symfony\Component\Security\Core\Exception\AccessDeniedException
to disallow access to the user. Symfony will handle this exception and
generates a response based on the authentication state:
If the user is not authenticated (or authenticated anonymously), an authentication entry point is used to generated a response (typically a redirect to the login page or an 401 Unauthorized response);
If the user is authenticated, but does not have the required permissions, a 403 Forbidden response is generated.
Customize the Forbidden Response
Create a class that implements
Symfony\Component\Security\Http\Authorization\AccessDeniedHandlerInterface.
This interface defines one method called handle() where you can
implement whatever logic that should execute when access is denied for the
current user (e.g. send a mail, log a message, or generally return a custom
response):
// src/Security/AccessDeniedHandler.php
namespace App\Security;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use Symfony\Component\Security\Http\Authorization\AccessDeniedHandlerInterface;
class AccessDeniedHandler implements AccessDeniedHandlerInterface
{
public function handle(Request $request, AccessDeniedException $accessDeniedException): ?Response
{
// ...
return new Response($content, 403);
}
}
If you’re using the default services.yaml configuration, you’re done! Symfony will automatically know about your new service. You can then configure it under your firewall:
Customizing All Access Denied Responses
In some cases, you might want to customize both responses or do a specific
action (e.g. logging) for each AccessDeniedException. In this case,
configure a kernel.exception listener:
// src/EventListener/AccessDeniedListener.php
namespace App\EventListener;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
class AccessDeniedListener implements EventSubscriberInterface
{
public static function getSubscribedEvents(): array
{
return [
// the priority must be greater than the Security HTTP
// ExceptionListener, to make sure it's called before
// the default exception listener
KernelEvents::EXCEPTION => ['onKernelException', 2],
];
}
public function onKernelException(ExceptionEvent $event): void
{
$exception = $event->getThrowable();
if (!$exception instanceof AccessDeniedException) {
return;
}
// ... perform some action (e.g. logging)
// optionally set the custom response
$event->setResponse(new Response(null, 403));
// or stop propagation (prevents the next exception listeners from being called)
//$event->stopPropagation();
}
}