简体   繁体   English

设置Symfony2 security.context登录后的用户数据

[英]Setting Symfony2 security.context User data after login

Using Symfony2 I want to augment the security.content user after login with information obtained after login. 使用Symfony2我想在登录后用登录后获得的信息扩充security.content用户。

So in the login success code I do the following in AccountController : 所以在登录成功代码中,我在AccountController执行以下操作:

$user = $this->get('security.context')->getToken()->getUser();

// Get everything we'll ever need for this user
$user->fillDetails($this, $this->container);

$token = new UsernamePasswordToken($user, null, 'main', $user->getRoles());

// Give it to the security context
$this->container->get('security.context')->setToken($token);

return $this->redirect($this->generateUrl('AccountBundle_homepage'));

If I immediately retrieve the user again after calling setToken() this information that is set in the User object in fillDetails() is still present. 如果我立即调用后,再次检索用户setToken()这个信息在用户对象设置fillDetails()仍然存在。

However in the controller action for AccountBundle_homepage when I get the user using 但是当我让用户使用时,在AccountBundle_homepage的控制器操作中

$user = $this->get('security.context')->getToken()->getUser();

The extra information I set in fillDetails() is no longer there, or 0 . 我在fillDetails()设置的额外信息不再存在,或者为0

Any help appreciated. 任何帮助赞赏。

The security context creates a token on each request, that means you can't modify a token, redirect the user and expect getting data set on the previous token. 安全上下文在每个请求上创建一个令牌,这意味着您无法修改令牌,重定向用户并期望在先前令牌上获取数据集。 If you don't persist your user, it won't work. 如果您不保留用户,则无法使用。 The user is reloaded on each request too. 每个请求也会重新加载用户。

You can find more information about the token here: http://symfony.com/doc/current/cookbook/security/custom_authentication_provider.html#the-token 您可以在此处找到有关令牌的更多信息: http//symfony.com/doc/current/cookbook/security/custom_authentication_provider.html#the-token

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM