将 Symfony2 LogoutSuccessHandler 重定向到原始注销目标 [英] Redirect Symfony2 LogoutSuccessHandler to original logout target

查看:23
本文介绍了将 Symfony2 LogoutSuccessHandler 重定向到原始注销目标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在注销时修改我的用户对象.为此,我有一个 security.yml,其中包含以下内容(除其他外) -

I need to modify my user object on logout. To do this, I have a security.yml that contains the following (amongst other things) -

#...
    logout:
        success_handler: my.logout_success_handler
        target: /
#...

...这定义了一个注销成功处理程序,它在 services.yml 中定义如下 -

...this defines a logout success handler, which is defined in services.yml like this -

   my.security.logout_success_handler:
       class: MySecurityLogoutSuccessHandler
       arguments: ["@security.context", "@doctrine.orm.default_entity_manager"]

...最后,我的处理程序的业务端是这样的 -

...finally, the business-end of my handler is like this -

// ...
public function onLogoutSuccess(Request $request)
{

    $user = $this->securityContext->getToken()->getUser();

    // ... do stuff with the user object....
    $this->em->flush();

    // now what?

}
// ...

那么,它在哪里说现在呢?"我知道我需要返回一个 Response 对象.理想情况下,我希望该响应对象将用户重定向到 security.yml 中 logout.target 中定义的任何内容.

So, where it says "now what?" I understand that I need to return a Response object. Ideally I want that response object to redirect the user to whatever is defined in logout.target in the security.yml.

有什么简单的方法可以查询吗?或者,更好的是,有没有另一种方法可以做这种事情,完全不需要我参与请求/响应对象?

Is there an easy way I can query that? Or, even better, is there another way of doing this kind of thing that doesn't require me to get involved with the request/response objects at all?

谢谢

推荐答案

所以,我想我已经找到了正确的答案 -

So, I think I've figured out the right answer -

而不是实现 LogoutSuccessHandlerInterface 和配置 logout.success_handler,我的 security.yml 现在看起来像这样 -

Rather than implementing LogoutSuccessHandlerInterface and configuring a logout.success_handler, my security.yml now looks like this -

# ...
logout:
    handlers: [my.bundle.security.logout_handler]
# ...

...我正在实施 SymfonyComponentSecurityHttpLogoutLogoutHandlerInterface.令人困惑的命名,但这似乎是执行注销后操作而不必涉及响应对象的首选方式.我的实现看起来像这样 -

...and I'm implementing SymfonyComponentSecurityHttpLogoutLogoutHandlerInterface. Confusing naming, but this seems to be the preferred way of doing post-logout operations without having to get involved with the response object. My implementation looks like this -

namespace MyBundleSecurity;

use SymfonyComponentSecurityHttpLogoutLogoutHandlerInterface;
use SymfonyComponentSecurityCoreAuthenticationTokenTokenInterface;
use SymfonyComponentHttpFoundationResponse;
use SymfonyComponentHttpFoundationRequest;
use DoctrineORMEntityManager;

/**
 * Do post logout stuff
 */
class LogoutHandler implements LogoutHandlerInterface
{

    /**
     * @var EntityManager
     */
    protected $em;

    /**
     * Constructor
     * @param EntityManager $em
     */
    public function __construct(EntityManager $em)
    {

        $this->em = $em;
    }

    /**
     * Do post logout stuff
     */
    public function logout(Request $request, Response $response, TokenInterface $authToken)
    {
        $user = $authToken->getUser();

        // do stuff with the user object...
        $this->em->flush();

        return $response;
    }
}

...如您所见,LogoutHandlerInterface 提供了一个预制的 $response 对象,我可以在完成后返回该对象.

...as you can see, the LogoutHandlerInterface provides a pre-made $response object that I can just return when I'm finished.

这篇关于将 Symfony2 LogoutSuccessHandler 重定向到原始注销目标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆