我刚来到这个美丽的世界。
我来这儿是因为我有一个关于交响乐的问题。
我必须为我的考试做一个项目,但我卡在哈希密码。
我遵循这里的文档:https://symfony.com/doc/current/security.html#registering-the-user-hashing-passwords
但是我不知道如何申报$plaintextPassword.
这是我的安全。
security:
# https://symfony.com/doc/current/security.html#registering-the-user-hashing-passwords
password_hashers:
SymfonyComponent@SecurityCoreUserPasswordAuthenticatedUserInterface: 'auto'
这是我的用户实体:
namespace AppEntity;
use AppRepositoryUserRepository;
use DoctrineORMMapping as ORM;
use SymfonyComponentSecurityCoreUserPasswordAuthenticatedUserInterface;
use SymfonyComponentSecurityCoreUserUserInterface;
/**
* @ORMEntity(repositoryClass=UserRepository::class)
*/
class User implements UserInterface, PasswordAuthenticatedUserInterface
{
/**
* @ORMId
* @ORMGeneratedValue
* @ORMColumn(type="integer")
*/
private $id;
/**
* @ORMColumn(type="string", length=180, unique=true)
*/
private $email;
/**
* @ORMColumn(type="json")
*/
private $roles = [];
/**
* @ORMColumn(type="string")
*/
private $password;
/**
* @ORMColumn(type="string", length=255)
*/
private $firstname;
/**
* @ORMColumn(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 AppController;
use AppEntityUser;
use AppFormRegisterType;
use DoctrineORMEntityManagerInterface;
use SymfonyComponentHttpFoundationRequest;
use SymfonyComponentHttpFoundationResponse;
use SymfonyComponentRoutingAnnotationRoute;
use SymfonyBundleFrameworkBundleControllerAbstractController;
use SymfonyComponentPasswordHasherHasherUserPasswordHasherInterface;
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
编码密码在数据库. .
谢谢:)
好吧,
我找到解决办法了。
$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
。
现在我的密码编码在我的db:D