How to Restrict Firewalls to a Request

How to Restrict Firewalls to a Request

When using the Security component, firewalls will decide whether they handle a request based on the result of a request matcher: the first firewall matching the request will handle it.

The last firewall can be configured without any matcher to handle every incoming request.

Restricting by Configuration

Most of the time you don’t need to create matchers yourself as Symfony can do it for you based on the firewall configuration.

Note

You can use any of the following restrictions individually or mix them together to get your desired firewall configuration.

Restricting by Path

This is the default restriction and restricts a firewall to only be initialized if the request path matches the configured pattern.

  • YAML

    1. # config/packages/security.yaml
    2. # ...
    3. security:
    4. firewalls:
    5. secured_area:
    6. pattern: ^/admin
    7. # ...
  • 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="secured_area" pattern="^/admin">
    11. <!-- ... -->
    12. </firewall>
    13. </config>
    14. </srv:container>
  • PHP

    1. // config/packages/security.php
    2. // ...
    3. $container->loadFromExtension('security', [
    4. 'firewalls' => [
    5. 'secured_area' => [
    6. 'pattern' => '^/admin',
    7. // ...
    8. ],
    9. ],
    10. ]);

The pattern is a regular expression. In this example, the firewall will only be activated if the path starts (due to the ^ regex character) with /admin. If the path does not match this pattern, the firewall will not be activated and subsequent firewalls will have the opportunity to be matched for this request.

Restricting by Host

If matching against the pattern only is not enough, the request can also be matched against host. When the configuration option host is set, the firewall will be restricted to only initialize if the host from the request matches against the configuration.

  • YAML

    1. # config/packages/security.yaml
    2. # ...
    3. security:
    4. firewalls:
    5. secured_area:
    6. host: ^admin\.example\.com$
    7. # ...
  • 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="secured_area" host="^admin\.example\.com$">
    11. <!-- ... -->
    12. </firewall>
    13. </config>
    14. </srv:container>
  • PHP

    1. // config/packages/security.php
    2. // ...
    3. $container->loadFromExtension('security', [
    4. 'firewalls' => [
    5. 'secured_area' => [
    6. 'host' => '^admin\.example\.com$',
    7. // ...
    8. ],
    9. ],
    10. ]);

The host (like the pattern) is a regular expression. In this example, the firewall will only be activated if the host is equal exactly (due to the ^ and $ regex characters) to the hostname admin.example.com. If the hostname does not match this pattern, the firewall will not be activated and subsequent firewalls will have the opportunity to be matched for this request.

Restricting by HTTP Methods

The configuration option methods restricts the initialization of the firewall to the provided HTTP methods.

  • YAML

    1. # config/packages/security.yaml
    2. # ...
    3. security:
    4. firewalls:
    5. secured_area:
    6. methods: [GET, POST]
    7. # ...
  • 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="secured_area" methods="GET,POST">
    11. <!-- ... -->
    12. </firewall>
    13. </config>
    14. </srv:container>
  • PHP

    1. // config/packages/security.php
    2. // ...
    3. $container->loadFromExtension('security', [
    4. 'firewalls' => [
    5. 'secured_area' => [
    6. 'methods' => ['GET', 'POST'],
    7. // ...
    8. ],
    9. ],
    10. ]);

In this example, the firewall will only be activated if the HTTP method of the request is either GET or POST. If the method is not in the array of the allowed methods, the firewall will not be activated and subsequent firewalls will again have the opportunity to be matched for this request.

Restricting by Service

If the above options don’t fit your needs you can configure any service implementing Symfony\Component\HttpFoundation\RequestMatcherInterface as request_matcher.

  • YAML

    1. # config/packages/security.yaml
    2. # ...
    3. security:
    4. firewalls:
    5. secured_area:
    6. request_matcher: app.firewall.secured_area.request_matcher
    7. # ...
  • 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="secured_area" request-matcher="app.firewall.secured_area.request_matcher">
    11. <!-- ... -->
    12. </firewall>
    13. </config>
    14. </srv:container>
  • PHP

    1. // config/packages/security.php
    2. // ...
    3. $container->loadFromExtension('security', [
    4. 'firewalls' => [
    5. 'secured_area' => [
    6. 'request_matcher' => 'app.firewall.secured_area.request_matcher',
    7. // ...
    8. ],
    9. ],
    10. ]);

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