猿问

如何在 Symfony 5 中编码密码?

我正在尝试在 Symfony 中对密码进行编码,并且在仔细按照此处的文档进行操作之后,似乎我仍然做错了什么。


这是我的 RegisterController.php:


<?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() )

        );

以上返回以下错误:


属性“plainPassword”和方法之一“getPlainPassword()”、“plainPassword()”、“isPlainPassword()”、“hasPlainPassword()”、“__get()”都不存在并且在“App\”类中具有公共访问权限实体\用户”。


这是我的 Register.twig.html:


<!DOCTYPE html>

<html>

    <head>

        <meta charset="UTF-8">

        <title>{% block title %}Register{% endblock %}</title>

        {# {% block stylesheets %}{% endblock %} #}

    </head>

    <body>

        <div class="container">

            <h2 class="form-signin-heading">Welcome, please register below</h2>

            {{ form(form) }}

        </div>


    </body>

</html>

最后我在我的security.yaml文件中有这个设置:


security:    

   encoders:

        App\Entity\User:

            algorithm: auto

我想这是我忽略的简单事情,但我无法让它发挥作用。这是我的第一次Symfony。


php

交响乐

密码加密


慕尼黑8549860
浏览 155回答 3
3回答

沧海一幻觉

实际上这是因为 symfony 检测到用户实体中是否没有“plainPassword”属性。使用此“plainPassword”属性的目的是作为临时数据,因此我们可以对其进行编码。您需要做的是在您的表单类型中设置映射为 false的“plainPassword”属性。public function buildForm(FormBuilderInterface $builder, array $options){&nbsp; &nbsp; $builder&nbsp; &nbsp; &nbsp; &nbsp; ->add('plainPassword', RepeatedType::class, array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'type'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; => PasswordType::class,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'mapped'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; => false,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'first_options'&nbsp; &nbsp; &nbsp;=> array('label' => 'New password'),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'second_options'&nbsp; &nbsp; => array('label' => 'Confirm new password'),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'invalid_message' => 'The password fields must match.',&nbsp; &nbsp; &nbsp; &nbsp; ))&nbsp; &nbsp; ;}并在您的控制器中将“plainPassword”编码为“密码”:/**&nbsp;* @Route("/register", name="app_register")&nbsp;*/public function register(Request $request, UserPasswordEncoderInterface $passwordEncoder): Response{&nbsp; &nbsp; $user = new User();&nbsp; &nbsp; $form = $this->createForm(RegistrationFormType::class, $user);&nbsp; &nbsp; $form->handleRequest($request);&nbsp; &nbsp; if ($form->isSubmitted() && $form->isValid()) {&nbsp; &nbsp; &nbsp; &nbsp; // encode the plain password&nbsp; &nbsp; &nbsp; &nbsp; $user->setPassword(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $passwordEncoder->encodePassword(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $user,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $form->get('plainPassword')->getData()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; &nbsp; &nbsp; $entityManager = $this->getDoctrine()->getManager();&nbsp; &nbsp; &nbsp; &nbsp; $entityManager->persist($user);&nbsp; &nbsp; &nbsp; &nbsp; $entityManager->flush();&nbsp; &nbsp; &nbsp; &nbsp; // do anything else you need here, like send an email&nbsp; &nbsp; &nbsp; &nbsp; return $this->redirectToRoute('any_route');&nbsp; &nbsp; }&nbsp; &nbsp; return $this->render('registration/register.html.twig', [&nbsp; &nbsp; &nbsp; &nbsp; 'form' => $form->createView(),&nbsp; &nbsp; ]);}

哔哔one

错误似乎在树枝文件中。您尝试显示该$plainPassword成员,但很可能没有在实体上定义它(在我开始使用 symfony 的过程中,发生了很多此错误)。尝试删除它,然后检查错误。至于密码的编码,请看这个 url,因为它解释了密码是如何在 security.yml 文件中加密的。您需要定义将被编码的类,算法可以更改为自定义的,您可以构建,为此,请检查此 URL。编码没有那么复杂,但错误与编码无关,所以请试试这个并更新帖子。

大话西游666

问题在别处。如果您调用不存在的“plainPassword”方法,请检查您的树枝或表单,并将其替换为“密码”。
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答