Skip to content

Commit dd9ce78

Browse files
committed
minor #11574 [Security] Tell about request_matcher (MatTheCat)
This PR was submitted for the master branch but it was merged into the 3.4 branch instead (closes #11574). Discussion ---------- [Security] Tell about request_matcher Fixes #11564 Commits ------- 51a1644 Tell about request_matcher
2 parents f1714eb + 51a1644 commit dd9ce78

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,26 @@
44
How to Restrict Firewalls to a Specific 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 incoming request.
11+
12+
Restricting by Configuration
13+
----------------------------
14+
15+
Most of the time you don't need to create matchers yourself as Symfony can do it for you based on the
16+
firewall configuration.
1017

1118
.. note::
1219

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

16-
Restricting by Pattern
17-
----------------------
23+
Restricting by Path
24+
~~~~~~~~~~~~~~~~~~~
1825

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

2229
.. configuration-block::
@@ -65,12 +72,12 @@ matches the configured ``pattern``.
6572
]);
6673
6774
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
75+
activated if the path starts (due to the ``^`` regex character) with ``/admin``. If
76+
the path does not match this pattern, the firewall will not be activated and subsequent
7077
firewalls will have the opportunity to be matched for this request.
7178

7279
Restricting by Host
73-
-------------------
80+
~~~~~~~~~~~~~~~~~~~
7481

7582
If matching against the ``pattern`` only is not enough, the request can also be matched against
7683
``host``. When the configuration option ``host`` is set, the firewall will be restricted to
@@ -129,7 +136,7 @@ and subsequent firewalls will have the opportunity to be matched for this
129136
request.
130137

131138
Restricting by HTTP Methods
132-
---------------------------
139+
~~~~~~~~~~~~~~~~~~~~~~~~~~~
133140

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

0 commit comments

Comments
 (0)