Yoo*_*oot 3 authentication handler switch-user symfony
我在我的应用程序中有一个基本的表单登录身份验证,我使用AuthenticationHandlerInterface设置了一个处理程序,我在onAuthenticationSuccess()方法中设置会话变量.
问题是,当我切换到另一个用户(使用ROLE_ALLOWED_TO_SWITCH)时,我的处理程序不再被调用,而我之前设置的会话变量在切换之前仍然是用户的变量.
示例:
(我知道myVar = X-> someAttribute不是一个很好的例子,因为我已经可以从安全令牌对象访问它,但它是为了简化问题)
谢谢
编辑:security.yml的摘录
firewalls:
main:
pattern: ^/
anonymous: ~
switch_user: { role: ROLE_ADMIN, parameter: _switch }
form_login:
provider: sso
success_handler: authentication_handler
login_path: /login
check_path: /login_check
logout:
path: /logout
target: /home
Run Code Online (Sandbox Code Playgroud)
当安全组件成功切换当前用户时,它将security.switch_user使用以下事件类调度事件:https://github.com/symfony/symfony/blob/2.0/src/Symfony/Component/Security/Http/Event/ SwitchUserEvent.php.
因此,您可能需要/想要收听此事件,并在调用侦听器时执行您的逻辑.
要收听此事件,请阅读有关侦听器的symfony cookbook条目:http://symfony.com/doc/current/cookbook/service_container/event_listener.html
services:
rocky.balboa.listener.security_switch_user:
class: Rocky\BalboaBundle\Listener\SecuritySwitchUserListener
tags:
- { name: kernel.event_listener, event: security.switch_user, method: onSecuritySwitchUser }
Run Code Online (Sandbox Code Playgroud)
.
// src/Rocky/BalboaBundle/Listener/SecuritySwitchUserListener.php
namespace Rocky\BalboaBundle\Listener;
use Symfony\Component\Security\Http\Event\SwitchUserEvent;
class SecuritySwitchUserListener
{
public function onSecuritySwitchUser(SwitchUserEvent $event)
{
$newUser = $event->getTargetUser();
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2123 次 |
| 最近记录: |