Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[Security] register alias for argument for password hasher#21137

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
vinceAmstoutz wants to merge1 commit intosymfony:7.4
base:7.4
Choose a base branch
Loading
fromvinceAmstoutz:fix/21075
Open
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletionssecurity.rst
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -461,8 +461,8 @@ You can also manually hash a password by running:

$ php bin/console security:hash-password

Read more about all available hashersand password migration in
:doc:`security/passwords`.
Read more about all available hashers(including specific hashers) and password
migration in:doc:`security/passwords`.

.. _firewalls-authentication:
.. _a-authentication-firewalls:
Expand Down
55 changes: 55 additions & 0 deletionssecurity/passwords.rst
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -226,6 +226,61 @@ After configuring the correct algorithm, you can use the
throw new \Exception('Bad credentials, cannot delete this user.');
}

Injecting a Specific Password Hasher
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

In some cases, you might define a password hasher in your configuration that is
not linked to a user entity but is instead identified by a unique key.
For example, you might have a separate hasher for things like password recovery
codes.

With the following configuration:

.. code-block:: yaml

# config/packages/security.yaml
security:
password_hashers:
recovery_code: 'auto'

firewalls:
main:
# ...

It is possible to inject the recovery_code password hasher into any service.
To do this, you can't rely on standard autowiring, as Symfony wouldn't know
which specific hasher to provide.

Instead, you can use the ``#[Target]`` attribute to request the hasher by its
configuration key::

// src/Controller/HomepageController.php
namespace App\Controller;

use Symfony\Component\DependencyInjection\Attribute\Target;
use Symfony\Component\PasswordHasher\PasswordHasherInterface;

class HomepageController extends AbstractController
{
public function __construct(
#[Target('recovery_code')]
private readonly PasswordHasherInterface $passwordHasher,
) {
}

#[Route('/')]
public function index(): Response
{
$plaintextToken = 'some-secret-token';

// Note: use hash(), not hashPassword(), as we are not using a UserInterface object
$hashedToken = $this->passwordHasher->hash($plaintextToken);
}
}

When injecting a specific hasher by its name, you should type-hint the generic
:class:`Symfony\\Component\\PasswordHasher\\PasswordHasherInterface`.

Reset Password
--------------

Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp