找不到Symfony注册CSRF令牌无效

时间:2019-04-16 19:29:45

标签: php forms symfony csrf-protection

所有人,谢谢您今后的帮助

我正在创建一个博客,并且希望将登录和注册页面置于同一视图上 为此,我用登录方法

并以相同的视图返回两种形式 注册和连接

我已经尝试制作一个渲染器,但是它不起作用,甚至删除了令牌按钮,都无效


public function login(AuthenticationUtils $authenticationUtils, Request $request, UserPasswordEncoderInterface $passwordEncoder, MailerService $mailerService, \Swift_Mailer $mailer): Response
    {
        // get the login error if there is one
        $error = $authenticationUtils->getLastAuthenticationError();
        // last username entered by the user
        $lastUsername = $authenticationUtils->getLastUsername();

        $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('password')->getData()
                )
            );

            $user->setConfirmationToken($this->generateToken());

            $entityManager = $this->getDoctrine()->getManager();
            $entityManager->persist($user);
            $entityManager->flush();

            $token = $user->getConfirmationToken();
            $email = $user->getEmail();
            $username = $user->getUsername();

            $mailerService->sendToken($mailer, $token, $email, $username, 'registration.html.twig');

            return $this->redirectToRoute('app_login');
        }

        return $this->render('security/login.html.twig', ['last_username' => $lastUsername, 'error' => $error, 'registrationForm' => $form->createView()]);
    }

模板:


    <div class="container">

        <div class="row">
            <div class="col-md-6">
                <div class="section-row">
                    <div class="section-title">
                        <h1 class="title text-center">Connexion</h1>
                    </div>
                    {% if error %}
                        <div class="alert alert-danger">{{ error.messageKey|trans(error.messageData, 'security') }}</div>
                    {% endif %}
                    <form method="post">
                        <div class="row">
                            <div class="col-md-12">
                                <div class="form-group">
                                    <input class="input" value="{{ last_username }}" type="text" name="username" placeholder="Nom d'utilisateur">
                                </div>
                            </div>
                            <div class="col-md-12">
                                <div class="form-group">
                                    <input class="input" type="password" name="password" placeholder="Mot de passe">
                                </div>
                            </div>
                            <div class="col-md-12">
                                <div class="form-group">
                                    <label>
                                        <input type="checkbox" name="_remember_me"> Se souvenir de moi
                                    </label>
                                </div>
                            </div>
                            <div class="col-md-12">
                                <div class="form-group">
                                    <a href="{{ path('forgotten_password') }}">Mot de passe oublier</a>
                                </div>
                            </div>
                            <input type="hidden" name="_csrf_token" value="{{ csrf_token('authenticate') }}">
                            <div class="col-md-12">
                                <button class="primary-button" type="submit">
                                    Se connecter
                                </button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
            <div class="col-md-6">
                <h1>Register</h1>

                {{ form_start(registrationForm) }}
                {{ form_row(registrationForm.username) }}
                {{ form_row(registrationForm.password.first) }}
                {{ form_row(registrationForm.password.second) }}

                <button class="btn">Register</button>
                {{ form_end(registrationForm) }}

            </div>
        </div>

    </div>

我想注册它时不起作用的问题使我发回令牌无效,而对于此连接,它可以正常工作 谢谢您对我的朋友们的帮助!

0 个答案:

没有答案