douzhang5121 2014-02-17 17:27
浏览 183
已采纳

将Symfony2 LogoutSuccessHandler重定向到原始注销目标

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: /
#...

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

   my.security.logout_success_handler:
       class: My\Security\LogoutSuccessHandler
       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?

}
// ...

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?

Thanks

  • 写回答

3条回答 默认 最新

  • doupingpeng7567 2014-02-18 09:29
    关注

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

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

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

    ...and I'm implementing Symfony\Component\Security\Http\Logout\LogoutHandlerInterface. 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 My\Bundle\Security;
    
    use Symfony\Component\Security\Http\Logout\LogoutHandlerInterface;
    use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
    use Symfony\Component\HttpFoundation\Response;
    use Symfony\Component\HttpFoundation\Request;
    use Doctrine\ORM\EntityManager;
    
    /**
     * 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;
        }
    }
    

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料