Nat*_*ova 1 php session symfony fosuserbundle symfony-2.5
我们的项目使用FOSUserBundle和Symfony 2.5.我们需要在登录后向用户会话添加自定义数据,该数据驻留在数据库中并且是动态的,但应该在所有模板和应用程序内的任何位置访问.
我正在考虑LoginManager.php从/ user-bundle/Security覆盖该类,但我也不完全确定这是否是最好的选择.
看起来这个logInUser()方法是添加我们的自定义更改的地方,因为它实际上设置了令牌,但是再次,如果有更聪明的方法,我肯定会使用它.
您可以添加安全交互式登录侦听器,在该侦听器中,您将可以访问存储在会话中的登录令牌.此令牌继承的Symfony \分量\安全\核心\认证\令牌\ AbstractToken因此,它有办法 "的setAttribute($名称,$值)" 和 "setAttributes(数组$属性)".无论你在此属性中设置什么,都要与用户和令牌一起存储在会话中.
请注意这是序列化的事实,并确保如果存储对象以实现serialize/unserialize方法(如果需要)以便不存在循环引用问题.
我推荐这种方法,因为它似乎符合您的要求:
有关身份验证事件的更多信息,请查看symfony cookbook:http: //symfony.com/doc/current/components/security/authentication.html#authentication-events
您还可以使用以下代码作为指导.
在服务yml
security.interactive_login.listener:
class: %security.interactive_login.listener.class%
arguments: ['@security.context', '@session']
tags:
- { name: kernel.event_listener, event: security.interactive_login, method: onSecurityInteractiveLogin }
Run Code Online (Sandbox Code Playgroud)
在你的听众中
use Symfony\Component\Security\Core\SecurityContextInterface;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
class SecurityListener
{
public function __construct(SecurityContextInterface $security, Session $session)
{
$this->security = $security;
$this->session = $session;
}
public function onSecurityInteractiveLogin(InteractiveLoginEvent $event)
{
$token = $event->getAuthenticationToken();
$token->setAttribute('key','some stuff i want later');
}
}
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助,
Alexandru Cosoi
| 归档时间: |
|
| 查看次数: |
1105 次 |
| 最近记录: |