|
4 | 4 | Using CSRF in the Login Form
|
5 | 5 | ============================
|
6 | 6 |
|
7 |
| -When using a login form, you should make sure that you are protected for CSRF |
8 |
| -(`Cross-site request forgery`_). The Security component already has build-in support |
9 |
| -form CSRF. In this article, you'll learn how you can use it in your login form. |
| 7 | +When using a login form, you should make sure that you are protected against CSRF |
| 8 | +(`Cross-site request forgery`_). The Security component already has built-in support |
| 9 | +for CSRF. In this article you'll learn how you can use it in your login form. |
10 | 10 |
|
11 | 11 | Configuring CSRF
|
12 | 12 | ----------------
|
13 | 13 |
|
14 |
| -At first, you have to configure the security component so it can use CSRF protection. |
15 |
| -The security component needs a CSRF provider. You can set this to use the default |
16 |
| -provider provided by the Form component: |
| 14 | +At first, you have to configure the Security component so it can use CSRF protection. |
| 15 | +The Security component needs a CSRF provider. You can set this to use the default |
| 16 | +provider available in the Form component: |
17 | 17 |
|
18 | 18 | .. configuration-block::
|
19 | 19 |
|
@@ -70,8 +70,8 @@ Rendering the CSRF field
|
70 | 70 | Now the Security component checks for CSRF tokens, you have to add a *hidden* field
|
71 | 71 | to the login form containing the CSRF token. By default, this field is named as
|
72 | 72 | ``_csrf_token``. That hidden field has to contain the CSRF token, which can be generated
|
73 |
| -by using the ``csrf_token`` function. That function requires a token ID, which is |
74 |
| -must be set to ``authenticate`` when using the login form: |
| 73 | +by using the ``csrf_token`` function. That function requires a token ID, which must |
| 74 | +be set to ``authenticate`` when using the login form: |
75 | 75 |
|
76 | 76 | .. configuration-block::
|
77 | 77 |
|
@@ -105,11 +105,60 @@ must be set to ``authenticate`` when using the login form:
|
105 | 105 | <button type="submit">login</button>
|
106 | 106 | </form>
|
107 | 107 |
|
108 |
| -After this, you have protected your login form for CSRF attacks. |
| 108 | +After this, you have protected your login form against CSRF attacks. |
109 | 109 |
|
110 | 110 | .. tip::
|
111 | 111 |
|
112 |
| - You can change the name of the field by setting ``csrf_parameter`` and the token |
113 |
| - ID by setting ``intention`` in your configuration. |
| 112 | + You can change the name of the field by setting ``csrf_parameter`` and change |
| 113 | + the token ID by setting ``intention`` in your configuration: |
| 114 | + |
| 115 | + .. configuration-block:: |
| 116 | + |
| 117 | + .. code-block:: yaml |
| 118 | +
|
| 119 | + # app/config/security.yml |
| 120 | + security: |
| 121 | + firewalls: |
| 122 | + secured_area: |
| 123 | + # ... |
| 124 | + form_login: |
| 125 | + # ... |
| 126 | + csrf_parameter: _csrf_security_token |
| 127 | + intention: a_private_string |
| 128 | +
|
| 129 | + .. code-block:: xml |
| 130 | +
|
| 131 | + <!-- app/config/config.xml --> |
| 132 | + <?xml version="1.0" encoding="UTF-8" ?> |
| 133 | + <srv:container xmlns="http://symfony.com/schema/dic/security" |
| 134 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 135 | + xmlns:srv="http://symfony.com/schema/dic/services" |
| 136 | + xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> |
| 137 | +
|
| 138 | + <config> |
| 139 | + <firewall name="secured_area"> |
| 140 | + <!-- ... --> |
| 141 | +
|
| 142 | + <form-login csrf-parameter="_csrf_security_token" |
| 143 | + intention="a_private_string" /> |
| 144 | + </firewall> |
| 145 | + </config> |
| 146 | + </srv:container> |
| 147 | +
|
| 148 | + .. code-block:: php |
| 149 | +
|
| 150 | + // app/config/security.php |
| 151 | + $container->loadFromExtension('security', array( |
| 152 | + 'firewalls' => array( |
| 153 | + 'secured_area' => array( |
| 154 | + // ... |
| 155 | + 'form_login' => array( |
| 156 | + // ... |
| 157 | + 'csrf_parameter' => '_csrf_security_token', |
| 158 | + 'intention' => 'a_private_string', |
| 159 | + ) |
| 160 | + ) |
| 161 | + ) |
| 162 | + )); |
114 | 163 |
|
115 | 164 | .. _`Cross-site request forgery`: http://en.wikipedia.org/wiki/Cross-site_request_forgery
|
0 commit comments