|
| 1 | +.. index:: |
| 2 | + single: Security; Restrict Security Firewalls to a Request |
| 3 | + |
| 4 | +How to Restrict Firewalls to a Specific Request |
| 5 | +=============================================== |
| 6 | + |
| 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. |
| 10 | + |
| 11 | +.. note:: |
| 12 | + |
| 13 | + You can use any of these restrictions individually or mix them together to get |
| 14 | + your desired firewall configuration. |
| 15 | + |
| 16 | +Restricting by Pattern |
| 17 | +---------------------- |
| 18 | + |
| 19 | +This is the default restriction and restricts a firewall to only be initialized if the request URL |
| 20 | +matches the configured ``pattern``. |
| 21 | + |
| 22 | +.. configuration-block:: |
| 23 | + |
| 24 | + .. code-block:: yaml |
| 25 | +
|
| 26 | + # app/config/security.yml |
| 27 | +
|
| 28 | + # ... |
| 29 | + security: |
| 30 | + firewalls: |
| 31 | + secured_area: |
| 32 | + pattern: ^/admin |
| 33 | + # ... |
| 34 | +
|
| 35 | + .. code-block:: xml |
| 36 | +
|
| 37 | + <!-- app/config/security.xml --> |
| 38 | + <?xml version="1.0" encoding="UTF-8"?> |
| 39 | + <srv:container xmlns="http://symfony.com/schema/dic/security" |
| 40 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 41 | + xmlns:srv="http://symfony.com/schema/dic/services" |
| 42 | + xsi:schemaLocation="http://symfony.com/schema/dic/services |
| 43 | + http://symfony.com/schema/dic/services/services-1.0.xsd"> |
| 44 | +
|
| 45 | + <config> |
| 46 | + <!-- ... --> |
| 47 | + <firewall name="secured_area" pattern="^/admin"> |
| 48 | + <!-- ... --> |
| 49 | + </firewall> |
| 50 | + </config> |
| 51 | + </srv:container> |
| 52 | +
|
| 53 | + .. code-block:: php |
| 54 | +
|
| 55 | + // app/config/security.php |
| 56 | +
|
| 57 | + // ... |
| 58 | + $container->loadFromExtension('security', array( |
| 59 | + 'firewalls' => array( |
| 60 | + 'secured_area' => array( |
| 61 | + 'pattern' => '^/admin', |
| 62 | + // ... |
| 63 | + ), |
| 64 | + ), |
| 65 | + )); |
| 66 | +
|
| 67 | +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 |
| 70 | +firewalls will have the opportunity to be matched for this request. |
| 71 | +
|
| 72 | +Restricting by Host |
| 73 | +------------------- |
| 74 | +
|
| 75 | +.. versionadded:: 2.4 |
| 76 | + Support for restricting security firewalls to a specific host was introduced in |
| 77 | + Symfony 2.4. |
| 78 | +
|
| 79 | +If matching against the ``pattern`` only is not enough, the request can also be matched against |
| 80 | +``host``. When the configuration option ``host`` is set, the firewall will be restricted to |
| 81 | +only initialize if the host from the request matches against the configuration. |
| 82 | + |
| 83 | +.. configuration-block:: |
| 84 | + |
| 85 | + .. code-block:: yaml |
| 86 | +
|
| 87 | + # app/config/security.yml |
| 88 | +
|
| 89 | + # ... |
| 90 | + security: |
| 91 | + firewalls: |
| 92 | + secured_area: |
| 93 | + host: ^admin\.example\.com$ |
| 94 | + # ... |
| 95 | +
|
| 96 | + .. code-block:: xml |
| 97 | +
|
| 98 | + <!-- app/config/security.xml --> |
| 99 | + <?xml version="1.0" encoding="UTF-8"?> |
| 100 | + <srv:container xmlns="http://symfony.com/schema/dic/security" |
| 101 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 102 | + xmlns:srv="http://symfony.com/schema/dic/services" |
| 103 | + xsi:schemaLocation="http://symfony.com/schema/dic/services |
| 104 | + http://symfony.com/schema/dic/services/services-1.0.xsd"> |
| 105 | +
|
| 106 | + <config> |
| 107 | + <!-- ... --> |
| 108 | + <firewall name="secured_area" host="^admin\.example\.com$"> |
| 109 | + <!-- ... --> |
| 110 | + </firewall> |
| 111 | + </config> |
| 112 | + </srv:container> |
| 113 | +
|
| 114 | + .. code-block:: php |
| 115 | +
|
| 116 | + // app/config/security.php |
| 117 | +
|
| 118 | + // ... |
| 119 | + $container->loadFromExtension('security', array( |
| 120 | + 'firewalls' => array( |
| 121 | + 'secured_area' => array( |
| 122 | + 'host' => '^admin\.example\.com$', |
| 123 | + // ... |
| 124 | + ), |
| 125 | + ), |
| 126 | + )); |
| 127 | +
|
| 128 | +The ``host`` (like the ``pattern``) is a regular expression. In this example, |
| 129 | +the firewall will only be activated if the host is equal exactly (due to |
| 130 | +the ``^`` and ``$`` regex characters) to the hostname ``admin.example.com``. |
| 131 | +If the hostname does not match this pattern, the firewall will not be activated |
| 132 | +and subsequent firewalls will have the opportunity to be matched for this |
| 133 | +request. |
| 134 | + |
| 135 | +Restricting by HTTP Methods |
| 136 | +--------------------------- |
| 137 | + |
| 138 | +.. versionadded:: 2.5 |
| 139 | + Support for restricting security firewalls to specific HTTP methods was introduced in |
| 140 | + Symfony 2.5. |
| 141 | + |
| 142 | +The configuration option ``methods`` restricts the initialization of the firewall to |
| 143 | +the provided HTTP methods. |
| 144 | + |
| 145 | +.. configuration-block:: |
| 146 | + |
| 147 | + .. code-block:: yaml |
| 148 | +
|
| 149 | + # app/config/security.yml |
| 150 | +
|
| 151 | + # ... |
| 152 | + security: |
| 153 | + firewalls: |
| 154 | + secured_area: |
| 155 | + methods: [GET, POST] |
| 156 | + # ... |
| 157 | +
|
| 158 | + .. code-block:: xml |
| 159 | +
|
| 160 | + <!-- app/config/security.xml --> |
| 161 | + <?xml version="1.0" encoding="UTF-8"?> |
| 162 | + <srv:container xmlns="http://symfony.com/schema/dic/security" |
| 163 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 164 | + xmlns:srv="http://symfony.com/schema/dic/services" |
| 165 | + xsi:schemaLocation="http://symfony.com/schema/dic/services |
| 166 | + http://symfony.com/schema/dic/services/services-1.0.xsd"> |
| 167 | +
|
| 168 | + <config> |
| 169 | + <!-- ... --> |
| 170 | + <firewall name="secured_area" methods="GET,POST"> |
| 171 | + <!-- ... --> |
| 172 | + </firewall> |
| 173 | + </config> |
| 174 | + </srv:container> |
| 175 | +
|
| 176 | + .. code-block:: php |
| 177 | +
|
| 178 | + // app/config/security.php |
| 179 | +
|
| 180 | + // ... |
| 181 | + $container->loadFromExtension('security', array( |
| 182 | + 'firewalls' => array( |
| 183 | + 'secured_area' => array( |
| 184 | + 'methods' => array('GET', 'POST'), |
| 185 | + // ... |
| 186 | + ), |
| 187 | + ), |
| 188 | + )); |
| 189 | +
|
| 190 | +In this example, the firewall will only be activated if the HTTP method of the |
| 191 | +request is either ``GET`` or ``POST``. If the method is not in the array of the |
| 192 | +allowed methods, the firewall will not be activated and subsequent firewalls will again |
| 193 | +have the opportunity to be matched for this request. |
0 commit comments