How to Customize Entry Points and Access Denied Responses
In Symfony, you can throw an AccessDeniedException to disallow access to the user. Symfony will handle this exception and generate a response based on the authentication state:
- If the user is not authenticated (or authenticated anonymously), an authentication entry point is used to generate a response (typically a redirect to the login page or a 401 Unauthorized response);
- If the user is authenticated, but does not have the required permissions, a 403 Forbidden response is generated.
Customize the Unauthorized Response
When an unauthenticated user tries to access a protected page, Symfony
gives them a suitable response to let them start authentication (e.g.
redirect to a login form or show a 401 Unauthorized HTTP response for
APIs). This response is created by the authentication entry point of the
firewall. To customize it, create a class that implements
AuthenticationEntryPointInterface.
This interface has one method (start()) that is called whenever an
unauthenticated user tries to access a protected resource:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
// src/Security/AuthenticationEntryPoint.php
namespace App\Security;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface;
class AuthenticationEntryPoint implements AuthenticationEntryPointInterface
{
public function __construct(
private UrlGeneratorInterface $urlGenerator,
) {
}
public function start(Request $request, ?AuthenticationException $authException = null): RedirectResponse
{
// add a custom flash message and redirect to the login page
$request->getSession()->getFlashBag()->add('note', 'You have to log in to access this page.');
return new RedirectResponse($this->urlGenerator->generate('security_login'));
}
}
If you're using the default services.yaml configuration, the handler is ready to use. Otherwise, you have to register this service in the container.
Now, configure this service ID as the entry point for the firewall:
1 2 3 4 5 6 7
# config/packages/security.yaml
firewalls:
# ...
main:
# ...
entry_point: App\Security\AuthenticationEntryPoint
Configure the Entry Point of a Firewall
A firewall can have multiple ways to authenticate (e.g. both a form login and
a social login). If more than one of its authenticators provides an entry
point, you must choose which one to use with the entry_point setting:
1 2 3 4 5 6 7 8 9 10 11 12
# config/packages/security.yaml
security:
# ...
firewalls:
main:
# allow authentication using a form or a custom authenticator
form_login: ~
custom_authenticators:
- App\Security\SocialConnectAuthenticator
# use the form login as the entry point for unauthenticated users
entry_point: form_login
The entry_point setting accepts the name of one of the authenticators of
the firewall (e.g. form_login) or the service ID of a custom entry point,
like the App\Security\AuthenticationEntryPoint class shown above.
Redirecting Anonymous Users to a Login Page
Some authenticators (e.g. json_login or custom authenticators) don't
redirect anonymous users to a login page. Instead of creating an entry point
class only to return that redirection, set the entry_point_path option of
the firewall:
1 2 3 4 5 6 7 8 9 10 11
# config/packages/security.yaml
security:
# ...
firewalls:
main:
json_login:
check_path: api_login
# a path (/login), an absolute URL (https://example.com/login)
# or a route name where anonymous users are redirected to log in
entry_point_path: app_login
This option accepts the same values as the login_path option of
form_login. It works even if the firewall doesn't define any authenticator,
but you can't use it together with the entry_point option.
Before redirecting, the firewall stores the requested URL in the session, so
you can send users back to it after they log in. This only happens in stateful
firewalls and for safe requests (e.g. GET) that are not AJAX requests.
8.2
The entry_point_path option was introduced in Symfony 8.2.
Multiple Authenticators with Separate Entry Points
Some applications use different authenticators to protect different parts of the application. For example, a login form protects the main website and API keys protect the API endpoints used by external parties.
A firewall can only have one entry point, so split the configuration into two separate firewalls:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
# config/packages/security.yaml
security:
# ...
firewalls:
api:
pattern: ^/api/
custom_authenticators:
- App\Security\ApiTokenAuthenticator
main:
lazy: true
form_login: ~
access_control:
- { path: '^/login', roles: PUBLIC_ACCESS }
- { path: '^/api', roles: ROLE_API_USER }
- { path: '^/', roles: ROLE_USER }
Customize the Forbidden Response
Create a class that implements
AccessDeniedHandlerInterface.
This interface defines one method called handle() where you can
implement any 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):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
// 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:
1 2 3 4 5 6 7
# config/packages/security.yaml
firewalls:
# ...
main:
# ...
access_denied_handler: App\Security\AccessDeniedHandler
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
// 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();
}
}