第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 Symfony 5 中編碼密碼?

如何在 Symfony 5 中編碼密碼?

慕尼黑8549860 2022-06-17 10:20:06
我正在嘗試在 Symfony 中對密碼進(jìn)行編碼,并且在仔細(xì)按照此處的文檔進(jìn)行操作之后,似乎我仍然做錯了什么。這是我的 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\”類中具有公共訪問權(quán)限實(shí)體\用戶”。這是我的 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文件中有這個設(shè)置:security:       encoders:        App\Entity\User:            algorithm: auto我想這是我忽略的簡單事情,但我無法讓它發(fā)揮作用。這是我的第一次Symfony。php交響樂密碼加密
查看完整描述

3 回答

?
滄海一幻覺

TA貢獻(xiàn)1824條經(jīng)驗(yàn) 獲得超5個贊

實(shí)際上這是因?yàn)?symfony 檢測到用戶實(shí)體中是否沒有“plainPassword”屬性。使用此“plainPassword”屬性的目的是作為臨時數(shù)據(jù),因此我們可以對其進(jìn)行編碼。您需要做的是在您的表單類型中設(shè)置映射為 false的“plainPassword”屬性。


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.',

        ))

    ;

}

并在您的控制器中將“plainPassword”編碼為“密碼”:


/**

 * @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(),

    ]);

}


查看完整回答
反對 回復(fù) 2022-06-17
?
嗶嗶one

TA貢獻(xiàn)1854條經(jīng)驗(yàn) 獲得超8個贊

錯誤似乎在樹枝文件中。您嘗試顯示該$plainPassword成員,但很可能沒有在實(shí)體上定義它(在我開始使用 symfony 的過程中,發(fā)生了很多此錯誤)。嘗試刪除它,然后檢查錯誤。

至于密碼的編碼,請看這個 url,因?yàn)樗忉屃嗣艽a是如何在 security.yml 文件中加密的。您需要定義將被編碼的類,算法可以更改為自定義的,您可以構(gòu)建,為此,請檢查此 URL。

編碼沒有那么復(fù)雜,但錯誤與編碼無關(guān),所以請試試這個并更新帖子。


查看完整回答
反對 回復(fù) 2022-06-17
?
大話西游666

TA貢獻(xiàn)1817條經(jīng)驗(yàn) 獲得超14個贊

問題在別處。

如果您調(diào)用不存在的“plainPassword”方法,請檢查您的樹枝或表單,并將其替換為“密碼”。


查看完整回答
反對 回復(fù) 2022-06-17
  • 3 回答
  • 0 關(guān)注
  • 194 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號