当前位置: 首页 > 知识库问答 >
问题:

Symfony3错误登录表单

范京
2023-03-14

我目前正在创建一个Symfony3应用程序,我无法使我的登录表单工作;我遵循以下步骤:

  • http://symfony.com/doc/current/cookbook/security/form_login_setup.html
  • http://symfony.com/doc/current/cookbook/doctrine/registration_form.html

注册系统正在工作,但我无法登录。我想知道我的代码是否有问题:

security.yml

# To get started with security, check out the documentation:
# http://symfony.com/doc/current/book/security.html
security:
    encoders:
        AppBundle\Entity\Compte:
            algorithm: bcrypt
    # http://symfony.com/doc/current/book/security.html#where-do-users-come-from-user-providers
    providers:
        mysqlprovider:
            entity:
                class: AppBundle:Compte
                property: username
    firewalls:
        # disables authentication for assets and the profiler, adapt it according to your needs
        dev:
            pattern: ^/(_(profiler|wdt)|css|images|js)/
            security: false

        main:
            anonymous: ~
            provider: mysqlprovider
            form_login:
                login_path: connexion
                check_path: connexion
                csrf_token_generator: security.csrf.token_manager
            logout: true
            # activate different ways to authenticate

            # http_basic: ~
            # http://symfony.com/doc/current/book/security.html#a-configuring-how-your-users-will-authenticate

            # form_login: ~
            # http://symfony.com/doc/current/cookbook/security/form_login_setup.html
    access_control:
        - { path: ^/connexion, roles: IS_AUTHENTICATED_ANONYMOUSLY }

AppBundle/Controller/SecurityController.php

namespace AppBundle\Controller;

use AppBundle\Entity\Compte;
use AppBundle\Form\CompteType;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\Security\Core\SecurityContextInterface;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\HttpFoundation\Request;

class SecurityController extends Controller
{
    /**
     * @Route("/connexion", name="Connexion")
     */
    public function loginAction(Request $request)
    {
        $authenticationUtils = $this->get('security.authentication_utils');

        $error = $authenticationUtils->getLastAuthenticationError();

        $lastUsername = $authenticationUtils->getLastUsername();

        return $this->render('AppBundle:Security:connexion.html.twig', array(
            'last_username' => $lastUsername,
            'error'         => $error,
        ));
    }

    /**
     * @Route("/enregistrement", name="Enregistrement")
     */
    public function registerAction(Request $request)
    {
        $user = new Compte();
        $form = $this->createForm(CompteType::class, $user);

        $form->handleRequest($request);
        if ($form->isSubmitted()) {
            $user->setPassword($this->get('security.password_encoder')->encodePassword($user, $user->getPlainPassword()));
            $em = $this->getDoctrine()->getManager();
            $em->persist($user);
            $em->flush();

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

        return $this->render(
            'AppBundle:Security:enregistrement.html.twig',
            array('form' => $form->createView())
        );
    }
}

AppBundle/Entity/Compte.php

<?php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Security\Core\User\UserInterface;

/**
 * Class Compte
 * @ORM\Entity
 * @ORM\Table(name="Compte")
 * @ORM\Entity(repositoryClass="AppBundle\Repository\CompteRepository")
 * @UniqueEntity(fields="email", message="Cette adresse mail est déjà prise !")
 * @UniqueEntity(fields="username", message="Ce nom d'utilisateur est déjà pris !")
 */
class Compte implements UserInterface, \Serializable
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @ORM\Column(type="boolean")
     * @Assert\NotBlank
     */
    private $isActive;

    /**
     * @ORM\Column(type="string", length=24, unique=true)
     * @Assert\NotBlank
     */
    private $username;

    /**
     * @ORM\Column(type="string", length=64, unique=true)
     * @Assert\NotBlank
     * @Assert\Email()
     */
    private $email;

    /**
     * @Assert\NotBlank
     * @Assert\Length(max=4096)
     */
    private $plainPassword;

    /**
     * @ORM\Column(name="`password`", type="string", length=128)
     * @Assert\NotBlank
     */
    private $password;

    /**
     * @ORM\OneToOne(targetEntity="Survivant", inversedBy="compte")
     */
    private $survivant;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }

    public function getUsername()
    {
        return $this->username;
    }

    public function setUsername($username)
    {
        $this->username = $username;
    }

    public function getPlainPassword()
    {
        return $this->plainPassword;
    }

    public function setPlainPassword($password)
    {
        $this->plainPassword = $password;
    }

    public function setPassword($password)
    {
        $this->password = $password;
    }

    public function getSalt()
    {
        return null;
    }

    public function eraseCredentials() 
    { 
    }

    public function getRoles() 
    { 
        return array('ROLE_USER'); 
    }

    public function getPassword() 
    { 
        return $this->password; 
    }

    public function serialize()
    {
        return serialize(array(
            $this->id,
            $this->username,
            $this->password,
        ));
    }

    public function unserialize($serialized)
    {
        list (
            $this->id,
            $this->username,
            $this->password,
        ) = unserialize($serialized);
    }

    /**
     * Constructor
     */
    public function __construct()
    {
        $this->isActive = true;
    }

    /**
     * Set survivant
     *
     * @param \AppBundle\Entity\Survivant $survivant
     *
     * @return Compte
     */
    public function setSurvivant(\AppBundle\Entity\Survivant $survivant = null)
    {
        $this->survivant = $survivant;

        return $this;
    }

    /**
     * Get survivant
     *
     * @return \AppBundle\Entity\Survivant
     */
    public function getSurvivant()
    {
        return $this->survivant;
    }
}

AppBundle/资源/视图/安全/connexion.html.twig

<body>
    {% if error %}
        <div>{{ error.messageKey|trans(error.messageData, 'security') }}</div>
    {% endif %}

    <form action="{{ path('Connexion') }}" method="post">
        <label for="username">Username:</label>
        <input type="text" id="username" name="_username" value="{{ last_username }}" />

        <label for="password">Password:</label>
        <input type="password" id="password" name="_password" />
        <input type="hidden" name="_csrf_token" value="{{ csrf_token('authenticate') }}" />
        <button type="submit">login</button>
    </form>
</body>

共有1个答案

臧亦
2023-03-14

错误出现在Symfony教程的security.yml中security.yml如下所示:

# app/config/security.yml
security:
    # ...

    firewalls:
        main:
            anonymous: ~
            form_login:
                login_path: login
                check_path: login

但是登录路径和检查路径是错误的;您应该将路线与/一起使用,因此路线必须如下所示:

# app/config/security.yml
security:
    # ...

    firewalls:
        main:
            anonymous: ~
            form_login:
                login_path: /login
                check_path: /login

我不知道这是否是一个常见的错误,但我希望这能帮助到某人。

 类似资料:
  • 为什么在给用户授权的时候会出这个错误。一切都是按照指示做的。

  • 编辑:我发现了一个类似的问题,专门针对Facebook的登录。我正在使用电子邮件认证,但问题/解决方案可能是相同的。 userinfo={nsunderlyingerror=0x14704d4b0{Error domain=firauthinternalerrordomain code=3“(null)”userinfo={firautherroruserinfoderializedrespons

  • 我已经创建了一个登录名。html页面和登录名。php脚本。在脚本中,它将首先获得我们输入的用户名和密码,然后使用数据库检查用户名或密码是否存在,如果用户有效,则它将检查他的部门id,并使用部门权限打开他们的页面,但问题是无论他们的部门是什么,都会一直打开管理页面。请帮我做这件事。 这是我的login.html代码 登录。php脚本

  • 我试图了解Spotify的登录/授权流程。我遵循一个教程,最终得到了这段代码。当我尝试登录时,会出现以下错误:-canOpenURL:URL失败:“spotify操作:/”-错误:“(null)” 我查看了回购协议,发现我需要将spotify action和spotify添加到信息中。在LSApplicationQueriesSchemes下注册。然而,在这样做后,我仍然得到上述错误。

  • 我使用cakephp2.1和我在UsersController中使用登录操作如下。 和登录名。ctp代码如下。 当使用电子邮件和密码提交表单时,用户无法登录,因此显示错误“无效电子邮件或密码,请重试”。就连我都把这美元给递了-

  • 我试图将谷歌登录集成到我的应用程序中。我没有后端服务器,我只是得到登录到我的应用程序的谷歌帐户的详细信息。 我第一次尝试使用谷歌登录的例子,但我得到了一个错误(除了打印下面的stacktrace外,没有进行任何代码更改)。我只是使用了signianctivity示例,因为我没有后端服务器。 密码 从我所读到的,这个问题可能是由SHA1一代引起的。 我遵循了完整的指南,但显然它不起作用。 我从gra