UserPasswordHasherInterface Symfony 6.1.2

问题描述 投票:0回答:1

我是这个美丽世界的新人:) .

我来这里是因为我有一个关于 symfony 的问题。

我必须为考试做一个项目,但我陷入了哈希密码的困境。

我遵循此处的文档:https://symfony.com/doc/current/security.html#registering-the-user-hashing-passwords

但我不知道如何申报

$plaintextPassword.

这是我的 security.yaml

security:
# https://symfony.com/doc/current/security.html#registering-the-user-hashing-passwords
password_hashers:
    Symfony\Component@\Security\Core\User\PasswordAuthenticatedUserInterface: 'auto'

这是我的用户实体:

namespace App\Entity;

use App\Repository\UserRepository;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Security\Core\User\UserInterface;

/**
 * @ORM\Entity(repositoryClass=UserRepository::class)
 */

class User implements UserInterface, PasswordAuthenticatedUserInterface
{

/**
 * @ORM\Id
 * @ORM\GeneratedValue
 * @ORM\Column(type="integer")
 */


private $id;

/**
 * @ORM\Column(type="string", length=180, unique=true)
 */

private $email;

/**
 * @ORM\Column(type="json")
 */

private $roles = [];

/**
 * @ORM\Column(type="string")
 */

private $password;

/**
 * @ORM\Column(type="string", length=255)
 */
private $firstname;

/**
 * @ORM\Column(type="string", length=255)
 */
private $lastname;

public function getId(): ?int
{
    return $this->id;
}

public function getEmail(): ?string
{
    return $this->email;
}

public function setEmail(string $email): self
{
    $this->email = $email;

    return $this;
}

/**
 * A visual identifier that represents this user.
 *
 * @see UserInterface
 */
public function getUserIdentifier(): string
{
    return (string) $this->email;
}

/**
 * @see UserInterface
 */
public function getRoles(): array
{
    $roles = $this->roles;
    // guarantee every user at least has ROLE_USER
    $roles[] = 'ROLE_USER';

    return array_unique($roles);
}

public function setRoles(array $roles): self
{
    $this->roles = $roles;

    return $this;
}

/**
 * @see PasswordAuthenticatedUserInterface
 */
public function getPassword(): string
{
    return $this->password;
}

public function setPassword(string $password): self
{
    $this->password = $password;

    return $this;
}

/**
 * @see UserInterface
 */
public function eraseCredentials()
{
    // If you store any temporary, sensitive data on the user, clear it here
    // $this->plainPassword = null;
}

public function getFirstname(): ?string
{
    return $this->firstname;
}

public function setFirstname(string $firstname): self
{
    $this->firstname = $firstname;

    return $this;
}

public function getLastname(): ?string
{
    return $this->lastname;
}

public function setLastname(string $lastname): self
{
    $this->lastname = $lastname;

    return $this;
}
}

这是我的 RegisterController :

namespace App\Controller;

use App\Entity\User;
use App\Form\RegisterType;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;

class RegisterController extends AbstractController
{
  private $entityManager;

    public function __construct(EntityManagerInterface $entityManager){
    $this->entityManager = $entityManager;
 }


/**
 * @Route("/inscription", name="register")
 */

public function index(Request $request, UserPasswordHasherInterface $passwordHasher): 
Response
{
    $user = new User();
    

    $form = $this->createForm(RegisterType::class, $user);

    $form->handleRequest($request); 

    if ($form->isSubmitted() && $form->isValid()){
        
        $user = $form->getData();

        /* $password = $passwordHasher->hashPassword($user,$user->getPassword());

        $user->setPassword($hashedpassword); */

        $hashedPassword = $passwordHasher->hashPassword(
            $user,
            $plaintextPassword
        );
        $user->setPassword($hashedPassword);



        $this->entityManager->persist($user);
        $this->entityManager->flush();

        

        /* dd($user);  */
    }
    
    return $this->render('url/inscription.html.twig', [
        'form' => $form->createView()
    ]);
}
}

在那之前一切都很好,但我不明白如何使用 UserPasswordHasherInterface 服务,如果有人可以帮助我知道如何声明这个变量

$plaintextpassword
以在数据库中编码我的密码..

谢谢:)

php symfony password-hash
1个回答
0
投票

嗯,

我找到了解决方案。

$plaintextPassword is a random variable to catch the normal password without being encoded.

然后,我找到了这个文档:https://symfony.com/doc/current/security/passwords.html

我只是忘了打电话

composer require symfony/password-hasher
.

现在我的密码已编码在我的数据库中:D

© www.soinside.com 2019 - 2024. All rights reserved.