Symfony 2凭据不好

时间:2014-09-16 13:27:11

标签: php symfony login

我有一个管理员,但我无法登录。它会导致错误的凭据错误。项目连接到数据库,因为我从实体生成表格并且它工作正常。我的猜测是问题在于用户角色,但我不确定。

感谢您的建议。

security.yml

security:
encoders:
    administrators:
     class: Nasivin\AdminBundle\Entity\Admin
     algorithm: plaintext

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

providers:
  administrators:
    entity: { class: NasivinAdminBundle:Admin, property: username }

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

    admin:
      pattern: ^/admin/
      provider: administrators
      anonymous: ~
      form_login:
        check_path: _admin_security_check
        login_path: _admin_login
        default_target_path: /admin/bla
        always_use_default_target_path: true
      logout:
        path: _admin_logout
        target: /admin

access_control:
    #- { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY, requires_channel: https }
    admin_login:
      path: ^/admin/login
      roles: IS_AUTHENTICATED_ANONYMOUSLY
    admin_area:
      path: ^/admin/.*
      roles: ROLE_USER

Admin.php实体类

    <?php
/**
 * Created by PhpStorm.
 * User: Michal
 * Date: 16.9.2014
 * Time: 11:37
 */

namespace Nasivin\AdminBundle\Entity;


use Symfony\Component\Security\Core\User\UserInterface;
use Doctrine\ORM\Mapping as ORM;

/**
 * Nasivin\AdminBundle\Entity\Admin
 *
 * @ORM\Table(name="tb_admin")
 * @ORM\Entity()
 */
class Admin implements UserInterface
{
    /**
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\Column(name="username", type="string", length=25, unique=true)
     */
    protected $username;

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

    /**
     * @ORM\Column(name="salt", type="string", length=64)
     */
    protected $salt;

    /**
     * @inheritDoc
     */
    public function getUsername()
    {
        return $this->username;
    }

    /**
     * @inheritDoc
     */
    public function getSalt()
    {
        // you *may* need a real salt depending on your encoder
        // see section on salt below
        return null;
    }

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

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

    /**
     * @inheritDoc
     */
    public function getRoles()
    {
        return array("ROLE_ADMIN", "ROLE_USER");
    }

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

    /**
     * Set username
     *
     * @param string $username
     * @return Admin
     */
    public function setUsername($username)
    {
        $this->username = $username;

        return $this;
    }

    /**
     * Set password
     *
     * @param string $password
     * @return Admin
     */
    public function setPassword($password)
    {
        $this->password = $password;

        return $this;
    }

    /**
     * Set salt
     *
     * @param string $salt
     * @return Admin
     */
    public function setSalt($salt)
    {
        $this->salt = $salt;

        return $this;
    }
}

login.html.twig

{% extends "::base.html.twig" %}

{% block title %}NasivinAdminBundle:Security:login{% endblock %}

{% block body %}
<h1>Welcome to the Security:login page</h1>
    <form action="{{ path('_admin_security_check') }}" method="post">
        <label for="username">Username:</label>
        <input type="text" id="username" name="_username" />

        <label for="password">Password:</label>
        <input type="password" id="password" name="_password" />

        {#
            If you want to control the URL the user
            is redirected to on success (more details below)
            <input type="hidden" name="_target_path" value="/account" />
        #}

        <button type="submit">login</button>
    </form>
{% endblock %}

2 个答案:

答案 0 :(得分:0)

首先,symfony上的表单必须作为对象。 Symfony集成了一个Form组件,可以轻松处理表单。

关于用户登录,我建议您使用fosuserbundle

答案 1 :(得分:0)

问题是我在数据库中创建了用户。一旦我创建了注册管理员和注册管理员的功能,它就会正常工作。