@@ -661,7 +661,7 @@ see :doc:`/cookbook/security/form_login`.
661661 ),
662662 ),
663663
664- **3. Be sure `` /login_check`` is behind a firewall **
664+ **3. Be sure /login_check is behind a firewall **
665665
666666 Next, make sure that your ``check_path `` URL (e.g. ``/login_check ``)
667667 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
10981098 // ...
10991099 }
11001100
1101- For more information, see the
1101+ For more information, see the
11021102:doc: `FrameworkExtraBundle documentation </bundles/SensioFrameworkExtraBundle/annotations/security >`.
11031103
11041104Securing other Services
@@ -1338,7 +1338,7 @@ in plain text (whether those users are stored in a configuration file or in
13381338a database somewhere). Of course, in a real application, you'll want to encode
13391339your users' passwords for security reasons. This is easily accomplished by
13401340mapping 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 ``,
13421342do the following:
13431343
13441344.. configuration-block ::
@@ -1352,14 +1352,17 @@ do the following:
13521352 in_memory :
13531353 memory :
13541354 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'
13571361
13581362 encoders :
13591363 Symfony\Component\Security\Core\User\User :
1360- algorithm : sha1
1361- iterations : 1
1362- encode_as_base64 : false
1364+ algorithm : bcrypt
1365+ cost : 12
13631366
13641367 .. code-block :: xml
13651368
@@ -1369,18 +1372,18 @@ do the following:
13691372 <provider name =" in_memory" >
13701373 <memory >
13711374 <user name =" ryan"
1372- password =" bb87a29949f3a1ee0559f8a57357487151281386 "
1375+ password =" $2a$12$w/aHvnC/XNeDVrrl65b3dept8QcKqpADxUlbraVXXsC03Jam5hvoO "
13731376 roles =" ROLE_USER" />
13741377 <user name =" admin"
1375- password =" 74913f5cd5f61ec0bcfdb775414c2fb3d161b620 "
1378+ password =" $2a$12$HmOsqRDJK0HuMDQ5Fb2.AOLMQHyNHGD0seyjU3lEVusjT72QQEIpW "
13761379 roles =" ROLE_ADMIN" />
13771380 </memory >
13781381 </provider >
13791382
13801383 <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+ />
13841387 </config >
13851388
13861389 .. code-block :: php
@@ -1393,11 +1396,11 @@ do the following:
13931396 'memory' => array(
13941397 'users' => array(
13951398 'ryan' => array(
1396- 'password' => 'bb87a29949f3a1ee0559f8a57357487151281386 ',
1399+ 'password' => '$2a$12$w/aHvnC/XNeDVrrl65b3dept8QcKqpADxUlbraVXXsC03Jam5hvoO ',
13971400 'roles' => 'ROLE_USER',
13981401 ),
13991402 'admin' => array(
1400- 'password' => '74913f5cd5f61ec0bcfdb775414c2fb3d161b620 ',
1403+ 'password' => '$2a$12$HmOsqRDJK0HuMDQ5Fb2.AOLMQHyNHGD0seyjU3lEVusjT72QQEIpW ',
14011404 'roles' => 'ROLE_ADMIN',
14021405 ),
14031406 ),
@@ -1406,73 +1409,32 @@ do the following:
14061409 ),
14071410 'encoders' => array(
14081411 'Symfony\Component\Security\Core\User\User' => array(
1409- 'algorithm' => 'sha1',
1410- 'iterations' => 1,
1411- 'encode_as_base64' => false,
1412+ 'algorithm' => 'bcrypt',
1413+ 'iterations' => 12,
14121414 ),
14131415 ),
14141416 ));
14151417
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.
14201420
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.
14441424
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
14511426
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 `.
14681429
14691430Determining the Hashed Password
14701431...............................
14711432
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::
14761438
14771439 $factory = $this->get('security.encoder_factory');
14781440 $user = new Acme\UserBundle\Entity\User();
@@ -1481,6 +1443,10 @@ can always be determined in the following way from a controller::
14811443 $password = $encoder->encodePassword('ryanpass', $user->getSalt());
14821444 $user->setPassword($password);
14831445
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+
14841450.. caution ::
14851451
14861452 When you allow a user to submit a plaintext password (e.g. registration
@@ -2157,8 +2123,8 @@ Learn more from the Cookbook
21572123* :doc: `Blacklist users by IP address with a custom voter </cookbook/security/voters >`
21582124* :doc: `Access Control Lists (ACLs) </cookbook/security/acl >`
21592125* :doc: `/cookbook/security/remember_me `
2126+ * :doc: `How to Restrict Firewalls to a Specific Host </cookbook/security/host_restriction >`
21602127
21612128.. _`FOSUserBundle` : https://github.com/FriendsOfSymfony/FOSUserBundle
21622129.. _`implement the \S erializable interface` : http://php.net/manual/en/class.serializable.php
2163- .. _`functions-online.com` : http://www.functions-online.com/sha1.html
21642130.. _`Timing attack` : http://en.wikipedia.org/wiki/Timing_attack
0 commit comments