简体   繁体   English

Symfony 在注销事件之前重定向用户

[英]Symfony redirect user BEFORE logout event

I have a Symfony 3.4 project (with FosUser if this helps) with multiple login types (local,Oauth2,ldap) that all logouts (in the end) are handled from a local user via the default logout method.我有一个 Symfony 3.4 项目(如果有帮助,则使用 FosUser)具有多种登录类型(本地、Oauth2、ldap),所有注销(最终)都是通过默认注销方法从本地用户处理的。

In SAML protocol however the logout needs to take place with redirect to Idp (Identity provider, here the Azure AD) take the success response and logout the user local.然而,在 SAML 协议中,注销需要重定向到 Idp(身份提供程序,此处为 Azure AD)进行成功响应并在本地注销用户。

In my online search only this documentation for 5.1 symfony (and not for 3.4) https://symfony.com/blog/new-in-symfony-5-1-simpler-logout-customization explains how is possible to trigger some code before logout event happen and not after (like a logout listener or subscriber does).在我的在线搜索中,只有 5.1 symfony(而不是 3.4)的文档 https://symfony.com/blog/new-in-symfony-5-1-simpler-logout-customization解释了如何在之前触发一些代码注销事件发生而不是之后(就像注销侦听器或订阅者那样)。

How is possible for a 3.4 Symfony project to check if the login type of a user is saml or not BEFORE the logout event happens? 3.4 Symfony 项目如何在注销事件发生之前检查用户的登录类型是否为 saml? If it is saml, can I redirect him to Idp (and back to custom route) and continue the process?如果是 saml,我可以将他重定向到 Idp(并返回自定义路由)并继续该过程吗?

Thanks to @Jakumi comments i manage to run a listener before the default LogoutListener .感谢@Jakumi 评论,我设法在默认LogoutListener之前运行了一个监听器。 First i have to register in services.yaml a listener with priority above 7首先我必须在 services.yaml 注册一个优先级高于 7 的监听器

App\EventListener\BeforeLogoutListener:
  tags:
    - { name: kernel.event_listener, event: kernel.request, priority: 8 }

Here i can't use appHelper or Security to get the current user because this listener runs BEFORE the Security/Http/Firewall.在这里,我不能使用 appHelper 或 Security 来获取当前用户,因为此侦听器在 Security/Http/Firewall 之前运行。 So i stop the event propagation setting new response, redirect the user too a custom logout router and THEN do my validation logic in the user.所以我停止事件传播设置新响应,将用户也重定向到自定义注销路由器,然后在用户中执行我的验证逻辑。

public function onkernelRequest(GetResponseEvent $requestEvent)
{
    //Get data from Database and validate this action

    if ($requestEvent->getRequest()->getPathInfo() === '/logout' &&  $isExternalLogout) {
            $requestEvent->setResponse(new RedirectResponse($this->router->generate('external_logout')));
    }

}

I hope I saved days from someone who was struggling to do the same.我希望我从一个正在努力做同样事情的人那里节省了几天的时间。

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

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