登录Symfony2后,成功处理程序不起作用 [英] Success handler not working after Symfony2 login

查看:119
本文介绍了登录Symfony2后,成功处理程序不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目标是在Symfony2中成功通过身份验证后执行一些操作.

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

为此,我扩展了AuthenticationSuccessHandlerInterface为表单登录创建服务,以便成为其成功处理程序.

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

这是security.yml文件(声明成功处理程序的位置)中的防火墙:

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 

这是LoginSuccessHandler服务(在UserBundle中创建):

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;
    }
}

这是UserBundle中的services.xml:

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>

正在调用LoginSuccessHandler构造函数,但没有收到任何错误消息.

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

我遇到的问题是登录成功后没有调用onAuthenticationSuccess.可能是我缺少了什么吗?

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

推荐答案

在我的工作解决方案中,我在侦听器中将方法 onSecurityInteractiveLogin 实现为:

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

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

也尝试实现此方法.

我具有相同的配置(security.yml和服务定义),但是我不使用fosuserbunde.

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

希望获得帮助

这篇关于登录Symfony2后,成功处理程序不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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