Skip to content

Commit d2e6a64

Browse files
committed
https://github.com/symfony/symfony-docs/pull/11574#discussion_r289199817
1 parent 4994ec1 commit d2e6a64

File tree

1 file changed

+53
-53
lines changed

1 file changed

+53
-53
lines changed

security/firewall_restriction.rst

Lines changed: 53 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,22 @@ result of a request matcher: the first firewall matching the request will handle
99

1010
The last firewall can be configured without any matcher to handle every incoming request.
1111

12-
Restricting by Service
13-
----------------------
12+
Restricting by Configuration
13+
----------------------------
1414

15-
You can configure any service implementing :class:`Symfony\\Component\\HttpFoundation\\RequestMatcherInterface`
16-
as ``request_matcher``.
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.
17+
18+
.. note::
19+
20+
You can use any of the following restrictions individually or mix them together to get
21+
your desired firewall configuration.
22+
23+
Restricting by Path
24+
~~~~~~~~~~~~~~~~~~~
25+
26+
This is the default restriction and restricts a firewall to only be initialized if the request path
27+
matches the configured ``pattern``.
1728

1829
.. configuration-block::
1930

@@ -25,7 +36,7 @@ as ``request_matcher``.
2536
security:
2637
firewalls:
2738
secured_area:
28-
request_matcher: app.firewall.secured_area.request_matcher
39+
pattern: ^/admin
2940
# ...
3041
3142
.. code-block:: xml
@@ -40,7 +51,7 @@ as ``request_matcher``.
4051
4152
<config>
4253
<!-- ... -->
43-
<firewall name="secured_area" request-matcher="app.firewall.secured_area.request_matcher">
54+
<firewall name="secured_area" pattern="^/admin">
4455
<!-- ... -->
4556
</firewall>
4657
</config>
@@ -54,28 +65,23 @@ as ``request_matcher``.
5465
$container->loadFromExtension('security', [
5566
'firewalls' => [
5667
'secured_area' => [
57-
'request_matcher' => 'app.firewall.secured_area.request_matcher',
68+
'pattern' => '^/admin',
5869
// ...
5970
],
6071
],
6172
]);
6273
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 firewall configuration.
65-
66-
Restricting by Configuration
67-
----------------------------
68-
69-
.. note::
70-
71-
You can use any of the following restrictions individually or mix them together to get
72-
your desired firewall configuration.
74+
The ``pattern`` is a regular expression. In this example, the firewall will only be
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
77+
firewalls will have the opportunity to be matched for this request.
7378

74-
Restricting by Path
79+
Restricting by Host
7580
~~~~~~~~~~~~~~~~~~~
7681

77-
This is the default restriction and restricts a firewall to only be initialized if the request path
78-
matches the configured ``pattern``.
82+
If matching against the ``pattern`` only is not enough, the request can also be matched against
83+
``host``. When the configuration option ``host`` is set, the firewall will be restricted to
84+
only initialize if the host from the request matches against the configuration.
7985

8086
.. configuration-block::
8187

@@ -87,7 +93,7 @@ matches the configured ``pattern``.
8793
security:
8894
firewalls:
8995
secured_area:
90-
pattern: ^/admin
96+
host: ^admin\.example\.com$
9197
# ...
9298
9399
.. code-block:: xml
@@ -102,7 +108,7 @@ matches the configured ``pattern``.
102108
103109
<config>
104110
<!-- ... -->
105-
<firewall name="secured_area" pattern="^/admin">
111+
<firewall name="secured_area" host="^admin\.example\.com$">
106112
<!-- ... -->
107113
</firewall>
108114
</config>
@@ -116,23 +122,24 @@ matches the configured ``pattern``.
116122
$container->loadFromExtension('security', [
117123
'firewalls' => [
118124
'secured_area' => [
119-
'pattern' => '^/admin',
125+
'host' => '^admin\.example\.com$',
120126
// ...
121127
],
122128
],
123129
]);
124130
125-
The ``pattern`` is a regular expression. In this example, the firewall will only be
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
128-
firewalls will have the opportunity to be matched for this request.
131+
The ``host`` (like the ``pattern``) is a regular expression. In this example,
132+
the firewall will only be activated if the host is equal exactly (due to
133+
the ``^`` and ``$`` regex characters) to the hostname ``admin.example.com``.
134+
If the hostname does not match this pattern, the firewall will not be activated
135+
and subsequent firewalls will have the opportunity to be matched for this
136+
request.
129137

130-
Restricting by Host
131-
~~~~~~~~~~~~~~~~~~~
138+
Restricting by HTTP Methods
139+
~~~~~~~~~~~~~~~~~~~~~~~~~~~
132140

133-
If matching against the ``pattern`` only is not enough, the request can also be matched against
134-
``host``. When the configuration option ``host`` is set, the firewall will be restricted to
135-
only initialize if the host from the request matches against the configuration.
141+
The configuration option ``methods`` restricts the initialization of the firewall to
142+
the provided HTTP methods.
136143

137144
.. configuration-block::
138145

@@ -144,7 +151,7 @@ only initialize if the host from the request matches against the configuration.
144151
security:
145152
firewalls:
146153
secured_area:
147-
host: ^admin\.example\.com$
154+
methods: [GET, POST]
148155
# ...
149156
150157
.. code-block:: xml
@@ -159,7 +166,7 @@ only initialize if the host from the request matches against the configuration.
159166
160167
<config>
161168
<!-- ... -->
162-
<firewall name="secured_area" host="^admin\.example\.com$">
169+
<firewall name="secured_area" methods="GET,POST">
163170
<!-- ... -->
164171
</firewall>
165172
</config>
@@ -173,24 +180,22 @@ only initialize if the host from the request matches against the configuration.
173180
$container->loadFromExtension('security', [
174181
'firewalls' => [
175182
'secured_area' => [
176-
'host' => '^admin\.example\.com$',
183+
'methods' => ['GET', 'POST'],
177184
// ...
178185
],
179186
],
180187
]);
181188
182-
The ``host`` (like the ``pattern``) is a regular expression. In this example,
183-
the firewall will only be activated if the host is equal exactly (due to
184-
the ``^`` and ``$`` regex characters) to the hostname ``admin.example.com``.
185-
If the hostname does not match this pattern, the firewall will not be activated
186-
and subsequent firewalls will have the opportunity to be matched for this
187-
request.
189+
In this example, the firewall will only be activated if the HTTP method of the
190+
request is either ``GET`` or ``POST``. If the method is not in the array of the
191+
allowed methods, the firewall will not be activated and subsequent firewalls will again
192+
have the opportunity to be matched for this request.
188193

189-
Restricting by HTTP Methods
190-
~~~~~~~~~~~~~~~~~~~~~~~~~~~
194+
Restricting by Service
195+
----------------------
191196

192-
The configuration option ``methods`` restricts the initialization of the firewall to
193-
the provided HTTP methods.
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``.
194199

195200
.. configuration-block::
196201

@@ -202,7 +207,7 @@ the provided HTTP methods.
202207
security:
203208
firewalls:
204209
secured_area:
205-
methods: [GET, POST]
210+
request_matcher: app.firewall.secured_area.request_matcher
206211
# ...
207212
208213
.. code-block:: xml
@@ -217,7 +222,7 @@ the provided HTTP methods.
217222
218223
<config>
219224
<!-- ... -->
220-
<firewall name="secured_area" methods="GET,POST">
225+
<firewall name="secured_area" request-matcher="app.firewall.secured_area.request_matcher">
221226
<!-- ... -->
222227
</firewall>
223228
</config>
@@ -231,13 +236,8 @@ the provided HTTP methods.
231236
$container->loadFromExtension('security', [
232237
'firewalls' => [
233238
'secured_area' => [
234-
'methods' => ['GET', 'POST'],
239+
'request_matcher' => 'app.firewall.secured_area.request_matcher',
235240
// ...
236241
],
237242
],
238243
]);
239-
240-
In this example, the firewall will only be activated if the HTTP method of the
241-
request is either ``GET`` or ``POST``. If the method is not in the array of the
242-
allowed methods, the firewall will not be activated and subsequent firewalls will again
243-
have the opportunity to be matched for this request.

0 commit comments

Comments
 (0)