无法使用FOSUserBundle登录

时间:2016-04-18 20:40:02

标签: php symfony fosuserbundle symfony2-easyadmin

尝试使用FOSUserBundle和EasyAdminBundle登录的所有可能性,但没有成功

这是我的security.yml:

security:
    encoders:
        MyApp\UserBundle\Entity\User:
            algorithm: sha1
            encode_as_base64: false
            iterations: 1
        FOS\UserBundle\Model\UserInterface: bcrypt

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

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

        auth_area:
            pattern: ^/users/(login|register|uploadPhoto|forgotPassword)
            anonymous: ~

        admin_area:
            pattern: ^/admin
            form_login:
                provider: fos_userbundle
                csrf_parameter: _csrf_security_token
                csrf_token_id: alongstringhere
                login_path: fos_user_security_login
                check_path: fos_user_security_check
                default_target_path: admin_index
                always_use_default_target_path: true
            logout:
                path: fos_user_security_logout
                target: fos_user_security_login
            anonymous: ~
            stateless: true

        secured_area:
            pattern:    ^/
            anonymous: ~
            stateless: true
            simple_preauth:
                authenticator: MyApp.sessionkey_authenticator


    providers:
        administrators:
            entity: { class: MyAppUserBundle:User, property: email }
        fos_userbundle:
            id: fos_user.user_manager

    access_control:
        - { path: ^/api/doc, roles: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/users/register, roles: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/users/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/users/logout, roles: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/users/uploadPhoto, roles: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/users/forgotPassword, roles: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/admin/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/admin/login_check$, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/admin/logout$, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/admin/register$, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/admin, roles: [ROLE_ADMIN, ROLE_USER] }
        - { path: ^/, roles: ROLE_USER }

用于fos用户的config.yml:

fos_user:
    db_driver: orm # other valid values are 'mongodb', 'couchdb' and 'propel'
    firewall_name: main
    user_class: MyApp\UserBundle\Entity\Admin

使用routes.yml:

easy_admin_bundle:
    resource: "@MyAppMainBundle/Controller/AdminController.php"
    type:     annotation
    prefix:   /admin

fos_user:
    resource: "@FOSUserBundle/Resources/config/routing/all.xml"
    prefix:   /admin

admin_login:
    pattern: /admin/login

admin_logout:
    pattern: /admin/logout

admin_index:
    pattern:  /admin

管理实体:

namespace MyApp\UserBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\Common\Collections\ArrayCollection;

/**
 * Admin
 *
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="MyApp\UserBundle\Entity\Repository\AdminRepository")
 * @ORM\Entity
 */
class Admin extends BaseUser
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    public function __construct()
    {
        $this->setUpdatedAt(new \DateTime(date('Y-m-d H:i:s')));

        if($this->getCreatedAt() == null)
        {
            $this->setCreatedAt(new \DateTime(date('Y-m-d H:i:s')));
        }
    }


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

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="updated_at", type="datetime")
     */
    private $updatedAt;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="created_at", type="datetime")
     */
    private $createdAt;

    /**
     * Set updatedAt
     *
     * @param \DateTime $updatedAt
     *
     * @return Admin
     */
    public function setUpdatedAt($updatedAt)
    {
        $this->updatedAt = $updatedAt;

        return $this;
    }

    /**
     * Get updatedAt
     *
     * @return \DateTime
     */
    public function getUpdatedAt()
    {
        return $this->updatedAt;
    }

    /**
     * Set createdAt
     *
     * @param \DateTime $createdAt
     *
     * @return Admin
     */
    public function setCreatedAt($createdAt)
    {
        $this->createdAt = $createdAt;

        return $this;
    }

    /**
     * Get createdAt
     *
     * @return \DateTime
     */
    public function getCreatedAt()
    {
        return $this->createdAt;
    }

    /**
     * @ORM\PrePersist
     * @ORM\PreUpdate
     */
    public function updatedTimestamps()
    {
        $this->setUpdatedAt(new \DateTime(date('Y-m-d H:i:s')));

        if($this->getCreatedAt() == null)
        {
            $this->setCreatedAt(new \DateTime(date('Y-m-d H:i:s')));
        }
    }
}

当我尝试登录时,它不会抛出任何错误,凭据有效(用户是在控制台中创建的),但我一直被重定向到登录页面,我无法继续使用管理面板。 任何人都可以帮忙吗? 提前谢谢!

1 个答案:

答案 0 :(得分:1)

stateless: true与表单login一起使用并没有多大意义,因为您需要在会话中保留有关已通过身份验证的用户的信息。您应该在配置中将其设置为false