简体   繁体   English

自定义实体登录错误Symfony2.7

[英]Custom Entity login error Symfony2.7

I'm using Symfony 2.7 and I'm having trouble logging users. 我正在使用Symfony 2.7,无法登录用户。 I use Custom Entity and I have created a custom entity "Operator" with proper variables and methods. 我使用自定义实体,并使用适当的变量和方法创建了一个自定义实体“运算符”。 I use two login methods in security file. 我在安全文件中使用两种登录方法。 One is in_memory and I use it for admin login which works correctly and the other one is user_db . 一个是in_memory ,我将其用于管理员登录,该登录可以正常工作,另一个是user_db I use bcrypt algorithm to encrypt the passwords. 我使用bcrypt算法来加密密码。 When I try to log in a user, it displays "Invalid credentials" message. 当我尝试登录用户时,它显示“无效凭据”消息。 What am I doing wrong? 我究竟做错了什么? Thank you! 谢谢!

security.yml 安全性

security:
    encoders:
        Symfony\Component\Security\Core\User\User: plaintext
        Application\AdminBundle\Entity\Operator:
            algorithm: bcrypt

providers:
        chain_provider:
            chain:
                providers: [ in_memory, user_db ]

user_db:
            entity:
                class: ApplicationAdminBundle:Operator
                property: username

SecurityController.php SecurityController.php

<?php

namespace Application\AdminBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;

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

    $error = $authenticationUtils->getLastAuthenticationError();

    return $this->render('ApplicationAdminBundle:security:login.html.twig',
        array(
            'error'         => $error,
        )
    );
}

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

}

"Operator" Entity and setPassword(), getPassword() methods “操作员”实体和setPassword(),getPassword()方法

public function setPassword($password)
    {
        $this->password = password_hash ($password, PASSWORD_BCRYPT);

        return $this;
    }

    /**
     * Get password
     *
     * @return string 
     */
    public function getPassword()
    {
        return $this->password;
    }

Probably you should not use password_hash you have to use symfony's encoder who knows how to handle the encryption of the password. 可能您不应该使用password_hash ,而必须使用知道如何处理密码加密的symfony编码器。 Try to implement something like this in your code ( in register controller usually ) : http://symfony.com/doc/current/book/security.html#dynamically-encoding-a-password 尝试在您的代码中实现类似这样的内容(通常在寄存器控制器中): http : //symfony.com/doc/current/book/security.html#dynamically-encoding-a-password

Visit this page http://symfony.com/doc/current/book/security.html to fully understand how symfony2 security works. 访问此页面http://symfony.com/doc/current/book/security.html可以完全了解symfony2安全性的工作原理。

Solution: 解:

All I had done wrong was the database table because the I had set the password field as varchar(25) and the hashed password could not be fully inserted. 我做错的只是数据库表,因为我将密码字段设置为varchar(25) ,并且散列密码无法完全插入。 I changed it to varchar(255) and everything is ok now. 我将其更改为varchar(255) ,现在一切正常。 Every user can now login. 现在,每个用户都可以登录。

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

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