Skip to content
  • About
    • What is Symfony?
    • Community
    • News
    • Contributing
    • Support
  • Documentation
    • Symfony Docs
    • Symfony Book
    • Screencasts
    • Symfony Bundles
    • Symfony Cloud
    • Training
  • Services
    • Platform.sh for Symfony Best platform to deploy Symfony apps
    • SymfonyInsight Automatic quality checks for your apps
    • Symfony Certification Prove your knowledge and boost your career
    • SensioLabs Professional services to help you with Symfony
    • Blackfire Profile and monitor performance of your apps
  • Other
  • Blog
  • Download
sponsored by SensioLabs
  1. Home
  2. Documentation
  3. Security
  4. How to Use Multiple Guard Authenticators
  • Documentation
  • Book
  • Reference
  • Bundles
  • Cloud

Table of Contents

  • Multiple Authenticators with Shared Entry Point
  • Multiple Authenticators with Separate Entry Points

How to Use Multiple Guard Authenticators

Edit this page

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

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

How to Use Multiple Guard Authenticators

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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<!-- app/config/security.xml -->
<?xml version="1.0" encoding="UTF-8"?>
<srv:container xmlns="http://symfony.com/schema/dic/security"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:srv="http://symfony.com/schema/dic/services"
    xsi:schemaLocation="http://symfony.com/schema/dic/services
        https://symfony.com/schema/dic/services/services-1.0.xsd">

    <config>
        <!-- ... -->
        <firewall name="default">
            <anonymous/>
            <guard entry-point="AppBundle\Security\LoginFormAuthenticator">
                <authenticator>AppBundle\Security\LoginFormAuthenticator</authenticator>
                <authenticator>AppBundle\Security\FacebookConnectAuthenticator</authenticator>
            </guard>
        </firewall>
    </config>
</srv:container>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// app/config/security.php
use AppBundle\Security\FacebookConnectAuthenticator;
use AppBundle\Security\LoginFormAuthenticator;

$container->loadFromExtension('security', [
    // ...
    'firewalls' => [
        'default' => [
            'anonymous' => null,
            'guard' => [
                'entry_point' => LoginFormAuthenticator::class,
                'authenticators' => [
                    LoginFormAuthenticator::class,
                    FacebookConnectAuthenticator::class,
                ],
            ],
        ],
    ],
]);

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 }
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
<!-- app/config/security.xml -->
<?xml version="1.0" encoding="UTF-8"?>
<srv:container xmlns="http://symfony.com/schema/dic/security"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:srv="http://symfony.com/schema/dic/services"
    xsi:schemaLocation="http://symfony.com/schema/dic/services
        https://symfony.com/schema/dic/services/services-1.0.xsd">

    <config>
        <!-- ... -->
        <firewall name="api" pattern="^/api/">
            <guard>
                <authenticator>AppBundle\Security\ApiTokenAuthenticator</authenticator>
            </guard>
        </firewall>
        <firewall name="default">
            <anonymous/>
            <guard>
                <authenticator>AppBundle\Security\LoginFormAuthenticator</authenticator>
            </guard>
        </firewall>
        <rule path="^/login" role="IS_AUTHENTICATED_ANONYMOUSLY"/>
        <rule path="^/api" role="ROLE_API_USER"/>
        <rule path="^/" role="ROLE_USER"/>
    </config>
</srv:container>
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
// app/config/security.php
use AppBundle\Security\ApiTokenAuthenticator;
use AppBundle\Security\LoginFormAuthenticator;

$container->loadFromExtension('security', [
    // ...
    'firewalls' => [
        'api' => [
            'pattern' => '^/api',
            'guard' => [
                'authenticators' => [
                    ApiTokenAuthenticator::class,
                ],
            ],
        ],
        'default' => [
            'anonymous' => null,
            'guard' => [
                'authenticators' => [
                    LoginFormAuthenticator::class,
                ],
            ],
        ],
    ],
    '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
    We stand with Ukraine.
    Version:
    Make sure your project is risk free

    Make sure your project is risk free

    Measure & Improve Symfony Code Performance

    Measure & Improve Symfony Code Performance

    Symfony footer

    ↓ Our footer now uses the colors of the Ukrainian flag because Symfony stands with the people of Ukraine.

    Avatar of Mardari Dorel, a Symfony contributor

    Thanks Mardari Dorel (@dorumd) for being a Symfony contributor

    4 commits • 326 lines changed

    View all contributors that help us make Symfony

    Become a Symfony contributor

    Be an active part of the community and contribute ideas, code and bug fixes. Both experts and newcomers are welcome.

    Learn how to contribute

    Symfony™ is a trademark of Symfony SAS. All rights reserved.

    • What is Symfony?

      • Symfony at a Glance
      • Symfony Components
      • Case Studies
      • Symfony Releases
      • Security Policy
      • Logo & Screenshots
      • Trademark & Licenses
      • symfony1 Legacy
    • Learn Symfony

      • Symfony Docs
      • Symfony Book
      • Reference
      • Bundles
      • Best Practices
      • Training
      • eLearning Platform
      • Certification
    • Screencasts

      • Learn Symfony
      • Learn PHP
      • Learn JavaScript
      • Learn Drupal
      • Learn RESTful APIs
    • Community

      • SymfonyConnect
      • Support
      • How to be Involved
      • Code of Conduct
      • Events & Meetups
      • Projects using Symfony
      • Downloads Stats
      • Contributors
      • Backers
    • Blog

      • Events & Meetups
      • A week of symfony
      • Case studies
      • Cloud
      • Community
      • Conferences
      • Diversity
      • Documentation
      • Living on the edge
      • Releases
      • Security Advisories
      • SymfonyInsight
      • Twig
      • SensioLabs
    • Services

      • SensioLabs services
      • Train developers
      • Manage your project quality
      • Improve your project performance
      • Host Symfony projects

      Deployed on

    Follow Symfony

    Search by Meilisearch