如何序列化有关身份验证的用户? [英] How to serialize a user regarding authentication?
问题描述
我的应用程序上有一个经过身份验证的部分,但身份验证是通过 oauth 对 3rd 方服务完成的.我从服务获得 200 回调,现在我创建/找到我的用户并将他设置为登录.
I'm have an authenticated section on my app, but the authentication is done via oauth to a 3rd party service. I get the 200 callback from the service, now I create/find my user and set him as logged in.
所以我的提供者是:
providers:
users:
entity: { class: MainBundle:User, property: id }
我的用户实现了安全性的 UserInterface,尽管我的用户实体上没有用户名、密码等属性.我假设 id 是我当时可以使用的唯一标识符.
My user implements the security's UserInterface, although I don't have the username, password, etc properties on my user entity. I assume the id is then the only identifier I can use then.
我的令牌设置如下:
$token = new UsernamePasswordToken($user, null, 'secured_area', $user->getRoles());
$this->securityContext->setToken($token);
我希望暂时不使用 JMS 包来完成此操作;我的序列化看起来像这样:
I wish to do this without using JMS bundle for now; my serialization looks like this:
/**
* Serialize
* @return string|void
*/
public function serialize()
{
return serialize(array(
'id' => $this->getId(),
'display_name' => $this->getDisplayName(),
'email' => $this->getEmail(),
));
}
/**
* Unserialize
* @return mixed|void
*/
public function unserialize($data)
{
$data = unserialize($data);
$this->setId($data['id']);
$this->setDisplayName($data['display_name']);
$this->setEmail($data['email']);
return $this;
}
使用上面的我得到一个无限循环重定向.
Using the above I get an infinite loop redirect.
起初我只序列化了 id,但随后用户的所有其他属性都不可用.
At first I only serialized the id, but then all the other properties of the user isn't available.
然后我尝试序列化整个对象($this),但这给了我 1000 的 xdebug 嵌套级别错误.
Then I tried serializing the whole object ($this), but that gives me a xdebug nesting level error of 1000.
我有点不知道如何使此身份验证与序列化一起工作
I'm a bit lost of how to make this authentication work with serialization
推荐答案
security.yml
security.yml
providers:
users:
entity: { class: MainBundle:User, property: id }
登录逻辑
$token = new UsernamePasswordToken($user, null, 'secured_area', $user->getRoles());
$this->securityContext->setToken($token);
我从用户实体中取出了序列化,也没有实现等效接口.也仍然没有接口属性,虽然这是必需的:
I took out the serialization out of the user entity, also not implementing equatable interface. Also still do not have the interface properties, although this is needed:
public function getUsername()
{
return $this->getId();
}
这篇关于如何序列化有关身份验证的用户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!