@@ -660,7 +660,7 @@ see :doc:`/cookbook/security/form_login`.
660
660
),
661
661
),
662
662
663
- **3. Be sure `` /login_check`` is behind a firewall **
663
+ **3. Be sure /login_check is behind a firewall **
664
664
665
665
Next, make sure that your ``check_path `` URL (e.g. ``/login_check ``)
666
666
is behind the firewall you're using for your form login (in this example,
@@ -1206,19 +1206,6 @@ custom user class is that it implements the :class:`Symfony\\Component\\Security
1206
1206
interface. This means that your concept of a "user" can be anything, as long
1207
1207
as it implements this interface.
1208
1208
1209
- .. versionadded :: 2.1
1210
- In Symfony 2.1, the ``equals `` method was removed from ``UserInterface ``.
1211
- If you need to override the default implementation of comparison logic,
1212
- implement the new :class: `Symfony\\ Component\\ Security\\ Core\\ User\\ EquatableInterface `
1213
- interface.
1214
-
1215
- .. note ::
1216
-
1217
- The user object will be serialized and saved in the session during requests,
1218
- therefore it is recommended that you `implement the \S erializable interface `_
1219
- in your user object. This is especially important if your ``User `` class
1220
- has a parent class with private properties.
1221
-
1222
1209
Next, configure an ``entity `` user provider, and point it to your ``User ``
1223
1210
class:
1224
1211
@@ -1278,7 +1265,7 @@ in plain text (whether those users are stored in a configuration file or in
1278
1265
a database somewhere). Of course, in a real application, you'll want to encode
1279
1266
your users' passwords for security reasons. This is easily accomplished by
1280
1267
mapping your User class to one of several built-in "encoders". For example,
1281
- to store your users in memory, but obscure their passwords via ``sha1 ``,
1268
+ to store your users in memory, but obscure their passwords via ``bcrypt ``,
1282
1269
do the following:
1283
1270
1284
1271
.. configuration-block ::
@@ -1292,14 +1279,17 @@ do the following:
1292
1279
in_memory :
1293
1280
memory :
1294
1281
users :
1295
- ryan : { password: bb87a29949f3a1ee0559f8a57357487151281386, roles: 'ROLE_USER' }
1296
- admin : { password: 74913f5cd5f61ec0bcfdb775414c2fb3d161b620, roles: 'ROLE_ADMIN' }
1282
+ ryan :
1283
+ password : $2a$12$w/aHvnC/XNeDVrrl65b3dept8QcKqpADxUlbraVXXsC03Jam5hvoO
1284
+ roles : ' ROLE_USER'
1285
+ admin :
1286
+ password : $2a$12$HmOsqRDJK0HuMDQ5Fb2.AOLMQHyNHGD0seyjU3lEVusjT72QQEIpW
1287
+ roles : ' ROLE_ADMIN'
1297
1288
1298
1289
encoders :
1299
1290
Symfony\Component\Security\Core\User\User :
1300
- algorithm : sha1
1301
- iterations : 1
1302
- encode_as_base64 : false
1291
+ algorithm : bcrypt
1292
+ cost : 12
1303
1293
1304
1294
.. code-block :: xml
1305
1295
@@ -1309,18 +1299,18 @@ do the following:
1309
1299
<provider name =" in_memory" >
1310
1300
<memory >
1311
1301
<user name =" ryan"
1312
- password =" bb87a29949f3a1ee0559f8a57357487151281386 "
1302
+ password =" $2a$12$w/aHvnC/XNeDVrrl65b3dept8QcKqpADxUlbraVXXsC03Jam5hvoO "
1313
1303
roles =" ROLE_USER" />
1314
1304
<user name =" admin"
1315
- password =" 74913f5cd5f61ec0bcfdb775414c2fb3d161b620 "
1305
+ password =" $2a$12$HmOsqRDJK0HuMDQ5Fb2.AOLMQHyNHGD0seyjU3lEVusjT72QQEIpW "
1316
1306
roles =" ROLE_ADMIN" />
1317
1307
</memory >
1318
1308
</provider >
1319
1309
1320
1310
<encoder class =" Symfony\Component\Security\Core\User\User"
1321
- algorithm =" sha1 "
1322
- iterations = " 1 "
1323
- encode_as_base64 = " false " />
1311
+ algorithm =" bcrypt "
1312
+ cost = " 12 "
1313
+ />
1324
1314
</config >
1325
1315
1326
1316
.. code-block :: php
@@ -1333,11 +1323,11 @@ do the following:
1333
1323
'memory' => array(
1334
1324
'users' => array(
1335
1325
'ryan' => array(
1336
- 'password' => 'bb87a29949f3a1ee0559f8a57357487151281386 ',
1326
+ 'password' => '$2a$12$w/aHvnC/XNeDVrrl65b3dept8QcKqpADxUlbraVXXsC03Jam5hvoO ',
1337
1327
'roles' => 'ROLE_USER',
1338
1328
),
1339
1329
'admin' => array(
1340
- 'password' => '74913f5cd5f61ec0bcfdb775414c2fb3d161b620 ',
1330
+ 'password' => '$2a$12$HmOsqRDJK0HuMDQ5Fb2.AOLMQHyNHGD0seyjU3lEVusjT72QQEIpW ',
1341
1331
'roles' => 'ROLE_ADMIN',
1342
1332
),
1343
1333
),
@@ -1346,77 +1336,36 @@ do the following:
1346
1336
),
1347
1337
'encoders' => array(
1348
1338
'Symfony\Component\Security\Core\User\User' => array(
1349
- 'algorithm' => 'sha1',
1350
- 'iterations' => 1,
1351
- 'encode_as_base64' => false,
1339
+ 'algorithm' => 'bcrypt',
1340
+ 'iterations' => 12,
1352
1341
),
1353
1342
),
1354
1343
));
1355
1344
1356
- By setting the ``iterations `` to ``1 `` and the ``encode_as_base64 `` to false,
1357
- the password is simply run through the ``sha1 `` algorithm one time and without
1358
- any extra encoding. You can now calculate the hashed password either programmatically
1359
- (e.g. ``hash('sha1', 'ryanpass') ``) or via some online tool like `functions-online.com `_
1360
-
1361
- .. tip ::
1362
-
1363
- Supported algorithms for this method depend on your PHP version.
1364
- A full list is available calling the PHP function :phpfunction: `hash_algos `.
1365
-
1366
- If you're creating your users dynamically (and storing them in a database),
1367
- you can use even tougher hashing algorithms and then rely on an actual password
1368
- encoder object to help you encode passwords. For example, suppose your User
1369
- object is ``Acme\UserBundle\Entity\User `` (like in the above example). First,
1370
- configure the encoder for that user:
1371
-
1372
- .. configuration-block ::
1373
-
1374
- .. code-block :: yaml
1375
-
1376
- # app/config/security.yml
1377
- security :
1378
- # ...
1379
-
1380
- encoders :
1381
- Acme\UserBundle\Entity\User : sha512
1382
-
1383
- .. code-block :: xml
1384
-
1385
- <!-- app/config/security.xml -->
1386
- <config >
1387
- <!-- ... -->
1388
-
1389
- <encoder class =" Acme\UserBundle\Entity\User" algorithm =" sha512" />
1390
- </config >
1345
+ .. versionadded :: 2.2
1346
+ The BCrypt encoder was introduced in Symfony 2.2.
1391
1347
1392
- .. code-block :: php
1348
+ You can now calculate the hashed password either programmatically
1349
+ (e.g. ``password_hash('ryanpass', PASSWORD_BCRYPT, array('cost' => 12)); ``)
1350
+ or via some online tool.
1393
1351
1394
- // app/config/security.php
1395
- $container->loadFromExtension('security', array(
1396
- // ...
1397
- 'encoders' => array(
1398
- 'Acme\UserBundle\Entity\User' => 'sha512',
1399
- ),
1400
- ));
1352
+ .. include :: /cookbook/security/_ircmaxwell_password-compat.rst.inc
1401
1353
1402
- In this case, you're using the stronger ``sha512 `` algorithm. Also, since
1403
- you've simply specified the algorithm (``sha512 ``) as a string, the system
1404
- will default to hashing your password 5000 times in a row and then encoding
1405
- it as base64. In other words, the password has been greatly obfuscated so
1406
- that the hashed password can't be decoded (i.e. you can't determine the password
1407
- from the hashed password).
1354
+ Supported algorithms for this method depend on your PHP version. A full list
1355
+ is available by calling the PHP function :phpfunction: `hash_algos `.
1408
1356
1409
1357
.. versionadded :: 2.2
1410
1358
As of Symfony 2.2 you can also use the :ref: `PBKDF2 <reference-security-pbkdf2 >`
1411
- and :ref: ` BCrypt < reference-security-bcrypt >` password encoders .
1359
+ password encoder .
1412
1360
1413
1361
Determining the Hashed Password
1414
1362
...............................
1415
1363
1416
- If you have some sort of registration form for users, you'll need to be able
1417
- to determine the hashed password so that you can set it on your user. No
1418
- matter what algorithm you configure for your user object, the hashed password
1419
- can always be determined in the following way from a controller::
1364
+ If you're storing users in the database and you have some sort of registration
1365
+ form for users, you'll need to be able to determine the hashed password so
1366
+ that you can set it on your user before inserting it. No matter what algorithm
1367
+ you configure for your user object, the hashed password can always be determined
1368
+ in the following way from a controller::
1420
1369
1421
1370
$factory = $this->get('security.encoder_factory');
1422
1371
$user = new Acme\UserBundle\Entity\User();
@@ -1425,6 +1374,10 @@ can always be determined in the following way from a controller::
1425
1374
$password = $encoder->encodePassword('ryanpass', $user->getSalt());
1426
1375
$user->setPassword($password);
1427
1376
1377
+ In order for this to work, just make sure that you have the encoder for your
1378
+ user class (e.g. ``Acme\UserBundle\Entity\User ``) configured under the ``encoders ``
1379
+ key in ``app/config/security.yml ``.
1380
+
1428
1381
.. caution ::
1429
1382
1430
1383
When you allow a user to submit a plaintext password (e.g. registration
@@ -2077,5 +2030,4 @@ Learn more from the Cookbook
2077
2030
.. _`JMSSecurityExtraBundle` : http://jmsyst.com/bundles/JMSSecurityExtraBundle/1.2
2078
2031
.. _`FOSUserBundle` : https://github.com/FriendsOfSymfony/FOSUserBundle
2079
2032
.. _`implement the \S erializable interface` : http://php.net/manual/en/class.serializable.php
2080
- .. _`functions-online.com` : http://www.functions-online.com/sha1.html
2081
2033
.. _`Timing attack` : http://en.wikipedia.org/wiki/Timing_attack
0 commit comments