Skip to content

Commit c5e5bf5

Browse files
committed
Tell about request_matcher
1 parent d5f5464 commit c5e5bf5

File tree

1 file changed

+69
-11
lines changed

1 file changed

+69
-11
lines changed

security/firewall_restriction.rst

Lines changed: 69 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,77 @@
44
How to Restrict Firewalls to a Request
55
======================================
66

7-
When using the Security component, you can create firewalls that match certain request options.
8-
In most cases, matching against the URL is sufficient, but in special cases, you can further
9-
restrict the initialization of a firewall against other options of the request.
7+
When using the Security component, firewalls will decide whether they handle a request based on the
8+
result of a request matcher: the first firewall matching the request will handle it.
9+
10+
The last firewall can be configured without any matcher to handle every request it sees.
11+
12+
Restricting by Service
13+
----------------------
14+
15+
You can configure any service implementing ``Symfony\Component\HttpFoundation\RequestMatcherInterface``
16+
as ``request_matcher``.
17+
18+
.. configuration-block::
19+
20+
.. code-block:: yaml
21+
22+
# config/packages/security.yaml
23+
24+
# ...
25+
security:
26+
firewalls:
27+
secured_area:
28+
request_matcher: app.firewall.secured_area.request_matcher
29+
# ...
30+
31+
.. code-block:: xml
32+
33+
<!-- config/packages/security.xml -->
34+
<?xml version="1.0" encoding="UTF-8"?>
35+
<srv:container xmlns="http://symfony.com/schema/dic/security"
36+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
37+
xmlns:srv="http://symfony.com/schema/dic/services"
38+
xsi:schemaLocation="http://symfony.com/schema/dic/services
39+
https://symfony.com/schema/dic/services/services-1.0.xsd">
40+
41+
<config>
42+
<!-- ... -->
43+
<firewall name="secured_area" request-matcher="app.firewall.secured_area.request_matcher">
44+
<!-- ... -->
45+
</firewall>
46+
</config>
47+
</srv:container>
48+
49+
.. code-block:: php
50+
51+
// config/packages/security.php
52+
53+
// ...
54+
$container->loadFromExtension('security', [
55+
'firewalls' => [
56+
'secured_area' => [
57+
'request_matcher' => 'app.firewall.secured_area.request_matcher',
58+
// ...
59+
],
60+
],
61+
]);
62+
63+
However in most cases you don’t need to create these matchers yourself as Symfony can do it for you based
64+
on the firewalls’ configuration.
65+
66+
Restricting by Configuration
67+
----------------------------
1068

1169
.. note::
1270

13-
You can use any of these restrictions individually or mix them together to get
71+
You can use any of the following restrictions individually or mix them together to get
1472
your desired firewall configuration.
1573

16-
Restricting by Pattern
17-
----------------------
74+
Restricting by Path
75+
~~~~~~~~~~~~~~~~~~~
1876

19-
This is the default restriction and restricts a firewall to only be initialized if the request URL
77+
This is the default restriction and restricts a firewall to only be initialized if the request path
2078
matches the configured ``pattern``.
2179

2280
.. configuration-block::
@@ -65,12 +123,12 @@ matches the configured ``pattern``.
65123
]);
66124
67125
The ``pattern`` is a regular expression. In this example, the firewall will only be
68-
activated if the URL starts (due to the ``^`` regex character) with ``/admin``. If
69-
the URL does not match this pattern, the firewall will not be activated and subsequent
126+
activated if the path starts (due to the ``^`` regex character) with ``/admin``. If
127+
the path does not match this pattern, the firewall will not be activated and subsequent
70128
firewalls will have the opportunity to be matched for this request.
71129

72130
Restricting by Host
73-
-------------------
131+
~~~~~~~~~~~~~~~~~~~
74132

75133
If matching against the ``pattern`` only is not enough, the request can also be matched against
76134
``host``. When the configuration option ``host`` is set, the firewall will be restricted to
@@ -129,7 +187,7 @@ and subsequent firewalls will have the opportunity to be matched for this
129187
request.
130188

131189
Restricting by HTTP Methods
132-
---------------------------
190+
~~~~~~~~~~~~~~~~~~~~~~~~~~~
133191

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

0 commit comments

Comments
 (0)