Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Symfony 2.2在登录后始终发送给我登录页面_Php_Session_Symfony_Login - Fatal编程技术网

Php Symfony 2.2在登录后始终发送给我登录页面

Php Symfony 2.2在登录后始终发送给我登录页面,php,session,symfony,login,Php,Session,Symfony,Login,我在Symfony 2.2.1上有一个简单的登录系统,问题是何时进入安全区域是显示登录表单并输入用户和通行证,但始终要将我发送到/login页面,有时将我发送到/。通过查看日志,我得到以下信息 [2013-04-22 20:25:01] event.DEBUG: Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\ProfilerListener::onKernelRequ

我在Symfony 2.2.1上有一个简单的登录系统,问题是何时进入安全区域是显示登录表单并输入用户和通行证,但始终要将我发送到
/login
页面,有时将我发送到
/
。通过查看日志,我得到以下信息

[2013-04-22 20:25:01] event.DEBUG: Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\ProfilerListener::onKernelRequest". [] []
[2013-04-22 20:25:01] event.DEBUG: Notified event "kernel.request" to listener "Symfony\Bundle\FrameworkBundle\EventListener\SessionListener::onKernelRequest". [] []
[2013-04-22 20:25:02] event.DEBUG: Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\FragmentListener::onKernelRequest". [] []
[2013-04-22 20:25:02] event.DEBUG: Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\RouterListener::onKernelRequest". [] []
[2013-04-22 20:25:02] request.INFO: Matched route "login_check" (parameters: "_route": "login_check") [] []
[2013-04-22 20:25:02] event.DEBUG: Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\LocaleListener::onKernelRequest". [] []
[2013-04-22 20:25:02] event.DEBUG: Notified event "kernel.request" to listener "Symfony\Component\Security\Http\Firewall::onKernelRequest". [] []
[2013-04-22 20:25:02] doctrine.DEBUG: SELECT t0.id AS id1, t0.nombre AS nombre2, t0.contrasena AS contrasena3, t0.salt AS salt4 FROM usuario t0 WHERE t0.nombre = ? LIMIT 1 ["rkmax"] []
[2013-04-22 20:25:02] doctrine.DEBUG: SELECT t0.id AS id1, t0.nombre AS nombre2 FROM rol t0 INNER JOIN usuario_rol ON t0.id = usuario_rol.rol_id WHERE usuario_rol.usuario_id = ? [8] []
[2013-04-22 20:25:02] security.INFO: User "" has been authenticated successfully [] []
...
[2013-04-22 20:25:02] security.WARNING: Username "" could not be found. [] []
正如您所看到的,当在SecurityContext中写入时,用户是空的,但我不知道原因

课程和设置 两个实体
Usuario
(用户)和
Rol
(角色)都实现了与symfony安全性对话的接口

class Rol implements RoleInterface

//Repository/Usuario
class Usuario extends EntityRepository implements UserProviderInterface
{
    public function loadUserByUsername($login)
    {
        $usuario = $this->findOneBy(array('nombre' => $login));

        if (!$usuario) {
            throw new UsernameNotFoundException(
                sprintf("El usuario '%s' no existe o el nombre esta mal escrito", $login)
            );
        }

        return $usuario;
    }

    public function refreshUser(UserInterface $usuario)
    {
        return $this->loadUserByUsername($usuario);
    }

    public function supportsClass($class)
    {
        return $class === 'Iscltda\SecurityBundle\Entity\Usuario';
    }
}

class Usuario implements UserInterface, \Serializable
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="nombre", type="string", length=200, nullable=false)
     */
    private $nombre;

    /**
     * @var string
     *
     * @ORM\Column(name="contrasena", type="string", length=64, nullable=false)
     */
    private $contrasena;

    /**
     * @var string
     *
     * @ORM\Column(name="salt", type="string", length=64, nullable=false)
     */
    private $salt;

    /**
     * @var \Doctrine\Common\Collections\Collection
     *
     * @ORM\ManyToMany(targetEntity="Rol", inversedBy="usuario")
     * @ORM\JoinTable(name="usuario_rol",
     *   joinColumns={
     *     @ORM\JoinColumn(name="usuario_id", referencedColumnName="id")
     *   },
     *   inverseJoinColumns={
     *     @ORM\JoinColumn(name="rol_id", referencedColumnName="id")
     *   }
     * )
     */
    private $rol;

    /**
     * Constructor
     */
    public function __construct()
    {
        $this->rol = new \Doctrine\Common\Collections\ArrayCollection();
    }

    public function __toString()
    {
        return $this->getNombre();
    }


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

    /**
     * Set nombre
     *
     * @param string $nombre
     * @return Usuario
     */
    public function setNombre($nombre)
    {
        $this->nombre = $nombre;

        return $this;
    }

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

    /**
     * Set contrasena
     *
     * @param string $contrasena
     * @return Usuario
     */
    public function setContrasena($contrasena)
    {
        $this->contrasena = $contrasena;

        return $this;
    }

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

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

        return $this;
    }

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

    /**
     * Add rol
     *
     * @param \Iscltda\SecurityBundle\Entity\Rol $rol
     * @return Usuario
     */
    public function addRol(\Iscltda\SecurityBundle\Entity\Rol $rol)
    {
        $this->rol[] = $rol;

        return $this;
    }

    /**
     * Remove rol
     *
     * @param \Iscltda\SecurityBundle\Entity\Rol $rol
     */
    public function removeRol(\Iscltda\SecurityBundle\Entity\Rol $rol)
    {
        $this->rol->removeElement($rol);
    }

    /**
     * Get rol
     *
     * @return \Doctrine\Common\Collections\Collection
     */
    public function getRol()
    {
        return $this->rol;
    }

    public function getRoles()
    {
        return $this->rol->toArray();
    }

    public function getPassword()
    {
        return $this->getContrasena();
    }

    public function getUsername()
    {
        $this->getNombre();
    }

    public function eraseCredentials()
    {

    }

    /**
     * Serializes the content of the current User object
     * @return string
     */
    public function serialize()
    {
        return \serialize(array(
            $this->id,
            $this->nombre,
            $this->salt,
            $this->contrasena
        ));
    }

    /**
     * Unserializes the given string in the current User object
     * @param serialized
     */
    public function unserialize($serialized)
    {
        list(
            $this->id,
            $this->nombre,
            $this->salt,
            $this->contrasena
        ) = \unserialize($serialized);
    }
}
routing.yml
设置

login:
    pattern: /admin/login
    defaults: { _controller: MySecurityBundle:Security:login }

login_check:
    pattern: /admin/login_check

logout:
    pattern: /admin/logout
这是我的
security.yml
设置

security:
    encoders:
        MyVendor\MySecurityBundle\Entity\Usuario:
            algorithm: sha256
            encode-as-base64: true
            iterations: 10

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

    providers:
        user_db:
            entity: { class: MyVendor\MySecurityBundle\Entity\Usuario, property: nombre }

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

        login:
            pattern:  ^/admin/login$
            security: false
            anonymous: ~

        secured_area:
            provider: user_db
            security: true
            pattern:    ^/admin
            form_login:
                login_path: /admin/login
                check_path: /admin/login_check
            logout:
                path:   /logout
                target: /
更新 我发现问题是我在方法
getUsername
i change to中没有返回任何内容

public function getUsername()
{
    return $this->getNombre();
}
但仍然是发送给我的登录页面

更新2 我在UserProvider类中发现了上一个问题,方法
refreshUser
错误,我改为

public function refreshUser(UserInterface $usuario)
{
    return $this->loadUserByUsername($usuario->getUsername());
}
这种方法很有效

public function refreshUser(UserInterface $usuario)
{
    return $this->loadUserByUsername($usuario->getUsername());
}