Fosuser symfony2的凭据错误

时间:2013-07-19 17:54:19

标签: symfony fosuserbundle

我正在尝试3个小时来安装和配置FOSuser,许多开发人员都建议我使用它。我想要在不使用FOS的情况下制作正常的登录表单,但是我遇到了很多问题。我遵循了所有步骤在文档中。安装还可以,配置也不过每次当我尝试登录时,都会显示“Bad credentials”。所以我发现这个命令我执行了:php app / console fos:user:create i give name-email-password 。它以某种方式工作,但只有我写的东西,我的意思是当我在我的注册表单中注册用户并尝试登录时显示“Bad credentials”。我希望我很清楚别的请告诉我你需要知道什么 这是我的Users.php,我有我的所有用户信息登录...

namespace test\indexBundle\Document;
use FOS\UserBundle\Model\User as BaseUser;
use Symfony\Component\Security\Core\User\UserInterface;

use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;

/**
 * 
 * @MongoDB\Document
 */

class Users extends BaseUser 
{


    /**
     * @MongoDB\Id
     */
    protected $id;

    /**
     * @MongoDB\String
     */
    protected $userId;

    /**
     * @MongoDB\String
     */
    protected $userEmail;

    /**
     * @MongoDB\String
     */
    protected $userPassword;


    /**
     * @MongoDB\String
     */
    protected $salt;


    /**
     * @MongoDB\Int
     */
    protected $isActive;


    public function __construct()
    {
        parent::__construct();
        $this->isActive = true;
        $this->salt = md5(uniqid(null, true));
    }


    /**
     * Set id
     *
     * @param id $id
     */
    public function setId($id)  
    {  
        $this->id = $id;  
    }  

    /**
     * Get id
     *
     * @return id $id
     */
    public function getId()  
    {  
        return $this->id;  
    } 


    /**
     * Set userId
     *
     * @param string $userId
     */
    public function setUserId()  
    {  
        $this->userId = $this->salt;  
    }  

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

    /**
     * Set userName
     *
     * @param string $userName
     */
    public function setUserName($userName)  
    {  
        $this->userName = $userName;  
    }  

    /**
     * Get userName
     *
     * @return string $userName
     */
    public function getUserName()  
    {  
        return $this->username;  
    } 


    /**
     * Set userEmail
     *
     * @param string $userEmail
     */
    public function setUserEmail($userEmail)  
    {  
        $this->userEmail = $userEmail;  
    }  

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


    /**
     * Set userPassword
     *
     * @param string $userPassword
     */
    public function setPassword($userPassword)  
    {  
        $this->userPassword = $userPassword;  
    }  

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

    /**
     * @inheritDoc
     */
    public function getSalt()
    {
        return '';
    }



    /**
     * @inheritDoc
     */
    public function getRoles()
    {
        return array('ROLE_USER');
    }

    /**
     * @inheritDoc
     */
    public function eraseCredentials()
    {
    }

    /**
     * @see \Serializable::serialize()
     */
    public function serialize()
    {
        return serialize(array(
            $this->id
        ));
    }

    /**
     * @see \Serializable::unserialize()
     */
    public function unserialize($serialized)
    {
        list (
            $this->id
        ) = unserialize($serialized);
    }
}

这里是我的security.yml:

jms_security_extra:
    secure_all_services: false
    expressions: true

security:
    encoders:

        FOS\UserBundle\Model\UserInterface: sha512
        test\indexBundle\Document\Users:
            algorithm:        sha1 
            encode_as_base64: false
            iterations:      1  


    role_hierarchy:
        ROLE_ADMIN:       ROLE_USER
        ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]


    providers:


        fos_userbundle:
            id: fos_user.user_provider.username_email

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

        main:
            pattern:   ^/
            anonymous: true


            form_login:
                check_path: /login_check
                login_path: /login
                provider: fos_userbundle
                post_only: true
                use_forward: false
                username_parameter:             email
                password_parameter:             password
                failure_path:                   null
                failure_forward:                false
                target_path_parameter: redirect_url
            logout:
                path:   /logout
                target: /blog


    access_control:
        - { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }

和登录功能:

public function loginAction()
    {

        $request = $this->getRequest();
        $session = $request->getSession();




        if ($this->get('security.context')->isGranted('IS_AUTHENTICATED_FULLY')) 
        {
            return $this->redirect($this->generateUrl('index_homepage'));
        }



        if ($request->attributes->has(SecurityContext::AUTHENTICATION_ERROR)) 
        {
            $error = $request->attributes->get(SecurityContext::AUTHENTICATION_ERROR);
        } 
        else 
        {
            $error = $session->get(SecurityContext::AUTHENTICATION_ERROR);
            $session->remove(SecurityContext::AUTHENTICATION_ERROR);
        }


        return $this->render('indexBundle:index:logIn.html.twig', array(
                'last_username' => $session->get(SecurityContext::LAST_USERNAME),
                'error'         => $error,
        ));
    }

1 个答案:

答案 0 :(得分:0)

我可能错了,但我认为FOSUserBundle要求用户在创建后激活,如果您使用表单注册,它会发送出去并通过电子邮件发送我认为的链接。我认为如果没有电子邮件,您可以使用app/console fos:user:activate激活。