我在Symfony2中遇到登录和身份验证方面的问题.例外情况是"$ user必须是UserInterface的实例,实现__toString方法的对象或原始字符串".
调试我的代码我可以注意到我尝试登录我的应用程序的用户可以成功进行身份验证(app/log/dev.log),但凭据var为null:
AbstractToken中的用户变量具有来自数据库的用户数据.
我继续在ContextListener-> refreshUser函数中调试,我得到这些值:
一切都有值null并且在Symfony\Bridge\Doctrine\Security\User\EntityUserProvider-> refreshUser函数返回变量$ refreshedUser为null,因此当ContextListener类上的函数$ token-> setUser($ refreshedUser)失败时抛出异常.
我写下我的security.yml和我正在使用的实体:
security.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:
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类:
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()); } }
谢谢您的帮助.
亲切的问候.
PS:我正在使用Symfony 2.5.6
好的,我忘了在我的存储库中的refreshUser函数上添加返回句子...
return $this->find($userInterface->getId());
要么
return $userInterface;
$ userInterface具有经过身份验证的用户,因此我不需要$ this-> find()方法.这解决了一切