Skip to content

How to Use Multiple Guard Authenticators

Warning: You are browsing the documentation for Symfony 3.x, which is no longer maintained.

Read the updated version of this page for Symfony 7.1 (the current stable version).

The Guard authentication component allows you to use many different authenticators at a time.

An entry point is a service id (of one of your authenticators) whose start() method is called to start the authentication process.

Multiple Authenticators with Shared Entry Point

Sometimes you want to offer your users different authentication mechanisms like a form login and a Facebook login while both entry points redirect the user to the same login page. However, in your configuration you have to explicitly say which entry point you want to use.

This is how your security configuration can look in action:

1
2
3
4
5
6
7
8
9
10
11
# app/config/security.yml
security:
    # ...
    firewalls:
        default:
            anonymous: ~
            guard:
                authenticators:
                    - AppBundle\Security\LoginFormAuthenticator
                    - AppBundle\Security\FacebookConnectAuthenticator
                entry_point: AppBundle\Security\LoginFormAuthenticator

There is one limitation with this approach - you have to use exactly one entry point.

Multiple Authenticators with Separate Entry Points

However, there are use cases where you have authenticators that protect different parts of your application. For example, you have a login form that protects the secured area of your application front-end and API end points that are protected with API tokens. As you can only configure one entry point per firewall, the solution is to split the configuration into two separate firewalls:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# app/config/security.yml
security:
    # ...
    firewalls:
        api:
            pattern: ^/api/
            guard:
                authenticators:
                    - AppBundle\Security\ApiTokenAuthenticator
        default:
            anonymous: ~
            guard:
                authenticators:
                    - AppBundle\Security\LoginFormAuthenticator
    access_control:
        - { path: '^/login', roles: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: '^/api', roles: ROLE_API_USER }
        - { path: '^/', roles: ROLE_USER }
This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.
TOC
    Version