Symfony
sponsored by SensioLabs
Menu
  • About
  • Documentation
  • Screencasts
  • Cloud
  • Certification
  • Community
  • Businesses
  • News
  • Download
  1. Home
  2. Documentation
  3. Security
  4. How to Restrict Firewalls to a Specific Request
  • Documentation
  • Book
  • Reference
  • Bundles
  • Cloud
Search by Algolia

Table of Contents

  • Restricting by Pattern
  • Restricting by Host
  • Restricting by HTTP Methods

How to Restrict Firewalls to a Specific Request

Edit this page

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

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

How to Restrict Firewalls to a Specific Request

When using the Security component, you can create firewalls that match certain request options. In most cases, matching against the URL is sufficient, but in special cases you can further
restrict the initialization of a firewall against other options of the request.

Note

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

Restricting by Pattern

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

  • YAML
  • XML
  • PHP
1
2
3
4
5
6
7
8
# app/config/security.yml

# ...
security:
    firewalls:
        secured_area:
            pattern: ^/admin
            # ...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<!-- 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
        http://symfony.com/schema/dic/services/services-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
// app/config/security.php

// ...
$container->loadFromExtension('security', array(
    'firewalls' => array(
        'secured_area' => array(
            'pattern' => '^/admin',
            // ...
        ),
    ),
));

The pattern is a regular expression. In this example, the firewall will only be
activated if the URL starts (due to the ^ regex character) with /admin. If the URL 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
  • XML
  • PHP
1
2
3
4
5
6
7
8
# app/config/security.yml

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

// ...
$container->loadFromExtension('security', array(
    'firewalls' => array(
        'secured_area' => array(
            '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.

  • YAML
  • XML
  • PHP
1
2
3
4
5
6
7
8
# app/config/security.yml

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

// ...
$container->loadFromExtension('security', array(
    'firewalls' => array(
        'secured_area' => array(
            'methods' => array('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.

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.
We stand with Ukraine.
Version:
Check Code Performance in Dev, Test, Staging & Production

Check Code Performance in Dev, Test, Staging & Production

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).

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

Avatar of roromix, a Symfony contributor

Thanks roromix for being a Symfony contributor

2 commits • 24 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