Skip to content

Commit aa2f1e5

Browse files
committed
Merge branch '3.4' into 4.2
* 3.4: Tell about request_matcher
2 parents 4fe8ad0 + 06b7cb0 commit aa2f1e5

File tree

1 file changed

+73
-13
lines changed

1 file changed

+73
-13
lines changed

security/firewall_restriction.rst

+73-13
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,29 @@
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
8+
request based on the result of a request matcher: the first firewall matching
9+
the request will handle it.
10+
11+
The last firewall can be configured without any matcher to handle every incoming
12+
request.
13+
14+
Restricting by Configuration
15+
----------------------------
16+
17+
Most of the time you don't need to create matchers yourself as Symfony can do it
18+
for you based on the firewall configuration.
1019

1120
.. note::
1221

13-
You can use any of these restrictions individually or mix them together to get
14-
your desired firewall configuration.
22+
You can use any of the following restrictions individually or mix them
23+
together to get your desired firewall configuration.
1524

16-
Restricting by Pattern
17-
----------------------
25+
Restricting by Path
26+
~~~~~~~~~~~~~~~~~~~
1827

19-
This is the default restriction and restricts a firewall to only be initialized if the request URL
20-
matches the configured ``pattern``.
28+
This is the default restriction and restricts a firewall to only be initialized
29+
if the request path matches the configured ``pattern``.
2130

2231
.. configuration-block::
2332

@@ -65,12 +74,12 @@ matches the configured ``pattern``.
6574
]);
6675
6776
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
77+
activated if the path starts (due to the ``^`` regex character) with ``/admin``. If
78+
the path does not match this pattern, the firewall will not be activated and subsequent
7079
firewalls will have the opportunity to be matched for this request.
7180

7281
Restricting by Host
73-
-------------------
82+
~~~~~~~~~~~~~~~~~~~
7483

7584
If matching against the ``pattern`` only is not enough, the request can also be matched against
7685
``host``. When the configuration option ``host`` is set, the firewall will be restricted to
@@ -129,7 +138,7 @@ and subsequent firewalls will have the opportunity to be matched for this
129138
request.
130139

131140
Restricting by HTTP Methods
132-
---------------------------
141+
~~~~~~~~~~~~~~~~~~~~~~~~~~~
133142

134143
The configuration option ``methods`` restricts the initialization of the firewall to
135144
the provided HTTP methods.
@@ -183,3 +192,54 @@ In this example, the firewall will only be activated if the HTTP method of the
183192
request is either ``GET`` or ``POST``. If the method is not in the array of the
184193
allowed methods, the firewall will not be activated and subsequent firewalls will again
185194
have the opportunity to be matched for this request.
195+
196+
Restricting by Service
197+
----------------------
198+
199+
If the above options don't fit your needs you can configure any service implementing
200+
:class:`Symfony\\Component\\HttpFoundation\\RequestMatcherInterface` as ``request_matcher``.
201+
202+
.. configuration-block::
203+
204+
.. code-block:: yaml
205+
206+
# config/packages/security.yaml
207+
208+
# ...
209+
security:
210+
firewalls:
211+
secured_area:
212+
request_matcher: app.firewall.secured_area.request_matcher
213+
# ...
214+
215+
.. code-block:: xml
216+
217+
<!-- config/packages/security.xml -->
218+
<?xml version="1.0" encoding="UTF-8"?>
219+
<srv:container xmlns="http://symfony.com/schema/dic/security"
220+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
221+
xmlns:srv="http://symfony.com/schema/dic/services"
222+
xsi:schemaLocation="http://symfony.com/schema/dic/services
223+
https://symfony.com/schema/dic/services/services-1.0.xsd">
224+
225+
<config>
226+
<!-- ... -->
227+
<firewall name="secured_area" request-matcher="app.firewall.secured_area.request_matcher">
228+
<!-- ... -->
229+
</firewall>
230+
</config>
231+
</srv:container>
232+
233+
.. code-block:: php
234+
235+
// config/packages/security.php
236+
237+
// ...
238+
$container->loadFromExtension('security', [
239+
'firewalls' => [
240+
'secured_area' => [
241+
'request_matcher' => 'app.firewall.secured_area.request_matcher',
242+
// ...
243+
],
244+
],
245+
]);

0 commit comments

Comments
 (0)