gpt4 book ai didi

Symfony2 $user 必须是 UserInterface 的一个实例

转载 作者:行者123 更新时间:2023-12-04 15:38:12 24 4
gpt4 key购买 nike

我在 Symfony2 中遇到登录和身份验证的问题。异常(exception)是“$user 必须是 UserInterface 的实例、实现 __toString 方法的对象或原始字符串。”

调试我的代码我可以注意到我尝试登录我的应用程序的用户可以成功通过身份验证 (app/log/dev.log) 但凭据 var 为空:

Debugging

AbstractToken 中的用户变量具有来自数据库的用户数据。

我继续在 ContextListener->refreshUser 函数中调试,我得到这些值:

Debugging2

一切都具有 null 值并且在 Symfony\Bridge\Doctrine\Security\User\EntityUserProvider->refreshUser 函数上返回变量 $refreshedUser 为 null,因此当 ContextListener 类上的函数 $token->setUser($refreshedUser) 失败并且抛出异常。

我写下我的 security.yml 和我正在使用的实体:

安全.yml:

security:
encoders:
Pladuch\BackBundle\Entity\BaseUser:
algorithm: sha512
encode_as_base64: false
iterations: 1

providers:
sga:
entity: { class: 'PladuchBackBundle:Usuario', property: username }

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

sga:
pattern: ^/gestion
anonymous: ~
form_login:
login_path: pladuch_login_sga
check_path: pladuch_login_check
default_target_path: pladuch_sga_index
csrf_provider: form.csrf_provider
provider: sga
logout:
path: pladuch_logout_sga
target: pladuch_login_sga

access_control:
- { path: ^/gestion/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/gestion, roles: ROLE_ADMIN }

抽象类 BaseUser:
<?php

namespace Pladuch\BackBundle\Entity;


use Symfony\Component\Security\Core\User\AdvancedUserInterface;

abstract class BaseUser implements AdvancedUserInterface, \Serializable
{
protected $id;

protected $salt;

protected $username;

protected $password;

public function __construct()
{
$this->isActive = true;
$this->salt = $this->generateSalt();
}

public function serialize()
{
return serialize(array($this->id, $this->username, $this->password));
}

public function unserialize($serialized)
{
list($this->id, $this->username, $this->password) = unserialize($serialized);
}

public function getRoles()
{
return array('ROLE_ADMIN');
}

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

public function setPassword($password)
{
$this->password = $password;
}

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

public function eraseCredentials()
{
}

public function setSalt($salt)
{
$this->salt = $salt;

return $this;
}

public function getSalt()
{
return $this->salt;
}

public function generateSalt()
{
return base_convert(sha1(uniqid(mt_rand(), true)), 16, 36);
}

public function isAccountNonExpired()
{
return true;
}

public function isAccountNonLocked()
{
return true;
}

public function isCredentialsNonExpired()
{
return true;
}

public function isEnabled()
{
return true;
}
}

类Usuario:
<?php

namespace Pladuch\BackBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
* Usuario
*
* @ORM\Table(name="usuario",
* uniqueConstraints={
* @ORM\UniqueConstraint(name="username", columns={"username"})
* },
* indexes={@ORM\Index(name="FK_USUARIO_ROL", columns={"rol_id"})})
* @ORM\Entity(repositoryClass="Pladuch\BackBundle\Repository\UsuarioRepository")
*/
class Usuario extends BaseUser
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\Id()
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
protected $id;

/**
* @var string
*
* @ORM\Column(name="username", type="string", length=255, nullable=false)
*/
protected $username;

/**
* @var string
*
* @ORM\Column(name="password", type="string", length=1024, nullable=false)
*/
protected $password;

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

/**
* @var string
*
* @ORM\Column(name="email", type="string", length=255, nullable=false)
*/
protected $email;

/**
* @var Rol
*
* @ORM\ManyToOne(targetEntity="Rol", inversedBy="id")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="rol_id", referencedColumnName="id")
* })
*/
protected $rol;

/**
* @var bool
*
* @ORM\Column(name="activo", type="boolean", nullable=true)
*/
protected $activo = true;



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

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

return $this;
}

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

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

return $this;
}

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

/**
* 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;
}

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

return $this;
}

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

/**
* Set rol
*
* @param Rol $rol
* @return Usuario
*/
public function setRol(Rol $rol = null)
{
$this->rol = $rol;

return $this;
}

/**
* Get rol
*
* @return Rol
*/
public function getRol()
{
return $this->rol;
}

/**
* @return array|\Symfony\Component\Security\Core\Role\Role[]
*/
public function getRoles()
{
return array($this->getRol()->getRol());
}

/**
* Set activo
*
* @param $activo
* @return $this
*/
public function setActivo($activo)
{
$this->activo = $activo;

return $this;
}

/**
* Get activo
*
* @return bool
*/
public function getActivo()
{
return $this->activo;
}
}

我在 UsuarioRepository 中实现了三个函数 loadUserByUsername、refreshUser 和 supportsClass:
class UsuarioRepository extends EntityRepository implements UserProviderInterface
{
public function loadUserByUsername($username)
{
$q = $this->createQueryBuilder('u')
->where('u.username = :username')
->setParameter('username', $username)
->getQuery();

try {
$user = $q->getSingleResult();
} catch (NoResultException $e) {
$message = sprintf('Unable to find an active Usuario object identified by %s', $username);
throw new UsernameNotFoundException($message, 0, $e);
}

return $user;
}

public function refreshUser(UserInterface $userInterface)
{
$class = get_class($userInterface);

if (! $this->supportsClass($class)) {
throw new UnsupportedUserException(sprintf('Instances of %s are not suppoted', $class));
}
}

public function supportsClass($class)
{
return $this->getEntityName() === $class || is_subclass_of($class, $this->getEntityName());
}
}

感谢您的帮助。

亲切的问候。

P.S:我使用的是 Symfony 2.5.6

最佳答案

好吧,我忘了在我的存储库中的 refreshUser 函数上添加返回语句...

return $this->find($userInterface->getId());

或者
return $userInterface;

$userInterface 有经过身份验证的用户,所以我不需要 $this->find() 方法。这解决了一切

关于Symfony2 $user 必须是 UserInterface 的一个实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27196221/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com