无法自动passwordhasherinterface

发布于 2025-02-13 04:44:01 字数 2151 浏览 0 评论 0原文

我正在尝试在“固定装置”类中自动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("[email protected]"),
            $hash,
            "token"
        );

        $user->confirmSignUp();

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

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

但是我有错误:

在DefacitionerRorexceptionpass.php行54: !!
!!无法自动服务“应用程序\ datafixtures \ userFixture”:参数“ $ hasher”
!!方法“ __construct()”参考接口“ Symfony \ Component \ passwordh
!! ASHER \ passwordHasherInterface”但是没有这样的服务。您是否创建了
!!实现此接口的类?
!!

我的file 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中使用平原密码? 为什么我会遇到这个错误?

I'm trying to autowire PasswordHasherInterface in the Fixtures class:

<?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("[email protected]"),
            $hash,
            "token"
        );

        $user->confirmSignUp();

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

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

But I got error:

In DefinitionErrorExceptionPass.php line 54:
!!
!! Cannot autowire service "App\DataFixtures\UserFixture": argument "$hasher"
!! of method "__construct()" references interface "Symfony\Component\PasswordH
!! asher\PasswordHasherInterface" but no such service exists. Did you create a
!! class that implements this interface?
!!

My file 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'

How to hash plain password in Symfony 6.1?
Why I get this error?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深海不蓝 2025-02-20 04:44:01

没有一般passwordhasher

您要么:

  • 使用工厂生成特定的:例如symfony \ component \ compents \ passwordHasher \ hasher \ passwordHasherFactoryInterface
  • 或您使用专用的密码hasher类:eg symfony \ component \ component \ component \ passwordHasher \ passwordHasher \ passwordHasher \ hasher \ hasher \ 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("[email protected]"),
            $hash,
            "token"
        );

        $user->confirmSignUp();

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

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

只是为了重申,这些步骤是:

  1. 安装软件包:作曲家要求Symfony/password-Hasher
  2. 加载哈瑟
    • 加载userpasswordhasherinterface
    • 加载passwordHasherFactoryInterface(请参阅示例)并获取passwordhasher

*:userpasswordhasherinterface的示例位于在这里

There is no general PasswordHasher.

Either you:

  • generate a specific one using a factory: e.g. Symfony\Component\PasswordHasher\Hasher\PasswordHasherFactoryInterface
  • or you use a dedicated password hasher class: e.g. Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface for users*.

Using a factory, your code would look like this: (untested)

//...
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("[email protected]"),
            $hash,
            "token"
        );

        $user->confirmSignUp();

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

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

Just to reiterate, the steps are:

  1. Install the package: composer require symfony/password-hasher
  2. Configure the hasher
  3. Load a hasher
    • Load the UserPasswordHasherInterface OR
    • Load the PasswordHasherFactoryInterface (see example) and get the PasswordHasher

*: An example of UserPasswordHasherInterface for a fixure is located here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文