duankaolei2921 2015-01-07 19:17
浏览 68
已采纳

Symfony2登录后,成功处理程序无法正常工作

The goal is to do something after authentication success in Symfony2.

In order to do that I have extended AuthenticationSuccessHandlerInterface creating a service for the form login in order to be its success handler.

Here is the firewall in the security.yml file (where the success handler is declared):

firewalls:
    main:
        pattern: ^/
        form_login:
            check_path: fos_user_security_check
            provider: fos_userbundle
            csrf_provider: form.csrf_provider
            success_handler: foo_user.component.authentication.handler.login_success_handler 
        logout:       true
        anonymous:    true 

Here is the LoginSuccessHandler service (created in the UserBundle):

namespace Foo\UserBundle\Component\Authentication\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;
    }

    public function onAuthenticationSuccess(Request $request, TokenInterface $token)
   {
        $referer_url = $request->headers->get('referer');                       
        $response = new RedirectResponse($referer_url);

        return $response;
    }
}

And here is the services.xml from the UserBundle:

<?xml version="1.0" encoding="utf-8"?> 

<container xmlns="http://symfony.com/schema/dic/services" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     
xsi:schemaLocation="http://symfony.com/schema/dic/services 
http://symfony.com/schema/dic/services/services-1.0.xsd">
    <parameters>
        <parameter key="foo_user.component.authentication.handler.login_success_handler.class">
            Foo\UserBundle\Component\Authentication\Handler\LoginSuccessHandler
        </parameter>
    </parameters>
    <services>
        <service id="foo_user.component.authentication.handler.login_success_handler" 
        class="%foo_user.component.authentication.handler.login_success_handler.class%">
            <tag name="monolog.logger" channel="security"/>
            <argument type="service" id="router"/>
            <argument type="service" id="security.context"/>
            <argument type="service" id="service_container"/>
        </service>
    </services>
</container>

The LoginSuccessHandler constructor is being called and I'm getting no error messages.

The problem I'm having is that onAuthenticationSuccess isn't being called after login success. May be I'm missing something?

  • 写回答

1条回答 默认 最新

  • dougu3988 2015-01-08 05:47
    关注

    In my working solutions I implements the method onSecurityInteractiveLogin in my listener as:

    public function onSecurityInteractiveLogin(InteractiveLoginEvent $event) 
        {
            $user = $event->getAuthenticationToken()->getUser();
        }
    

    Try implementing this method too.

    I have your identical configuration (security.yml and service definition) but i don't use fosuserbunde.

    hope this help

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器