Skip to content
  • About
    • What is Symfony?
    • Community
    • News
    • Contributing
    • Support
  • Documentation
    • Symfony Docs
    • Symfony Book
    • Screencasts
    • Symfony Bundles
    • Symfony Cloud
    • Training
  • Services
    • SensioLabs Professional services to help you with Symfony
    • 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
    • Blackfire Profile and monitor performance of your apps
  • Other
  • Blog
  • Download
sponsored by SensioLabs
  1. Home
  2. Documentation
  3. Security
  4. How to Restrict Firewalls to a Request
  • Documentation
  • Book
  • Reference
  • Bundles
  • Cloud

Table of Contents

  • Restricting by Configuration
    • Restricting by Path
    • Restricting by Host
    • Restricting by HTTP Methods
  • Restricting by Service

How to Restrict Firewalls to a Request

Edit this page

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.

1
2
3
4
5
6
7
8
# config/packages/security.yaml

# ...
security:
    firewalls:
        secured_area:
            pattern: ^/admin
            # ...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<!-- config/packages/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
        http://symfony.com/schema/dic/security
        https://symfony.com/schema/dic/security/security-1.0.xsd">

    <config>
        <!-- ... -->
        <firewall name="secured_area" pattern="^/admin">
            <!-- ... -->
        </firewall>
    </config>
</srv:container>
1
2
3
4
5
6
7
8
9
10
11
// config/packages/security.php
use Symfony\Config\SecurityConfig;

return static function (SecurityConfig $security) {
    // ....

    $security->firewall('secured_area')
        ->pattern('^/admin')
        // ...
    ;
};

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.

1
2
3
4
5
6
7
8
# config/packages/security.yaml

# ...
security:
    firewalls:
        secured_area:
            host: ^admin\.example\.com$
            # ...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<!-- config/packages/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
        http://symfony.com/schema/dic/security
        https://symfony.com/schema/dic/security/security-1.0.xsd">

    <config>
        <!-- ... -->
        <firewall name="secured_area" host="^admin\.example\.com$">
            <!-- ... -->
        </firewall>
    </config>
</srv:container>
1
2
3
4
5
6
7
8
9
10
11
// config/packages/security.php
use Symfony\Config\SecurityConfig;

return static function (SecurityConfig $security) {
    // ....

    $security->firewall('secured_area')
        ->host('^admin\.example\.com$')
        // ...
    ;
};

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.

1
2
3
4
5
6
7
8
# config/packages/security.yaml

# ...
security:
    firewalls:
        secured_area:
            methods: [GET, POST]
            # ...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<!-- config/packages/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
        http://symfony.com/schema/dic/security
        https://symfony.com/schema/dic/security/security-1.0.xsd">

    <config>
        <!-- ... -->
        <firewall name="secured_area" methods="GET,POST">
            <!-- ... -->
        </firewall>
    </config>
</srv:container>
1
2
3
4
5
6
7
8
9
10
11
// config/packages/security.php
use Symfony\Config\SecurityConfig;

return static function (SecurityConfig $security) {
    // ....

    $security->firewall('secured_area')
        ->methods(['GET', 'POST'])
        // ...
    ;
};

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 RequestMatcherInterface as request_matcher.

1
2
3
4
5
6
7
8
# config/packages/security.yaml

# ...
security:
    firewalls:
        secured_area:
            request_matcher: App\Security\CustomRequestMatcher
            # ...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<!-- config/packages/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
        http://symfony.com/schema/dic/security
        https://symfony.com/schema/dic/security/security-1.0.xsd">

    <config>
        <!-- ... -->
        <firewall name="secured_area" request-matcher="App\Security\CustomRequestMatcher">
            <!-- ... -->
        </firewall>
    </config>
</srv:container>
1
2
3
4
5
6
7
8
9
10
11
12
// config/packages/security.php
use App\Security\CustomRequestMatcher;
use Symfony\Config\SecurityConfig;

return static function (SecurityConfig $security) {
    // ....

    $security->firewall('secured_area')
        ->requestMatcher(CustomRequestMatcher::class)
        // ...
    ;
};
This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.
TOC
    Version
    We stand with Ukraine.
    Version:

    Symfony Security is backed by

    No stress: we've got you covered with our 116 automated quality checks of your code

    No stress: we've got you covered with our 116 automated quality checks of your code

    Be trained by SensioLabs experts (2 to 6 day sessions -- French or English).

    Be trained by SensioLabs experts (2 to 6 day sessions -- French or English).

    Symfony footer

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

    Avatar of Guilliam Xavier, a Symfony contributor

    Thanks Guilliam Xavier for being a Symfony contributor

    12 commits • 97 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 Algolia