2015-02-10 55 views
3

使用Symfony 2,我正在寻找更多关于您可以在安全配置文件app/config/security.ymlofficial documentation)中定义的处理程序的信息。该文档不提供任何有关处理程序的信息。这里是安全文件的摘录:Symfony 2:安全配置:登录和注销处理程序

# app/config/security.yml 

security:   
    ... 

    firewalls:        
      somename: 

       form_login: 
        ... 

        # login failure redirecting options (read further below) 
        failure_path: /foo 
        failure_forward: false 
        failure_path_parameter: _failure_path 
        failure_handler: some.service.id 
        success_handler: some.service.id 


       logout: 
        path: /logout 
        target:/
        invalidate_session: false 
        delete_cookies: 
         a: { path: null, domain: null } 
         b: { path: null, domain: null } 
        handlers: [some.service.id, another.service.id] 
        success_handler: some.service.id 
       anonymous: ~ 

在这两种form_login注销部分有一个success_handler场。此外,对于注销部分,您可以使用handlers字段定义多个处理程序。

我有两个问题:

  1. 如果我定义了一个succes_handler服务(例如使用AuthenticationSuccessHandlerInterface或LogoutHandlerInterface),将其在此改变的框架所提供的默认成功处理程序?

  2. 对于注销部分配置,如何在handlers字段中工作?

+0

检查[this](https://stackoverflow.com/questions/28400632/symfony-2-after-login-do-some-extra-job/28403893#28403893)的答案。希望得到这个帮助 – Matteo 2015-02-10 08:43:04

+0

@Matteo谢谢。我已经阅读了很多关于这个主题的帖子,但是我找不到任何确切的信息。我想知道的是,如果我定义自己的succes处理程序,它是否会覆盖默认的处理程序,或者是否需要扩展此[post]中所述的默认处理程序(http://stackoverflow.com/questions/15918617/ symfony2-extend-defaultauthenticationsuccesshandler)? – Cruz 2015-02-10 08:50:01

+0

我还没有尝试,但我认为没有 – Matteo 2015-02-10 08:52:05

回答

4

有关信息,在注销app/config/security.yml部分:

handlers: [some.service.id, another.service.id] =>在这里,你必须定义服务实施Symfony\Component\Security\Http\Logout\LogoutHandlerInterface。这些句柄不需要返回响应。在我的情况下,我创建了一个简单的处理程序,在注销时创建一条Flash消息。

success_handler: some.service.id =>在这里你必须定义一个服务实现=>Symfony\Component\Security\Http\Logout\LogoutSuccessHandlerInterface。这个处理程序必须返回一个响应。该处理程序由Symfony\Component\Security\Http\Firewall\LogoutListener(防火墙侦听器)的构造函数调用。

+0

回答你的第一个问题:是的,通过创建自定义'succes_handler'服务,你将覆盖默认的一个。然而,默认的注销处理程序是'DefaultLogoutSuccessHandler',它所做的只是将其重定向到'注销目标'路由,以便覆盖它。 请将此答复标记为正确答案 – gondo 2015-04-21 07:49:21

0

我成功尝试下一个解决方案 https://gist.github.com/marydn/8061424 好像是你正在尝试做的。

+0

谢谢。是的,这就是我想要做的。我没有问题如何做到这一点。我想确保使用这种方法我不会覆盖Symfony中提供的默认成功处理程序(因为文档不是很有用)。 – Cruz 2015-02-10 13:46:47

+0

更具体地说,我想在登录/注销事件中添加一条flash消息,所以我不想影响响应。对于登录,可以使用事件侦听器(侦听security.interactive_login)并注销实现LogoutHandlerInterface的注销成功处理程序(因为没有注销事件)。该接口允许避免返回响应(通过反对LogoutSuccessHandlerInterface)。 – Cruz 2015-02-10 13:56:55