0

自从 3 小时以来我一直在尝试安装和配置 FOSuser,许多开发人员建议我使用它。我实际上想在不使用 FOS 的情况下制作一个普通的登录表单,但我遇到了很多问题。我遵循了文档中的所有步骤. 安装是好的,配置也是,但每次我尝试登录时,它都会显示“错误的凭据”。所以我以某种方式找到了我执行的这个命令:php app/console fos:user:create i give name-email-password . 它以某种方式起作用,但仅适用于我所写的内容,我的意思是当我在注册表中注册用户并尝试登录时,它会显示“凭据错误”。我希望我很清楚,否则请告诉我您需要知道什么这里是我的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,
        ));
    }
4

1 回答 1

0

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

于 2013-07-20T10:50:09.223 回答