gpt4 book ai didi

redirect - 如何在 LoginListener 中的 onSecurityInteractiveLogin 方法中重定向 - Symfony2

转载 作者:行者123 更新时间:2023-12-05 00:35:48 25 4
gpt4 key购买 nike

我们想在登录后重定向到一个路径或另一个路径。此外,我们需要在此路径中传递 de language。

我们有一个登录监听器作为服务来检查这一点。我们的问题是做重定向 Action 。
代码:

<service id="acme.login_listener" class="acme\DemoBundle\Listener\LoginListener">
<tag name="kernel.event_listener" event="security.interactive_login" method="onSecurityInteractiveLogin" priority="-1" />
<tag name="kernel.event_listener" event="filter.response" method="onKernelResponse" />
<argument type="service" id="service_container" />
<argument type="service" id="router" />
</service>

监听器代码:
namespace Acme\DemoBundle\Listener;

use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
use Symfony\Component\DependencyInjection\ContainerInterface;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\Router;

class LoginListener
{
protected $container;
protected $router;

public function __construct(ContainerInterface $container, Router $router)
{
$this->container = $container;
$this->router = $router;
}

public function onSecurityInteractiveLogin(InteractiveLoginEvent $event)
{
$token = $event->getAuthenticationToken();
$request = $event->getRequest();

$username = $token->getUsername();

$em = $this->container->get('doctrine')->getEntityManager();

$user = $em->getRepository('acmeDemoBundle:User')->findOneByUsername($username);


$locale = strtolower($user->getUsuari()->getIdioma()->getCodi6391());
$locale = 'ca'; //force locale test

$session = $request->getSession();
$session->setLocale($locale);
$session->set('idioma', $locale);
$session->set('_locale', $locale);
$session->set('locale', $locale);

$url = $this->router->generate('onePath', array('_locale' => $locale));

//$event->setResponse(new RedirectResponse($url));
//$event->stopPropagation();
//echo $url;exit;

$response = new RedirectResponse($url);
return $response;
...

我们的问题是在 onSecurityInteractiveLogin 函数中重定向到所需的路径。

我们尝试执行 $event->setResponse 但它不起作用。这个 $event var 没有这个方法。

任何的想法?
谢谢你的帮助。

编辑:第二个选项(2012 年 1 月 27 日)

以下: http://www.reecefowell.com/2011/10/26/redirecting-on-loginlogout-in-symfony2-using-loginhandlers/

我尝试使用处理程序。我配置了services.xml:
<service id="company.login_success_handler" class="project\demoBundle\Handler\LoginSucessHandler">
<tag name="monolog.logger" channel="project" />
<argument type="service" id="logger" />
<argument type="service" id="router" />
</service>

ON security.yml:
firewalls:
main:
pattern: /.*
form_login:
check_path: /login_check
login_path: /login
success_handler: company.login_success_handler
use_forward: false

在 Symfony/src/project/demoBundle/Handler/loginSuccessHandler.php
<?php 

namespace project\demoBundle\Handler;

use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\SecurityContext;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\Router;

class LoginSuccessHandler implements AuthenticationSuccessHandlerInterface
{

protected $router;
protected $security;

public function __construct(Router $router, SecurityContext $security)
{
$this->router = $router;
$this->security = $security;
echo "hello world";exit;
}

public function onAuthenticationSuccess(Request $request, TokenInterface $token)
{

echo "hello world 2";exit;
if ($this->security->isGranted('ROLE_SUPER_ADMIN'))
{
$response = new RedirectResponse($this->router->generate('category_index'));
}
elseif ($this->security->isGranted('ROLE_ADMIN'))
{
$response = new RedirectResponse($this->router->generate('category_index'));
}
elseif ($this->security->isGranted('ROLE_USER'))
{
// redirect the user to where they were before the login process begun.
$referer_url = $request->headers->get('referer');

$response = new RedirectResponse($referer_url);
}

return $response;
}

}

但是如果我执行 web 应用程序,它不会进入这个函数,也不会显示“Hello world”。

你知道我在做什么坏事吗?
谢谢!

最佳答案

如果您需要更改响应,请使用处理程序而不是事件监听器。更多信息在这里:http://www.reecefowell.com/2011/10/26/redirecting-on-loginlogout-in-symfony2-using-loginhandlers/

关于redirect - 如何在 LoginListener 中的 onSecurityInteractiveLogin 方法中重定向 - Symfony2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8972069/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com