@@ -661,7 +661,7 @@ see :doc:`/cookbook/security/form_login`.
661
661
),
662
662
),
663
663
664
- **3. Be sure `` /login_check`` is behind a firewall **
664
+ **3. Be sure /login_check is behind a firewall **
665
665
666
666
Next, make sure that your ``check_path `` URL (e.g. ``/login_check ``)
667
667
is behind the firewall you're using for your form login (in this example,
@@ -1098,7 +1098,7 @@ Thanks to the SensioFrameworkExtraBundle, you can also secure your controller us
1098
1098
// ...
1099
1099
}
1100
1100
1101
- For more information, see the
1101
+ For more information, see the
1102
1102
:doc: `FrameworkExtraBundle documentation </bundles/SensioFrameworkExtraBundle/annotations/security >`.
1103
1103
1104
1104
Securing other Services
@@ -1338,7 +1338,7 @@ in plain text (whether those users are stored in a configuration file or in
1338
1338
a database somewhere). Of course, in a real application, you'll want to encode
1339
1339
your users' passwords for security reasons. This is easily accomplished by
1340
1340
mapping your User class to one of several built-in "encoders". For example,
1341
- to store your users in memory, but obscure their passwords via ``sha1 ``,
1341
+ to store your users in memory, but obscure their passwords via ``bcrypt ``,
1342
1342
do the following:
1343
1343
1344
1344
.. configuration-block ::
@@ -1352,14 +1352,17 @@ do the following:
1352
1352
in_memory :
1353
1353
memory :
1354
1354
users :
1355
- ryan : { password: bb87a29949f3a1ee0559f8a57357487151281386, roles: 'ROLE_USER' }
1356
- admin : { password: 74913f5cd5f61ec0bcfdb775414c2fb3d161b620, roles: 'ROLE_ADMIN' }
1355
+ ryan :
1356
+ password : $2a$12$w/aHvnC/XNeDVrrl65b3dept8QcKqpADxUlbraVXXsC03Jam5hvoO
1357
+ roles : ' ROLE_USER'
1358
+ admin :
1359
+ password : $2a$12$HmOsqRDJK0HuMDQ5Fb2.AOLMQHyNHGD0seyjU3lEVusjT72QQEIpW
1360
+ roles : ' ROLE_ADMIN'
1357
1361
1358
1362
encoders :
1359
1363
Symfony\Component\Security\Core\User\User :
1360
- algorithm : sha1
1361
- iterations : 1
1362
- encode_as_base64 : false
1364
+ algorithm : bcrypt
1365
+ cost : 12
1363
1366
1364
1367
.. code-block :: xml
1365
1368
@@ -1369,18 +1372,18 @@ do the following:
1369
1372
<provider name =" in_memory" >
1370
1373
<memory >
1371
1374
<user name =" ryan"
1372
- password =" bb87a29949f3a1ee0559f8a57357487151281386 "
1375
+ password =" $2a$12$w/aHvnC/XNeDVrrl65b3dept8QcKqpADxUlbraVXXsC03Jam5hvoO "
1373
1376
roles =" ROLE_USER" />
1374
1377
<user name =" admin"
1375
- password =" 74913f5cd5f61ec0bcfdb775414c2fb3d161b620 "
1378
+ password =" $2a$12$HmOsqRDJK0HuMDQ5Fb2.AOLMQHyNHGD0seyjU3lEVusjT72QQEIpW "
1376
1379
roles =" ROLE_ADMIN" />
1377
1380
</memory >
1378
1381
</provider >
1379
1382
1380
1383
<encoder class =" Symfony\Component\Security\Core\User\User"
1381
- algorithm =" sha1 "
1382
- iterations = " 1 "
1383
- encode_as_base64 = " false " />
1384
+ algorithm =" bcrypt "
1385
+ cost = " 12 "
1386
+ />
1384
1387
</config >
1385
1388
1386
1389
.. code-block :: php
@@ -1393,11 +1396,11 @@ do the following:
1393
1396
'memory' => array(
1394
1397
'users' => array(
1395
1398
'ryan' => array(
1396
- 'password' => 'bb87a29949f3a1ee0559f8a57357487151281386 ',
1399
+ 'password' => '$2a$12$w/aHvnC/XNeDVrrl65b3dept8QcKqpADxUlbraVXXsC03Jam5hvoO ',
1397
1400
'roles' => 'ROLE_USER',
1398
1401
),
1399
1402
'admin' => array(
1400
- 'password' => '74913f5cd5f61ec0bcfdb775414c2fb3d161b620 ',
1403
+ 'password' => '$2a$12$HmOsqRDJK0HuMDQ5Fb2.AOLMQHyNHGD0seyjU3lEVusjT72QQEIpW ',
1401
1404
'roles' => 'ROLE_ADMIN',
1402
1405
),
1403
1406
),
@@ -1406,73 +1409,32 @@ do the following:
1406
1409
),
1407
1410
'encoders' => array(
1408
1411
'Symfony\Component\Security\Core\User\User' => array(
1409
- 'algorithm' => 'sha1',
1410
- 'iterations' => 1,
1411
- 'encode_as_base64' => false,
1412
+ 'algorithm' => 'bcrypt',
1413
+ 'iterations' => 12,
1412
1414
),
1413
1415
),
1414
1416
));
1415
1417
1416
- By setting the ``iterations `` to ``1 `` and the ``encode_as_base64 `` to false,
1417
- the password is simply run through the ``sha1 `` algorithm one time and without
1418
- any extra encoding. You can now calculate the hashed password either programmatically
1419
- (e.g. ``hash('sha1', 'ryanpass') ``) or via some online tool like `functions-online.com `_
1418
+ .. versionadded :: 2.2
1419
+ The BCrypt encoder was introduced in Symfony 2.2.
1420
1420
1421
- .. tip ::
1422
-
1423
- Supported algorithms for this method depend on your PHP version.
1424
- A full list is available calling the PHP function :phpfunction: `hash_algos `.
1425
-
1426
- If you're creating your users dynamically (and storing them in a database),
1427
- you can use even tougher hashing algorithms and then rely on an actual password
1428
- encoder object to help you encode passwords. For example, suppose your User
1429
- object is ``Acme\UserBundle\Entity\User `` (like in the above example). First,
1430
- configure the encoder for that user:
1431
-
1432
- .. configuration-block ::
1433
-
1434
- .. code-block :: yaml
1435
-
1436
- # app/config/security.yml
1437
- security :
1438
- # ...
1439
-
1440
- encoders :
1441
- Acme\UserBundle\Entity\User : sha512
1442
-
1443
- .. code-block :: xml
1421
+ You can now calculate the hashed password either programmatically
1422
+ (e.g. ``password_hash('ryanpass', PASSWORD_BCRYPT, array('cost' => 12)); ``)
1423
+ or via some online tool.
1444
1424
1445
- <!-- app/config/security.xml -->
1446
- <config >
1447
- <!-- ... -->
1448
-
1449
- <encoder class =" Acme\UserBundle\Entity\User" algorithm =" sha512" />
1450
- </config >
1425
+ .. include :: /cookbook/security/_ircmaxwell_password-compat.rst.inc
1451
1426
1452
- .. code-block :: php
1453
-
1454
- // app/config/security.php
1455
- $container->loadFromExtension('security', array(
1456
- // ...
1457
- 'encoders' => array(
1458
- 'Acme\UserBundle\Entity\User' => 'sha512',
1459
- ),
1460
- ));
1461
-
1462
- In this case, you're using the stronger ``sha512 `` algorithm. Also, since
1463
- you've simply specified the algorithm (``sha512 ``) as a string, the system
1464
- will default to hashing your password 5000 times in a row and then encoding
1465
- it as base64. In other words, the password has been greatly obfuscated so
1466
- that the hashed password can't be decoded (i.e. you can't determine the password
1467
- from the hashed password).
1427
+ Supported algorithms for this method depend on your PHP version. A full list
1428
+ is available by calling the PHP function :phpfunction: `hash_algos `.
1468
1429
1469
1430
Determining the Hashed Password
1470
1431
...............................
1471
1432
1472
- If you have some sort of registration form for users, you'll need to be able
1473
- to determine the hashed password so that you can set it on your user. No
1474
- matter what algorithm you configure for your user object, the hashed password
1475
- can always be determined in the following way from a controller::
1433
+ If you're storing users in the database and you have some sort of registration
1434
+ form for users, you'll need to be able to determine the hashed password so
1435
+ that you can set it on your user before inserting it. No matter what algorithm
1436
+ you configure for your user object, the hashed password can always be determined
1437
+ in the following way from a controller::
1476
1438
1477
1439
$factory = $this->get('security.encoder_factory');
1478
1440
$user = new Acme\UserBundle\Entity\User();
@@ -1481,6 +1443,10 @@ can always be determined in the following way from a controller::
1481
1443
$password = $encoder->encodePassword('ryanpass', $user->getSalt());
1482
1444
$user->setPassword($password);
1483
1445
1446
+ In order for this to work, just make sure that you have the encoder for your
1447
+ user class (e.g. ``Acme\UserBundle\Entity\User ``) configured under the ``encoders ``
1448
+ key in ``app/config/security.yml ``.
1449
+
1484
1450
.. caution ::
1485
1451
1486
1452
When you allow a user to submit a plaintext password (e.g. registration
@@ -2157,8 +2123,8 @@ Learn more from the Cookbook
2157
2123
* :doc: `Blacklist users by IP address with a custom voter </cookbook/security/voters >`
2158
2124
* :doc: `Access Control Lists (ACLs) </cookbook/security/acl >`
2159
2125
* :doc: `/cookbook/security/remember_me `
2126
+ * :doc: `How to Restrict Firewalls to a Specific Host </cookbook/security/host_restriction >`
2160
2127
2161
2128
.. _`FOSUserBundle` : https://github.com./FriendsOfSymfony/FOSUserBundle
2162
2129
.. _`implement the \S erializable interface` : http://php.net/manual/en/class.serializable.php
2163
- .. _`functions-online.com` : http://www.functions-online.com/sha1.html
2164
2130
.. _`Timing attack` : http://en.wikipedia.org/wiki/Timing_attack
0 commit comments