使用Symfony2登录时出现错误“用户提供程序必须返回UserInterface对象”

时间:2013-03-27 18:45:55

标签: symfony symfony-2.2

我正在尝试修改symfony附带的现有演示登录,从in_memory到用户的数据库存储。我还在:

[2013-03-27 19:24:34] security.INFO: Authentication request failed: The user provider must return a UserInterface object. [] []

所以我改变了:

security.yml

  providers:
  user_db:
        entity: { class: Cremesk\AgentisBundle\Entity\Pouzivatel, property: meno }

还创建了提到的实体:

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

 /**
 * Pouzivatel
 *
 * @ORM\Table('pouzivatel')
 * @ORM\Entity
 */
 class Pouzivatel implements UserInterface
 .etc

并将控制器保留原样:

 use Symfony\Bundle\FrameworkBundle\Controller\Controller;
 use Symfony\Component\Security\Core\SecurityContext;
 use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
 use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
 use JMS\SecurityExtraBundle\Annotation\Secure;


 /**
  * @Route("/demo/secured")
  */
 class SecuredController extends Controller
 {
     /**
      * @Route("/login", name="_demo_login")
      * @Template()
      */
     public function loginAction()
     {
         if ($this->get('request')->attributes->has(SecurityContext::AUTHENTICATION_ERROR)) {
        $error = $this->get('request')->attributes->get(SecurityContext::AUTHENTICATION_ERROR);
    } else {
        $error = $this->get('request')->getSession()->get(SecurityContext::AUTHENTICATION_ERROR);
    }

    return array(
        'last_username' => $this->get('request')->getSession()->get(SecurityContext::LAST_USERNAME),
        'error'         => $error,
    );
}

/**
 * @Route("/login_check", name="_security_check")
 */
public function securityCheckAction()
{
    // The security layer will intercept this request
}

/**
 * @Route("/logout", name="_demo_logout")
 */
public function logoutAction()
{
    // The security layer will intercept this request
}

/**
 * @Route("/hello", defaults={"name"="World"}),
 * @Route("/hello/{name}", name="_demo_secured_hello")
 * @Template()
 */
public function helloAction($name)
{
    return array('name' => $name);
}

/**
 * @Route("/hello/admin/{name}", name="_demo_secured_hello_admin")
 * @Secure(roles="ROLE_ADMIN")
 * @Template()
 */
public function helloadminAction($name)
{
    return array('name' => $name);
}
 }

我无法登录工作。 将不胜感激任何帮助。谢谢。

1 个答案:

答案 0 :(得分:2)

检查用户实体的名称空间和安全配置匹配。

namespace Cremesk\AgentisBundle\Entity;

use ....

class Pouzivatel implements UserInterface
{
    ....

并在您的security.yml

providers:
    user_db:
        entity: { class: Cremesk\AgentisBundle\Entity\Pouzivatel, property: meno }