How to Use Multiple Guard Authenticators

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:

  • YAML

    1. # config/packages/security.yaml
    2. security:
    3. # ...
    4. firewalls:
    5. default:
    6. anonymous: lazy
    7. guard:
    8. authenticators:
    9. - App\Security\LoginFormAuthenticator
    10. - App\Security\FacebookConnectAuthenticator
    11. entry_point: App\Security\LoginFormAuthenticator
  • XML

    1. <!-- config/packages/security.xml -->
    2. <?xml version="1.0" encoding="UTF-8" ?>
    3. <srv:container xmlns="http://symfony.com/schema/dic/security"
    4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    5. xmlns:srv="http://symfony.com/schema/dic/services"
    6. xsi:schemaLocation="http://symfony.com/schema/dic/services
    7. https://symfony.com/schema/dic/services/services-1.0.xsd">
    8. <config>
    9. <!-- ... -->
    10. <firewall name="default">
    11. <anonymous lazy="true"/>
    12. <guard entry-point="App\Security\LoginFormAuthenticator">
    13. <authenticator>App\Security\LoginFormAuthenticator</authenticator>
    14. <authenticator>App\Security\FacebookConnectAuthenticator</authenticator>
    15. </guard>
    16. </firewall>
    17. </config>
    18. </srv:container>
  • PHP

    1. // config/packages/security.php
    2. use App\Security\FacebookConnectAuthenticator;
    3. use App\Security\LoginFormAuthenticator;
    4. $container->loadFromExtension('security', [
    5. // ...
    6. 'firewalls' => [
    7. 'default' => [
    8. 'anonymous' => 'lazy',
    9. 'guard' => [
    10. 'entry_point' => LoginFormAuthenticator::class,
    11. 'authenticators' => [
    12. LoginFormAuthenticator::class,
    13. FacebookConnectAuthenticator::class,
    14. ],
    15. ],
    16. ],
    17. ],
    18. ]);

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:

  • YAML

    1. # config/packages/security.yaml
    2. security:
    3. # ...
    4. firewalls:
    5. api:
    6. pattern: ^/api/
    7. guard:
    8. authenticators:
    9. - App\Security\ApiTokenAuthenticator
    10. default:
    11. anonymous: lazy
    12. guard:
    13. authenticators:
    14. - App\Security\LoginFormAuthenticator
    15. access_control:
    16. - { path: '^/login', roles: IS_AUTHENTICATED_ANONYMOUSLY }
    17. - { path: '^/api', roles: ROLE_API_USER }
    18. - { path: '^/', roles: ROLE_USER }
  • XML

    1. <!-- config/packages/security.xml -->
    2. <?xml version="1.0" encoding="UTF-8" ?>
    3. <srv:container xmlns="http://symfony.com/schema/dic/security"
    4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    5. xmlns:srv="http://symfony.com/schema/dic/services"
    6. xsi:schemaLocation="http://symfony.com/schema/dic/services
    7. https://symfony.com/schema/dic/services/services-1.0.xsd">
    8. <config>
    9. <!-- ... -->
    10. <firewall name="api" pattern="^/api/">
    11. <guard>
    12. <authenticator>App\Security\ApiTokenAuthenticator</authenticator>
    13. </guard>
    14. </firewall>
    15. <firewall name="default">
    16. <anonymous lazy="true"/>
    17. <guard>
    18. <authenticator>App\Security\LoginFormAuthenticator</authenticator>
    19. </guard>
    20. </firewall>
    21. <rule path="^/login" role="IS_AUTHENTICATED_ANONYMOUSLY"/>
    22. <rule path="^/api" role="ROLE_API_USER"/>
    23. <rule path="^/" role="ROLE_USER"/>
    24. </config>
    25. </srv:container>
  • PHP

    1. // config/packages/security.php
    2. use App\Security\ApiTokenAuthenticator;
    3. use App\Security\LoginFormAuthenticator;
    4. $container->loadFromExtension('security', [
    5. // ...
    6. 'firewalls' => [
    7. 'api' => [
    8. 'pattern' => '^/api',
    9. 'guard' => [
    10. 'authenticators' => [
    11. ApiTokenAuthenticator::class,
    12. ],
    13. ],
    14. ],
    15. 'default' => [
    16. 'anonymous' => 'lazy',
    17. 'guard' => [
    18. 'authenticators' => [
    19. LoginFormAuthenticator::class,
    20. ],
    21. ],
    22. ],
    23. ],
    24. 'access_control' => [
    25. ['path' => '^/login', 'roles' => 'IS_AUTHENTICATED_ANONYMOUSLY'],
    26. ['path' => '^/api', 'roles' => 'ROLE_API_USER'],
    27. ['path' => '^/', 'roles' => 'ROLE_USER'],
    28. ],
    29. ]);

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.