From 51a1644894d55ef1331d782ad5781cab03a3046d Mon Sep 17 00:00:00 2001 From: MatTheCat Date: Fri, 31 May 2019 09:21:57 +0200 Subject: [PATCH] Tell about request_matcher --- security/firewall_restriction.rst | 80 ++++++++++++++++++++++++++----- 1 file changed, 69 insertions(+), 11 deletions(-) diff --git a/security/firewall_restriction.rst b/security/firewall_restriction.rst index 355b32dea79..3a0278e2592 100644 --- a/security/firewall_restriction.rst +++ b/security/firewall_restriction.rst @@ -4,19 +4,26 @@ 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. +When using the Security component, firewalls will decide whether they handle a request based on the +result of a request matcher: the first firewall matching the request will handle it. + +The last firewall can be configured without any matcher to handle every incoming request. + +Restricting by Configuration +---------------------------- + +Most of the time you don't need to create matchers yourself as Symfony can do it for you based on the +firewall configuration. .. note:: - You can use any of these restrictions individually or mix them together to get + You can use any of the following restrictions individually or mix them together to get your desired firewall configuration. -Restricting by Pattern ----------------------- +Restricting by Path +~~~~~~~~~~~~~~~~~~~ -This is the default restriction and restricts a firewall to only be initialized if the request URL +This is the default restriction and restricts a firewall to only be initialized if the request path matches the configured ``pattern``. .. configuration-block:: @@ -65,12 +72,12 @@ matches the configured ``pattern``. ]); 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 +activated if the path starts (due to the ``^`` regex character) with ``/admin``. If +the path 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 @@ -129,7 +136,7 @@ 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. @@ -183,3 +190,54 @@ 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. + +Restricting by Service +---------------------- + +If the above options don't fit your needs you can configure any service implementing +:class:`Symfony\\Component\\HttpFoundation\\RequestMatcherInterface` as ``request_matcher``. + +.. configuration-block:: + + .. code-block:: yaml + + # config/packages/security.yaml + + # ... + security: + firewalls: + secured_area: + request_matcher: app.firewall.secured_area.request_matcher + # ... + + .. code-block:: xml + + + + + + + + + + + + + + .. code-block:: php + + // config/packages/security.php + + // ... + $container->loadFromExtension('security', [ + 'firewalls' => [ + 'secured_area' => [ + 'request_matcher' => 'app.firewall.secured_area.request_matcher', + // ... + ], + ], + ]);