php - 无法 Autowiring PasswordHasherInterface

标签 php symfony hash frameworks passwords

我正在尝试在Fixtures类中 Autowiring PasswordHasherInterface:

<?php
namespace App\DataFixtures;

use App\Model\User\Entity\User\Email;
use App\Model\User\Entity\User\Id;
use App\Model\User\Entity\User\Role;
use App\Model\User\Entity\User\User;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Persistence\ObjectManager;
use Symfony\Component\PasswordHasher\PasswordHasherInterface;

class UserFixture extends Fixture
{
    private PasswordHasherInterface $hasher;

    public function __construct(PasswordHasherInterface $hasher)
    {
        $this->hasher = $hasher;
    }
    
    public function load(ObjectManager $manager): void
    {
        $hash = $this->hasher->hash("password");

        $user = User::signUpByEmail(
            Id::next(),
            new \DateTimeImmutable(),
            new Email("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8cede8e1e5e2ccedfcfca2f8e9fff8" rel="noreferrer noopener nofollow">[email protected]</a>"),
            $hash,
            "token"
        );

        $user->confirmSignUp();

        $user->changeRole(Role::admin());

        $manager->persist($user);
        $manager->flush();
    }
}

但是我得到了错误:

在 DefinitionErrorExceptionPass.php 第 54 行: !!
!!无法 Autowiring 服务“App\DataFixtures\UserFixture”:参数“$hasher”
!!方法“__construct()”引用接口(interface)“Symfony\Component\PasswordH
!! asher\PasswordHasherInterface”,但不存在此类服务。您是否创建了
!!实现这个接口(interface)的类?
!!

我的文件 services.yaml:

parameters:

services:
    # default configuration for services in *this* file
    _defaults:
        autowire: true      # Automatically injects dependencies in your services.
        autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.

    # makes classes in src/ available to be used as services
    # this creates a service per class whose id is the fully-qualified class name
    App\:
        resource: '../src/'
        exclude:
            - '../src/DependencyInjection/'
            - '../src/Model/User/Entity/'
            - '../src/Kernel.php'

如何在 Symfony 6.1 中散列纯密码? 为什么我会收到此错误?

最佳答案

没有通用的PasswordHasher

您:

  • 使用工厂生成一个特定的:例如Symfony\Component\PasswordHasher\Hasher\PasswordHasherFactoryInterface
  • 或者您使用专用的密码哈希器类:例如Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface 对于用户*。

使用工厂,您的代码将如下所示:(未经测试)

//...
use Symfony\Component\PasswordHasher\Hasher\PasswordHasherFactoryInterface;


class UserFixture extends Fixture
{
    private PasswordHasherFactoryInterface $passwordHasherFactory;

    public function __construct(PasswordHasherFactoryInterface $hasherFactory)
    {
      $this->passwordHasherFactory = $passwordHasherFactory;
    }
    
    public function load(ObjectManager $manager): void
    {
        $passwordHasher = $this->passwordHasherFactory->getPasswordHasher(User::class);
        $hash = $passwordHasher->hash("password");

        $user = User::signUpByEmail(
            Id::next(),
            new \DateTimeImmutable(),
            new Email("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a0c1c4cdc9cee0c1d0d08ed4c5d3d4" rel="noreferrer noopener nofollow">[email protected]</a>"),
            $hash,
            "token"
        );

        $user->confirmSignUp();

        $user->changeRole(Role::admin());

        $manager->persist($user);
        $manager->flush();
    }

重申一下,步骤是:

  1. 安装软件包:composer require symfony/password-hasher
  2. Configure the hasher
  3. 加载哈希器
    • 加载UserPasswordHasherInterface
    • 加载PasswordHasherFactoryInterface(请参阅example)并获取PasswordHasher

*:固定装置的 UserPasswordHasherInterface 示例位于 here .

关于php - 无法 Autowiring PasswordHasherInterface,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72874895/

相关文章:

mysql - 像phpMyAdmin 中的教义多长时间了?

javascript - 如何使用 Twig 中实体的现有数据预先填充集合原型(prototype)?

php - 使用 PHP 将值插入 MySQL 表

javascript - 是否可以将 JS 变量连接到 PHP 邮件方法中,如果可以,如何实现?

mysql - PDO 使用 bindParam 返回 MySQL 错误

express - Payum Paypal Express 结账详情

algorithm - 我可以根据初始 key 和输出哈希来识别哈希算法吗?

java - 如何为相同的用户标识选择相同的颜色

database - 在密码加密中使用随机盐?

PHP 网站标题