Php 如何在Symfony 5中对密码进行编码?
我正在尝试用Symfony对密码进行编码,在仔细阅读文档之后,我仍然觉得我做错了什么 这是我的RegisterController.php:Php 如何在Symfony 5中对密码进行编码?,php,symfony,password-encryption,Php,Symfony,Password Encryption,我正在尝试用Symfony对密码进行编码,在仔细阅读文档之后,我仍然觉得我做错了什么 这是我的RegisterController.php: <?php namespace App\Controller; use App\Entity\User; use App\Form\Type\UserType; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\Routin
<?php
namespace App\Controller;
use App\Entity\User;
use App\Form\Type\UserType;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
class RegisterController extends AbstractController
{
private $passwordEncoder;
public function __construct(UserPasswordEncoderInterface $passwordEncoder)
{
$this->passwordEncoder = $passwordEncoder;
}
/**
* @Route("/register", name="user.register")
*/
public function create(Request $request)
{
$user = new User();
$form = $this->createForm(UserType::class, $user);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$user->setPassword(
$this->passwordEncoder->encodePassword( $user, $user->getPassword() )
);
我想我忽略了一些简单的事情,但我无法让它发挥作用。这是我第一次使用
Symfony
问题出在别处
如果调用“明文密码”方法,则在您的小树枝或表单中进行检查,该方法不存在,并将其替换为“密码”。问题出在其他地方
如果调用“明文密码”方法(该方法不存在),请签入您的小树枝或表单,并将其替换为“密码”。错误似乎在小树枝文件中。您试图显示
$plainPassword
成员,但它很可能未在实体上定义(在我访问symfony的过程中,经常发生此错误)。尝试删除它,然后检查错误
至于密码的编码,请参阅,因为它解释了security.yml文件中密码是如何加密的。您需要定义将被编码的类,算法可以更改为自定义的,您可以构建的,请检查
编码没有那么复杂,但错误与编码无关,因此请尝试此操作并更新帖子。错误似乎在细枝文件中。您试图显示
$plainPassword
成员,但它很可能未在实体上定义(在我访问symfony的过程中,经常发生此错误)。尝试删除它,然后检查错误
至于密码的编码,请参阅,因为它解释了security.yml文件中密码是如何加密的。您需要定义将被编码的类,算法可以更改为自定义的,您可以构建的,请检查
编码并不复杂,但错误与编码无关,因此请尝试此操作并更新帖子。因此,我发现了错误,唉!我已经在我的UserType.php中设置了字段
plainPassword
:
class UserType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('firstname', TextType::class)
->add('lastname', TextType::class)
->add('email', EmailType::class)
->add('birthdate', DateType::class)
->add('plainPassword', RepeatedType::class, array(
'type' => PasswordType::class,
'first_options' => array('label' => 'Password'),
'second_options' => array('label' => 'Repeat Password'),
))
->add('save', SubmitType::class, ['label' => 'Register']);
}
...
将其更改为密码
,就像我在用户
实体中所做的那样,可以更正错误
谢谢大家的帮助。所以,我发现了错误,唉!我已经在我的UserType.php中设置了字段
plainPassword
:
class UserType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('firstname', TextType::class)
->add('lastname', TextType::class)
->add('email', EmailType::class)
->add('birthdate', DateType::class)
->add('plainPassword', RepeatedType::class, array(
'type' => PasswordType::class,
'first_options' => array('label' => 'Password'),
'second_options' => array('label' => 'Repeat Password'),
))
->add('save', SubmitType::class, ['label' => 'Register']);
}
...
将其更改为密码
,就像我在用户
实体中所做的那样,可以更正错误
谢谢大家的帮助。实际上发生这种情况是因为symfony检测到用户实体中是否没有“plainPassword”属性。使用此“plainPassword”属性的目的是作为临时数据,以便对其进行编码。您需要做的是将表单类型中映射为false的“plainPassword”属性设置为false
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('plainPassword', RepeatedType::class, array(
'type' => PasswordType::class,
'mapped' => false,
'first_options' => array('label' => 'New password'),
'second_options' => array('label' => 'Confirm new password'),
'invalid_message' => 'The password fields must match.',
))
;
}
在控制器中,将“明文密码”编码为“密码”:
实际上,这是因为symfony检测用户实体中是否没有“plainPassword”属性。使用此“plainPassword”属性的目的是作为临时数据,以便对其进行编码。您需要做的是将表单类型中映射为false的“plainPassword”属性设置为false
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('plainPassword', RepeatedType::class, array(
'type' => PasswordType::class,
'mapped' => false,
'first_options' => array('label' => 'New password'),
'second_options' => array('label' => 'Confirm new password'),
'invalid_message' => 'The password fields must match.',
))
;
}
在控制器中,将“明文密码”编码为“密码”:
更新您的问题,并链接到您正在跟踪的文档。User::password用于最终哈希密码。创建用户时,通常使用附加的plainPassword属性来存储来自表单的未删除密码。您可能只需要向您的用户实体添加明文密码。使用指向您正在跟踪的文档的链接更新您的问题。User::password用于最终哈希密码。创建用户时,通常使用附加的plainPassword属性来存储来自表单的未删除密码。您可能只需要向您的用户实体添加plainPassword。我已更新我的问题,以包括我的小树枝文件,该文件似乎没有
plainPassword
作为方法。我正在使用helper方法form(form)
我已经更新了我的问题,以包括我的小树枝文件,它似乎没有plainPassword
作为方法。我正在使用helper方法form(form)
我已经更新了我的问题。是的,我确实遵循了security.yaml
文件中的文档。我知道编码不会这么复杂,可能是我在其他部分出错了。请同时添加表单内容。哇哦,我现在发现了错误…请参考plainPassword
:facepalm。感谢您的帮助@fovasI更新了我的问题。是的,我确实遵循了security.yaml
文件中的文档。我知道编码不会这么复杂,可能是我在其他部分出错了。请同时添加表单内容。哇哦,我现在发现了错误…请参考plainPassword
:facepalm。感谢您的帮助@fovas
/**
* @Route("/register", name="app_register")
*/
public function register(Request $request, UserPasswordEncoderInterface $passwordEncoder): Response
{
$user = new User();
$form = $this->createForm(RegistrationFormType::class, $user);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
// encode the plain password
$user->setPassword(
$passwordEncoder->encodePassword(
$user,
$form->get('plainPassword')->getData()
)
);
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($user);
$entityManager->flush();
// do anything else you need here, like send an email
return $this->redirectToRoute('any_route');
}
return $this->render('registration/register.html.twig', [
'form' => $form->createView(),
]);
}