简体   繁体   English

如何在 symfony 4 注销中重定向到外部 url

[英]How to redirect to external url in symfony 4 logout

Just wondering if there is an easy solution to this in Symfony 4. Normally users would logout and be sent back to the home page.只是想知道在 Symfony 4 中是否有一个简单的解决方案。通常用户会注销并被发送回主页。 But there is one page where it is checked that the user is currently authenticated on another site, if this is incorrect I have a link that logs the user out of my site and redirects to the external site.但是有一个页面可以检查用户当前是否在另一个站点上进行了身份验证,如果这是不正确的,我有一个链接可以让用户退出我的站点并重定向到外部站点。 I managed this on an old silex based version of the site using the following routing of controllers in app.php我使用 app.php 中的以下控制器路由在基于 silex 的旧版本站点上管理此操作

$app->get('/logout', $app->factory(function() use($app) {
$pid = $app['request_stack']->getCurrentRequest()->query->get('pid');
$message = $app['request_stack']->getCurrentRequest()->query->get('message');
$redirect = $app['request_stack']->getCurrentRequest()->query->get('redirect');
return $app->redirect(FRAMEWORK_URL."/logout?pid=$pid&message=$message&redirect=$redirect");
})

); );

Thanks谢谢

Martyn马丁

Set value of logout.target for a firewall in security.yaml to an external URL:设定值logout.target在防火墙security.yaml外部URL:

    firewalls:
        main:
                ...
            logout:
                ...
                target: 'EXTERNAL URL'

Value of logout.target could be an URL or app route name. logout.target值可以是 URL 或应用程序路由名称。 App route and related controller could be used to create dynamic redirect targets.应用路由和相关控制器可用于创建动态重定向目标。

Unfortunately logout.target will not work as there is special check (in Symfony\\Component\\Security\\Http\\HttpUtils::createRedirectResponse ) to match the request domain to the redirection domain.不幸的是logout.target将不起作用,因为有特殊检查(在Symfony\\Component\\Security\\Http\\HttpUtils::createRedirectResponse )将请求域与重定向域匹配。 If they're not the same it will set the target to / .如果它们不相同,它将目标设置为/

Proper way of redirecting to external URL is to create class which implements Symfony\\Component\\Security\\Http\\Logout\\LogoutSuccessHandlerInterface , for example like this:重定向到外部 URL 的正确方法是创建实现Symfony\\Component\\Security\\Http\\Logout\\LogoutSuccessHandlerInterface ,例如像这样:

<?php

// src/Security/CustomLogoutSuccessHandler.php

class CustomLogoutSuccessHandler implements LogoutSuccessHandlerInterface
{
    private $target;

    public function __construct(string $target)
    {
        $this->target = $target;
    }

    public function onLogoutSuccess(Request $request)
    {
        return new RedirectResponse($this->target);
    }
}

Then create service in services.yaml :然后在services.yaml创建服务:

services:
  App\Security\CustomLogoutSuccessHandler:
    arguments: ['%env(resolve:LOGOUT_TARGET_URL)%'] # resolve from .env (or you can get from anywhere)

and finally tell security.yaml to use your handler instead of default one:最后告诉security.yaml使用您的处理程序而不是默认处理程序:

security:
  firewalls:
    main:
      logout:
        success_handler: 'App\Security\CustomLogoutSuccessHandler'

Ok so tried this好的所以试过这个

I made a new route in a controller:我在控制器中创建了一条新路由:

    /**
 * @Route("/redirectafterlogout/{url?/}", name="redirectafterlogout")
 */
public function redirectafterlogout($url)
{
    if($url == "/"){
        // redirects to the "homepage" route
        return $this->redirectToRoute('homepage');
    } else {
        return $this->redirect('http://'.$url);
    }
}

I added this to security.yaml我将此添加到 security.yaml

logout:
            path:   logout
            target: 'redirectafterlogout'

it works fine and logs me out and puts me on the homepage if i use the normal logout link.如果我使用正常的注销链接,它可以正常工作并将我注销并将我放在主页上。 However, if i try a link like:但是,如果我尝试使用以下链接:

it redirects me but does not seem to log me out?它重定向了我,但似乎没有让我退出? I can go back with me browser and I'm still logged in?我可以用我的浏览器返回并且我仍然登录吗?

Not sure what I am doing wrong?不确定我做错了什么?

Thanks谢谢

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

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