简体   繁体   English

登录表单问题 - Symfony 2.7

[英]Login form issue - Symfony 2.7

I am following the Symfony book and cookbook recipes and I met problem with simple login form - no matter if entered login/pass are valid, message shows up - 'Invalid credentials'.我正在关注 Symfony 书籍和食谱食谱,但我遇到了简单登录表单的问题——无论输入的登录名/密码是否有效,都会显示消息——“凭据无效”。 Users are loaded via Doctrine (User class which implements UserInterface).用户通过 Doctrine(实现 UserInterface 的 User 类)加载。 Source codes :源代码:

Security file:安全文件:

providers:
    user_provider:
      entity:
        class: BakaMainBundle:User

firewalls:

    dev:
        pattern: ^/(_(profiler|wdt)|css|images|js)/
        security: false

    default:
        anonymous: ~
        http_basic: ~
        provider: user_provider
        form_login:
           login_path: /login
           check_path: /login_check
           target_path_parameter: /index/welcome

access_control:
  - { path: ^/admin, roles: ROLE_ADMIN }

encoders:
  Baka\MainBundle\Entity\User:
    algorithm: bcrypt
    cost: 12

Controller :控制器 :

class SecurityController extends Controller
{
    /**
     * @Route("/login", name="login_route")
     */
    public function loginAction()
    {
        $authUtils = $this->get('security.authentication_utils');
        $error = $authUtils->getLastAuthenticationError();
        $enteredUsername = $authUtils->getLastUsername();

        return $this->render('BakaMainBundle::Login.html.twig',
            array
            (
                'last_username' => $enteredUsername,
                'error' => $error,
                'site' => 'login'
            ));
    }

    /**
     * @Route("/login_check", name="login_check")
     */
    public function loginCheckAction()
    {

    }
}

User repository :用户存储库:

class UserRepository extends \Doctrine\ORM\EntityRepository implements UserProviderInterface
{
    public function loadUserByUsername($username)
    {
        $user = $this->createQueryBuilder('u')
            ->where('u.username = :username OR u.email = :email')
            ->setParameter('username', $username)
            ->setParameter('email', $username)
            ->getQuery()
            ->getOneOrNullResult();

        if ($user === null)
        {
            $returnMessage = sprintf(
                '%s - such username of email adress does not exist in database! Try again with other login data.',
                $username);
            throw new UnsupportedUserException($returnMessage);
        }

        return $user;
    }

    public function refreshUser(UserInterface $user)
    {
        $userClass = get_class($user);
        if (!$this->supportsClass($userClass))
        {
            throw new UnsupportedUserException
            (sprintf('Ops! Something goes wrong. Your user class is not supported by security system.'));
        }

        return $this->find($user->getId());
    }

    public function supportsClass($userclass)
    {
        return $this->getEntityName() === $userclass || is_subclass_of($userclass, $this->getEntityName());
    }

And the form html tag :和表单 html 标签:

<form action="{{ path('login_check') }}" method="post">

Any suggestions?有什么建议? I will be grateful for resolving my problem.我将不胜感激解决我的问题。

I think you should use the class namespace instead of the bundle name, when specifying the provider class.我认为在指定提供程序类时,您应该使用类名称空间而不是包名称。 Also, you need to specify which property you will be selecting as the "username" from your Entity :此外,您需要指定您将从Entity选择哪个property作为“用户名”:

security:
    providers:
        user_provider:
          entity:
            class: Baka\MainBundle\Entity\User
            property: username (this should be an existing property of your entity class)

Also, your User entity needs to implement Symfony\\Component\\Security\\Core\\User\\UserInterface (or AdvancedUserInterface ).此外,您的User实体需要实现Symfony\\Component\\Security\\Core\\User\\UserInterface (或AdvancedUserInterface )。 Once you're done with that, everything should work if you have users in the database with a properly encoded password.完成此操作后,如果数据库中的用户具有正确编码的密码,那么一切都应该可以正常工作。

You should read:你应该阅读:

  1. How to Load Security Users from the Database (the Entity Provider) to understand how to load users from the database How to Load Security Users from the Database (the Entity Provider)了解如何从数据库中加载用户
  2. Security to get better understanding of how the security component works and how it should be configured.安全性以更好地了解安全组件的工作原理以及应该如何配置它。

I've already identified the reason of issue, and it transpired to be trivial - field which serves as an Encoded Password row in the DB had 15 characters long limit :我已经确定了问题的原因,并且它被证明是微不足道的 - 作为数据库中编码密码行的字段有 15 个字符的长度限制:

    /**
     * @ORM\Column(type="string", length=15)
     */
    protected $password;

And since '12 rounds' bcrypt needs much more digits to represent plain password, Doctrine was forced to shorten encrypted pass so it was impossible to decode later.由于“12 轮”bcrypt 需要更多数字来表示普通密码,Doctrine 被迫缩短加密密码,因此以后无法解码。 After changing to suggested by Symfony size the problem has gone :更改为 Symfony 建议的大小后,问题就消失了:

    /**
     * @ORM\Column(type="string", length=4096)
     */
    protected $password;

Thank you for all support.谢谢大家的支持。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM